Binary search tree (Distance from node to root)

Hi, so, I need some help, I'm new to this BST thing, so, I don't understand most of the function regarding to this.

I have my tree, and I need to find the number in the tree (given by the user) and get the distance between the node found and the root.

What I've done so far is this, but I don't think it's correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void BinarySearchTree::find()
{
	system("cls");

	BinarySearchTree b;
	int num;
	tree_node* cur;
	tree_node* parent;
	cur=root;
	bool founded = false;
	cout<<"- - - - - - - - - - - - - -"<<endl<<endl;
	cout<<"Insert the number "; cin>>num;
	while(cur!=NULL)
	{
		if(cur->num==num)
		{
			found=cur;
			founded=true;
			break;
		}
		else
		{
			parent=cur;
			if(num>cur->num)
			{
				cur=cur->right;
			}
			else
			{
				cur=cur->left;
			}
		}
	}

	if(!found)
	{
		cout<<"The number doesn't exist."<<endl;
	}
	else
	{
		cout<<"Founded."<<endl;
	}
	
	system("pause>null");
}


I founded the node, but I don't know how to determine the distance.
How many times you looped is how far you've gone from root.
Then, I need to put a counter in the while?
Kind of that, does it work as you want ?
Last edited on
Topic archived. No new replies allowed.