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