0

How to print path of a binary tree after adding every node (branches of a tree have values 0 and 1)? For example, I have this binary tree:

NODE1 has two children, NODE2 and NODE3. Branch from NODE1 to NODE2 is 0, and from NODE1 to NODE3 is 1.
From NODE2 to NODE4 is 1.
From NODE4 to NODE5 is 0.

How to print a full path 0-1-1-0.

I have written this function (don't have a complete code, so can't compile it)

void pathToNode(QUEUE* queue, NODE* node, NODE* root, int* found)
{
    if(root == NULL)
    {
        return;
    }
    if(root == node)
    {
        *found = 1;
        return;
    }
    inputElementInQueue(queue, 0);
    pathToNode(queue, node, root -> left, found);

    if(*found == 0)
    {
       returnFirstElementOfQueue(queue);
       inputElementInQueue(queue, 1);
       pathToNode(queue, node, root -> right, found);
    }

    if(*found == 0)
    {
        returnFirstElementOfQueue(queue);
    }
}

Is this right?

Thanks for replies.

2
Contributors
1
Reply
9
Views
1 Year
Discussion Span
Last Post by rubberman
0

You aren't providing enough information. In any case, why are you conflating queues with a binary tree? A binary tree is only equivalent to a queue when the tree is degenerate. IE, there are no branches, which can happen if you insert data that is already sorted, which is why we have B+, AVL, and Black/White trees, to rebalance the tree automatically on insertion and removal.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.