我正在研究二叉搜索樹。我有一個前一個節點和一個節點。前一個節點位于該節點之前。我需要幫助分配前一個節點。這是我的代碼塊:private BSTNode<E> add(BSTNode<E> node, E value, BSTNode<E> parent, BSTNode<E> prev){ if (node == null) { node = new BSTNode<E>(value); node.parent = parent; //issue takes place here. node.next = node; node = prev; this.numElements++; } else if (node.data.compareTo(value) > 0) { node.left = add(node.left, value, node , getPrevNode(node)); } else if (node.data.compareTo(value) < 0) { node.right = add(node.right, value, node, node.parent); } return node;}在這個類里面public class BinarySearchTree<E extends Comparable<E>>{private BSTNode<E> root; // root of overall treeprivate int numElements;private BSTNode<E> first;// post: constructs an empty search treepublic BinarySearchTree(){ this.root = null; this.numElements = 0;}private static class BSTNode<E>{ public E data; public BSTNode<E> left; public BSTNode<E> right; public BSTNode<E> parent; public BSTNode<E> next; public BSTNode(E data) { this(data, null, null, null, null); } public BSTNode(E data, BSTNode<E> left, BSTNode<E> right, BSTNode<E> parent, BSTNode<E> next) { this.data = data; this.left = left; this.right = right; this.parent = parent; this.next = next; } }}我會嘗試使用遞歸來解決這個問題,但是放棄這些想法,因為我不確定如何解決這個問題。我已經嘗試了幾種方法,但都沒有奏效。
1 回答

慕工程0101907
TA貢獻1887條經驗 獲得超5個贊
找到了我要找的答案,就是這個
if(prev == null)
{
node.next = parent;
}
else
{
node.next = prev.next;
prev.next = node;
}
添加回答
舉報
0/150
提交
取消