tree traversal

this code does not work properly, please help
It was able to print out only 2 levels
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
template <typename T>
int BST<T>::nonrecursive_level(const T& item)
{
   std::queue<BSTNode*> q1;
    BSTNode *temp = myRoot;
    BSTNode *frontQueue = NULL;
    int level = -1;
    if (temp == NULL)
        return -1;
    q1.push(temp);
    while (! q1.empty())
    {
        frontQueue = q1.front();
        T element = q1.front()->data;
        cout << "elements " << element << "\n";
        q1.pop();
        if (element == item)
        {
           element++;
           break;
        }
        if (temp->left != NULL)
        {
           q1.push(temp->left);
        }
        if (temp->right != NULL)
        {
            q1.push(temp->right);
        }
    }
    return level;
}
Last edited on
I'm sorry, I don't want to read the code that way
please use the code-tags <> and make indentions
Code tages are [ code ] and [ /code ] without the spaces. First one at the start of the entire code, second at the end.
gamer2015. thank you
can you see what i did wrong here now
I guess BST is for binary sarch tree...

your code should either only print once/twice or never stop because you don't modify temp so temp is allways the root node and you allways check for temp->left and temp->right

you also allways return the value -1 for some reason
Last edited on
When you push it in the queue, you have updated the temp
Topic archived. No new replies allowed.