Domanda

I am trying to create a binary tree and traverse it.
Problem is that, CreateBT() makes the tree correctly but TreeTraverse(BTNode node) only print root of the tree and its 2 child no matter which ever traversal i use.

I even checked nodes and its child using ( commented in the code )

//btn is binaryTreeNode
System.out.println("btn : "+btn.getData()+" - right : "+btn.getRight()+" - left "+btn.getLeft());

and output is as expected :

btn : 0 - right : Data_S.BTNode@1ba0281 - left Data_S.BTNode@fa6824
btn : 1 - right : Data_S.BTNode@1cb1d37 - left Data_S.BTNode@1eb424b
btn : 2 - right : Data_S.BTNode@4d8040 - left Data_S.BTNode@1f14fd
btn : 3 - right : null - left Data_S.BTNode@9b04ac
btn : 4 - right : null - left null
btn : 5 - right : null - left null
btn : 6 - right : null - left null

I am really messed up in finding the error, can someone help me please??

Code

void CreateBT()
    {
        int nodeCnt = 0;
        BTNode leftN, rightN;
        while(!BTq.isQueueEmpty())
        {
            BTNode btn = new BTNode(BTq.deQueue());

            if(root == null)
                root = btn;

            int leftNodeIndex = getLeftIndex(nodeCnt);
            int rightNodeIndex = getRightIndex(nodeCnt++);

            if(leftNodeIndex >= BTq.queueArray.length)
                btn.setLeft(null);
            else
            {
                leftN = new BTNode(BTq.queueArray[leftNodeIndex]);
                btn.setLeft(leftN);
            }


            if(rightNodeIndex >= BTq.queueArray.length)
                btn.setRight(null);
            else
            {
                    rightN = new BTNode(BTq.queueArray[rightNodeIndex]);
                    btn.setRight(rightN);
            }

        //System.out.println("btn : "+btn.getData()+" - right : "+btn.getRight()+" - left "+btn.getLeft());
        }

        TreeTraverse(root);
    }


public void TreeTraverse(BTNode node)
    {
        if(node == null)
            return;
            TreeTraverse(node.left);
    System.out.println(node.getData());
    TreeTraverse(node.right);

    }

Output

1
0
2

Input Array data

0 1 2 3 4 5 6 7
È stato utile?

Soluzione

When you set the children of a node, as in your code

leftN = new BTNode(BTq.queueArray[leftNodeIndex]);
btn.setLeft(leftN);

you create a node and in the next while loop, you create another node by
BTNode btn = new BTNode(BTq.deQueue());

and these two nodes are different while they are supposed to be the same, so you may set the children of root right, but in the next loop, you add the children to the node you create which is btn instead of the one you create in the previous loop which is lefttN.

Update:
if you print the node and its children, like System.out.println("btn : "+btn+" - right : "+btn.getRight()+" - left "+btn.getLeft()); you may find that root's left node is different from what you print in the second line.

The problem is that you create a node a(0) for example, and set its children b(1) and c(2), and in the next loop you create a node d(1) and set its children .... while b is the left child of a, and d should also be the left child of a, but b and d are different, you set the children of d instead of b, so b doesn't have children, and when you call TreeTraverse , it will stop at the second layer.

What you need to do is to set the children of b instead of d

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top