6

I have a simple question that I am confused about. I know what is the concept of having a key/value pair in a Binary Search Tree and how the tree looks like when it is built.

What I am unsure is of how to search for a value in such a BST if I have no track of what its key would be?

For example:

Let say I have a binary search tree full of integers (as values) and also unique integers (as keys). And let say I want to count the number of times a specific integer (lets say: 200) occurs in this BST. So what I know, 200 is the "value" and not the "key". Hence I dont know the key at all.

How can I search now for all the "200"s in the whole BST? Does it become a simple BST now and I dont need the key at all? But again, the tree is arranged to the left child and the right child using the "key" and not the value.

I can also give you a code sample of how I initialize the BST:

void insertNode(TreeNode *&p, int key, int value)
{
  if (p == NULL) {
    p = new TreeNode;
    p->key = key;
    p->value = value;
    p->left = NULL;
    p->right = NULL;
    return;
  }

  if (key < p->key)
    insertNode(p->left, key, value);
  else
    insertNode(p->right, key, value);
}

Any help would be appreciated.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
Faizan
  • 137
  • 1
  • 2
  • 8
  • You should state what language (computer language) you are programming in. And also the language as a tag to your question. – Annabel Mar 16 '13 at 20:28
  • Sorry about that. I have done the edit now. – Faizan Mar 16 '13 at 20:32
  • If you are searching for the *values*, then either you swapped *key* and *value* in your implementation/concept or you don't need to store a key at all and use the value only. – Potaito Jun 06 '16 at 08:29

2 Answers2

7

You can not take advantage of the fact that the tree is binary search tree if you want to search by value instead of key. So you have no other option but to iterate over the whole tree using BFS for instance.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thanks for the response. Thats what I was thinking but wasn't sure about it. And I am still not sure what to do. This would obviously not be something really useful of not having to use the fact of a key. This means if we are given such a tree, and are asked to search for a value rather than a key, we have no way other than standard search methods, and the concept of a Binary Search Tree dies? Bad. – Faizan Mar 17 '13 at 00:41
1

Binary search tree(BST) is very useful for storing data for rapid access, storage, and deletion. Data in a binary search tree are stored in tree nodes, and must have associated with them an ordinal value or key; these keys are used to structure the tree such that the value of a left child node is less than that of the parent node, and the value of a right child node is greater than that of the parent node. Sometimes, the key and datum are one and the same.

Typical key values include simple integers or strings, the actual data for the key will depend on the application. Lets consider a binary search tree that stores string/double pairs. That is, the key is the string value and the data associated with the key is a double value. Developers can search the tree using string values.

In this case, a basic recursive search algorithm will look like:

 node search (node, key) {
   if node is null then return null;

   if node.key = key then
      return node

   if key < node then
      return search (node.left, key);
   else
      return search (node.right, key);
  }   

where the params are node: root of the tree & key: the value to be searched.

So typically we can search based on the required key but not on the associated value with the key.

Srikanth
  • 471
  • 2
  • 14
  • Thanks for the response Srikanth, but again, as I stated in the question, I have no track of what the key is. I just want to count the number of times a specific value occurs in a BST without knowing any key at all. – Faizan Mar 16 '13 at 21:08