2011-11-14 4 views
1

문제 : 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; 
    } 

} 

답변

1

문제는 이러한 개체 경우

if (node.getPerson() == person) 

테스트들이 동일한 "논리"값이없는 경우, 메모리에 동일한 주소를 가지고있다. 다음으로 대체해야합니다 :

if (node.getPerson().getName().equals(person.getName())) 
+0

감사합니다. 도움에 정말 감사드립니다. – 09182746

+0

문제를 해결 한 경우 승인 된 답변을 선택하고 upvote하는 것을 잊지 마십시오. :) – Tudor

+0

그래도 허용되는 답변을 선택할 수 있습니다. – Tudor

0

사용 String.compareTo() 매개 변수와 같은 사람 이름 '과 여기에

은 (잎) 트리 노드입니다.

+0

흠 ... 이렇게 잘 작동하지 않았습니다. 비교할 문자열을 알아낼 수 없습니까? 생성되는 객체 중 하나 ... 그리고 이미 생성 된 문자열의 객체 참조는 무엇입니까? 뿌리? – 09182746

+0

'Person.name'을 BST의 키로 사용하고 있으므로 입력 된 정수 값과 현재 노드의 정수 값을 비교하는 대신 입력 된 사람의 이름과 현재 노드의 사람 이름을 비교하려고합니다. – skyuzo

+0

여기서는 입력 된 사람의 이름이고 현재 노드의 사람 이름은 어디에 있습니까 ???? 내 클라이언트 클래스를 보여줄 것입니다 ... 사람들은 이름과 무게를 포함하는 텍스트 파일로부터 생성됩니다 ... – 09182746