问题:将二叉搜索树应用于“person”对象而不是 int 值。人物对象由名称和重量组成。树将组织和搜索的person对象的值是一个人的“名称”(字符串)。
这是(叶子)树节点:
public class Node {
private Node leftChild;
private Node rightChild;
private Node parent;
private Person person;
private int height;
public Node(Node p, Node l, Node r, Person paul, int h) {
leftChild = l;
rightChild = r;
person = paul;
height = h;
parent = p;
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}
这是BinarySearchTree:
public class BinarySearchTree {
private Node root;
public BinarySearchTree(Node root) {
this.root = root;
}
public BinarySearchTree() {
this.root = (Node)null;
}
void setRoot(Node r) {
root = r;
}
Node getRoot() {
return root;
}
public Node findParent(Person person, Node node) {
if (node.getPerson() == person)//Error in code here
return (Node)null; // root itself
else if(node.getLeftChild() != null && node.getLeftChild().getPerson() == person)
return node;
else if(node.getRightChild() != null && node.getRightChild().getPerson() == person)
return node;
else if (node.getPerson().getName().compareTo(" a ") > 0 ){
return findParent(person, node.getLeftChild());}
else
return findParent(person, node.getRightChild());
}
public Node insertNode(Person person) {
return insertNode(person, null, null);
}
public Node insertNode(Person person, Node node, Node parent) {
if(node == (Node)null)
node = new Node(parent, null, null, person, 0);
else if (person.getName().compareTo(node.getPerson().getName()) < 0)
node.setLeftChild(insertNode(person,node.getLeftChild(), node));
else if (person.getName().compareTo(node.getPerson().getName()) > 0)
node.setRightChild(insertNode(person, node.getRightChild(), node));
node.setHeight();
return node;
}
/* in-order traversal for showing inside of tree */
public void traverseInOrder(Node node) {
if(node.getLeftChild() != (Node)null)
traverseInOrder(node.getLeftChild());
System.out.print("Value: " + '\n' + node.getPerson().toString() + ", Height: " +
node.getHeight() + ", Parent: ");
Node n = findParent(node.getPerson(), getRoot());
if(n == (Node)null)
System.out.println("root");
else
System.out.println(n.getPerson().toString() + "");
if(node.getRightChild()!= (Node)null)
traverseInOrder(node.getRightChild());
}
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
//or is the parent supposed to be a null pointer ????
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person Person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}
答案 0 :(得分:0)
将String.compareTo()
与人名作为参数。
答案 1 :(得分:0)
问题在于
if (node.getPerson() == person)
测试这些对象在内存中是否具有相同的地址,而不是它们具有相同的“逻辑”值。您应该替换为:
if (node.getPerson().getName().equals(person.getName()))