为什么在我的 LinkedBinaryTree 实现中只添加了根
Why is only the root is being added in my LinkedBinaryTree implementation
这是我的代码。 LinkedBinaryTree 和 Position classes 来自教科书,如果需要,我可以提供这些代码,但我不确定是否有必要。这个 class 应该可以在二叉树中添加一个新节点,使得对于每个内部节点,存储在父节点左侧的元素小于父节点的元素,并且元素存储在父节点右边的比父节点大,所以它几乎应该创建一个二叉搜索树。问题是当我打印出结果树时(见下文)我只得到根元素。
import net.datastructures.LinkedBinaryTree;
import net.datastructures.Position;
import java.util.Iterator;
public class IntLinkedBinaryTree extends LinkedBinaryTree<Integer> {
// define instance variables and methods, including a constructor(s) as needed
private Position<Integer> root;
private int size;
/**
* Creates an empty IntLinkedBinaryTree
*/
public IntLinkedBinaryTree() {
root = null;
size = 0;
}
/**
* Add a new node with e to the tree rooted at position p
*
* @param p The root of the tree to which new node is added
* @param e The element of the new node
* @return If a node with e does not exist, a new node with e is added and
* reference to the node is returned. If a node with e exists, null is returned.
*/
public Position<Integer> add(Position<Integer> p, Integer e) {
if (p == null) {
root = addRoot(e);
size++;
return p;
}
Position<Integer> x = p;
Position<Integer> y = x;
while (x != null) {
if (x.getElement().equals(e)) {
return null;
} else if (x.getElement() > e) {
y = x;
x = left(x);
} else {
y = x;
x = right(x);
}
} // end while
Position<Integer> temp;
if (y.getElement() > e) {
temp = createNode(e, validate(y), null, null);
addLeft(temp, e);
} else {
temp = createNode(e, validate(y), null, null);
addRight(temp, e);
}
size++;
return temp;
}
public static void main(String[] args) {
// create a new binary tree instance
IntLinkedBinaryTree t = new IntLinkedBinaryTree();
// add some integers
t.add(t.root, 100);
t.add(t.root, 50);
t.add(t.root, 150);
t.add(t.root, 70);
// print all integers in the tree in increasing order
// after adding above four integers, the following should be printed
// 50 70 100 150
Iterator<Position<Integer>> it = t.inorder().iterator();
System.out.println();
while (it.hasNext()) {
System.out.print(it.next().getElement() + " ");
}
System.out.println();
}
}
我的输出只有 100,我快要抓狂了。 addRoot()、addLeft() 和 addRight()、inorder() 和 iterator() 方法由讲师在 LinkedBinaryTree class 中提供,整个 main 方法也是如此。当我使用 intelliJ 调试代码时,它调用了 add* 方法,但只有 100 次打印到控制台。
我将 addLeft() 和 addRight() 更改为 setLeft() 和 setRight()。它现在有效,但我不确定为什么,因为 addLeft/Right() 在超级 class...
中调用 setLeft/Right()
这是我的代码。 LinkedBinaryTree 和 Position classes 来自教科书,如果需要,我可以提供这些代码,但我不确定是否有必要。这个 class 应该可以在二叉树中添加一个新节点,使得对于每个内部节点,存储在父节点左侧的元素小于父节点的元素,并且元素存储在父节点右边的比父节点大,所以它几乎应该创建一个二叉搜索树。问题是当我打印出结果树时(见下文)我只得到根元素。
import net.datastructures.LinkedBinaryTree;
import net.datastructures.Position;
import java.util.Iterator;
public class IntLinkedBinaryTree extends LinkedBinaryTree<Integer> {
// define instance variables and methods, including a constructor(s) as needed
private Position<Integer> root;
private int size;
/**
* Creates an empty IntLinkedBinaryTree
*/
public IntLinkedBinaryTree() {
root = null;
size = 0;
}
/**
* Add a new node with e to the tree rooted at position p
*
* @param p The root of the tree to which new node is added
* @param e The element of the new node
* @return If a node with e does not exist, a new node with e is added and
* reference to the node is returned. If a node with e exists, null is returned.
*/
public Position<Integer> add(Position<Integer> p, Integer e) {
if (p == null) {
root = addRoot(e);
size++;
return p;
}
Position<Integer> x = p;
Position<Integer> y = x;
while (x != null) {
if (x.getElement().equals(e)) {
return null;
} else if (x.getElement() > e) {
y = x;
x = left(x);
} else {
y = x;
x = right(x);
}
} // end while
Position<Integer> temp;
if (y.getElement() > e) {
temp = createNode(e, validate(y), null, null);
addLeft(temp, e);
} else {
temp = createNode(e, validate(y), null, null);
addRight(temp, e);
}
size++;
return temp;
}
public static void main(String[] args) {
// create a new binary tree instance
IntLinkedBinaryTree t = new IntLinkedBinaryTree();
// add some integers
t.add(t.root, 100);
t.add(t.root, 50);
t.add(t.root, 150);
t.add(t.root, 70);
// print all integers in the tree in increasing order
// after adding above four integers, the following should be printed
// 50 70 100 150
Iterator<Position<Integer>> it = t.inorder().iterator();
System.out.println();
while (it.hasNext()) {
System.out.print(it.next().getElement() + " ");
}
System.out.println();
}
}
我的输出只有 100,我快要抓狂了。 addRoot()、addLeft() 和 addRight()、inorder() 和 iterator() 方法由讲师在 LinkedBinaryTree class 中提供,整个 main 方法也是如此。当我使用 intelliJ 调试代码时,它调用了 add* 方法,但只有 100 次打印到控制台。
我将 addLeft() 和 addRight() 更改为 setLeft() 和 setRight()。它现在有效,但我不确定为什么,因为 addLeft/Right() 在超级 class...
中调用 setLeft/Right()