BST Number route

Hey guys, for my project i had to make a BST that prints the route of a number. for example if i have 1,3,6,2,5,8,9 and i want it to show me the route it passes to get to 8. (which would be 1,3,6,8). i have a code here but it doesnt print out anything at all, so i dont even know if i got it right...

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
void route( node *root, int thing )
{
    node *fake=root;
    bool founded=false;
    
    if (fake == NULL)
    {
            cout<<"the BST is empty!\n";
    }
    else
    {
        while (founded!=false)
        {
            if (fake == NULL)
            {
                cout<<"Number not found!";
                break;  
            }
            else if ( thing == fake->numero )
            {
                cout<<fake->numero;
                founded=true;
            }
            else if ( thing < fake->numero )
            {
                cout<<fake->numero <<" ";
                fake = fake->left;
            }
            else
            {
                cout<<fake->numero <<" ";
                fake = fake->right;
            }
        
        }
    }
    
}
Are you sure your tree is built? I don't immediately see a reason why the printing would be wrong, so I'm guessing it's the underlying code. I'd check with a debugger if I were you.

(P.S.: How can you be sure that it'll be 1->3->6->8?)
Topic archived. No new replies allowed.