之前我问过这个问题,但我仍然无法在CompleteTree类中修复insert()方法。我试图创建一个完整的二叉树(不是二叉搜索树 - 树节点内的值并不重要)。以下是:
public static void main(String[] args) {
CompleteTree tree = new CompleteTree();
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
tree.insert(6);
tree.inorder();
}
应该创建一个如下所示的树:
1
/ \
2 3
/ \ /
4 5 6
它应打印出4 2 5 1 6 3
起初我认为我的代码有效,但我很快意识到存在问题。我的插入最多可以达到11次。但是,当我尝试插入12时,它会成为8而不是6的左孩子。
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \
8 9 10 11
// Code works fine up to this point
在查看我的代码之后,我可以看到为什么会在那里插入12个,我可以看到我的代码存在问题,但我不知道如何修复它以便以正确的顺序插入节点。任何帮助将不胜感激!
TreeNode.java
public class TreeNode {
private Object value;
private TreeNode left, right;
public TreeNode(Object value) {
this.value = value;
this.left = null;
this.right = null;
}
public TreeNode(Object value, TreeNode left, TreeNode right) {
this.value = value;
this.left = left;
this.right = right;
}
public void setValue(Object theNewValue) {
value = theNewValue;
}
public void setLeft(TreeNode theNewLeft) {
left = theNewLeft;
}
public void setRight(TreeNode theNewRight) {
right = theNewRight;
}
public Object getValue() {
return this.value;
}
public TreeNode getLeft() {
return this.left;
}
public TreeNode getRight() {
return this.right;
}
}
BinaryTree.java:
public abstract class BinaryTree {
private TreeNode root;
public BinaryTree() {
this.root = null;
}
public void setRoot(TreeNode newNode) {
this.root = newNode;
}
public TreeNode getRoot() {
return this.root;
}
public boolean isEmpty() {
return (this.root == null);
}
public abstract void insert(Comparable item);
public abstract TreeNode find(Comparable key);
} // end of class
CompleteTree.java:
public class CompleteTree extends BinaryTree {
public CompleteTree() {
super();
}
public void insert(Comparable item) {
if(this.getRoot() == null) {
this.setRoot(new TreeNode(item));
return;
}
TreeNode t = this.getRoot();
while(t != null) {
if(t.getLeft() == null) {
t.setLeft(new TreeNode(item));
return;
}
else if(t.getRight() == null) {
t.setRight(new TreeNode(item));
return;
}
else {
int lCount = countChildren(t.getLeft());
int rCount = countChildren(t.getRight());
if(lCount == rCount)
t = t.getLeft();
else if(lCount == 0 || lCount == 1)
t = t.getLeft();
else
t = t.getRight();
}
}
}
public int countChildren(TreeNode t) {
int count = 0;
if(t.getLeft() != null)
count++;
if(t.getRight() != null)
count++;
return count;
}
public TreeNode find(Comparable key) {
TreeNode p = this.getRoot();
while(p != null && key.compareTo(p.getValue()) != 0) {
if(key.compareTo(p.getValue()) < 0)
p = p.getLeft();
else
p = p.getRight();
}
return p;
}
public BinaryTree swapSubtrees() {
doSwap(this.getRoot());
return this;
}
private void doSwap(TreeNode p) {
if(p != null) {
TreeNode temp = p.getLeft();
p.setLeft(p.getRight());
p.setRight(temp);
doSwap(p.getLeft());
doSwap(p.getRight());
}
}
public void inorder() {
doInorderTraversal(this.getRoot());
}
private void doInorderTraversal(TreeNode t) {
if(t != null) {
doInorderTraversal(t.getLeft());
System.out.print(t.getValue() + " ");
doInorderTraversal(t.getRight());
}
}
}
CompleteTreeDemo.java:
public class CompleteTreeDemo {
public static void main(String[] args) {
CompleteTree tree = new CompleteTree();
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
tree.insert(6);
tree.insert(7);
tree.insert(8);
tree.insert(9);
tree.insert(10);
tree.insert(11);
tree.insert(12); // This gets inserted into the wrong place
tree.inorder();
}
}