我有一個(gè) binarySearch 樹,我想創(chuàng)建一個(gè)方法 assignFirst。此方法應(yīng)在樹中找到具有最小值的節(jié)點(diǎn),并相應(yīng)地更新樹的“第一個(gè)”屬性。我有很多方法,但我不想將所有方法都包含在這里,因?yàn)槲蚁氡3趾喍毯秃唵?。因此,我將在該類中包含該類和一些功能。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 void assignFirst(){ if (root.left == null) { first.data = root.data; } else { first.data = root.left.data; }}public class Iterator{ private BSTNode<E> currentNode; public Iterator() { currentNode = first; } public boolean hasNext() { return currentNode != null; } public E next() { E value = currentNode.data; currentNode = currentNode.next; return value; }}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; }}}我更新了我的方法看起來像這樣。我仍然不確定這是否是正確的做法。private void assignFirst(){ if (first.left != null) { first = first.left; } else { first = root; }}
1 回答

不負(fù)相思意
TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
我想到了。我是這樣寫的。
private void assignFirst()
{
BSTNode<E> node = root;
while(node.left != null)
{
node = node.left;
}
first = node;
}
添加回答
舉報(bào)
0/150
提交
取消