public static BiNode linklist(BiNode root)
{
BiNode head = null, tail=null;
convertBST(head, tail, root);
return head;
}
public static void convertBST(BiNode head, BiNode tail, BiNode root)
{
BiNode leftTail = null, rightHead = null;
if(root==null){
head = null;
tail = null;
return;
}
System.out.println("root = "+root.key);
convertBST(head, leftTail, root.node1);
convertBST(rightHead, tail, root.node2);
if(leftTail != null)
{
System.out.println("leftTail = "+leftTail.key);
leftTail.node2 = root;
root.node1 = leftTail;
}else{
head = root;
System.out.println("head = "+ head.key+", root = "+root.key);
}
if(rightHead != null)
{
rightHead.node1 = root;
root.node2 = rightHead;
}else{
tail = root;
System.out.println("tail = "+ tail.key+", root = "+root.key);
}
}
above is my java code which is used to convert a BST to a double link list.
But I do not know why the head always change, which is supposed to point to the head of the link list and not change.
I am glad great mind would help me debug this code! thanks!!!
linklist()andconvertBST()? None of the other code appears to contribute to your BST to linked list logic, so it just clutters everything up. It'll also help a bit if everything is indented neatly.