문제 : int 값이 아닌 "사람"개체에 이진 검색 트리를 적용합니다. person 객체는 이름과 가중치로 구성됩니다. 트리가 구성하고 검색 할 사람 오브젝트의 값은 사람의 "이름"(문자열)입니다.BST 데이터 구조 - 클래스 프로젝트 - 중첩 클래스에 액세스
다음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;
}
}
감사합니다. 도움에 정말 감사드립니다. – 09182746
문제를 해결 한 경우 승인 된 답변을 선택하고 upvote하는 것을 잊지 마십시오. :) – Tudor
그래도 허용되는 답변을 선택할 수 있습니다. – Tudor