Binary Search Tree

Can somebody help with this problem: I need to, after entering some value, check how many values in my binary search tree are less than the value entered. I have been thinking quite a lot about it...and I don't really know how to do it. You don't need to give me whole code, just some general idea what to do.
NOTE: This is not a homework assignment; I like to do some algorithm design in my spare time.
Thanks
Traverse the tree and count.
closed account (o1vk4iN6)
^

recursive method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class TreeNode* {
    int data;

   TreeNode* left;
   TreeNode* right;
};

int tree_less_than( TreeNode* node, int value )
{
     return (node->left) ? tree_less_than( node->left, value) : 0 
          + (node->right) ? tree_less_than(node->right, value) : 0 
          + (node->data < value) ? 1 : 0;
}
Last edited on
Topic archived. No new replies allowed.