2015-11-08 88 views
1

所以我想實現BST(二叉搜索樹)我做了這增加了樹節點到樹[樹節點]數組我二傳手不設置

這裏的add方法就是我想要的樹節點類設置父節點以及左右節點,我使用調試器進行了檢查,但我不確定爲什麼,但是它沒有設置父變量,並且它只在leftChild和rightChild字段中添加一個或另一個。

有問題的二傳手是這一個

//set parent 
public void setParent(TreeNode t) 
{ 
    this.parent = t.parent; 
} 

當我從它並不正確設置PAS43DEPQ類調用它,我無法理解。

class TreeNode implements Comparable<TreeNode> 
{ 
    private Integer value; 
    private TreeNode leftChild; 
    private TreeNode rightChild; 
    private TreeNode parent; 

    //constructors 
    public TreeNode(){} 
    public TreeNode(Integer v){this.value = v;} 
    public TreeNode(TreeNode t){ 
     this.value = t.value; 
     this.parent = t.parent; 
     this.leftChild = t.leftChild; 
     this.rightChild = t.rightChild; 
    } 
    public TreeNode (Comparable c){this.value = (int) c;} 

    //set parent 
    public void setParent(TreeNode t) 
    { 
     this.parent = t.parent; 
    } 
    //get parent 
    public TreeNode getParent() 
    { 
     return this.parent; 
    } 
    //get value 
    public int getValue(){return value;} 
    //set value 
    public void setValue(Integer i){ this.value = i;} 
    //get left node 
    public TreeNode getLeftChild(){return leftChild;} 
    //get right node 
    public TreeNode getRightChild(){return rightChild;} 
    //set left child 
    public void setLeftChild(TreeNode t) {this.leftChild = t;} 
    //set right child 
    public void setRightChild(TreeNode t) {this.rightChild = t;} 

    public TreeNode find(int n) 
    { 
     //this statement runs if the current node is == the value being searched. 
     if(this.value == n) 
      return this; 
     //this returns values left of the root then performs a recursive call if not found 
     if(value < this.value && leftChild != null) 
      return leftChild.find(n); 
     //this does the same as above except looks on the right side of the root 
     if(rightChild != null) 
      return rightChild.find(n); 

     //this returns if value is not found 
     return null; 
    } 

    @Override 
    public int compareTo(TreeNode o) 
    { 

     if (this.value == o.value) 
     { 
      return 0;// if value equal 
     } 
     if (this.value > o.value) //if value greater 
     { 
      return 1; 
     } 
     if (this.value < o.value) 
     { 
      return -1; //if value less 
     } 
     return 99; 
    } 
} 

這裏是一流的,我從補充:

public class PAS43DEPQ implements DEPQ 
{ 
    private TreeNode[] tree = new TreeNode[100]; 
    int index = 0; 

    @Override 
    public Comparable inspectLeast() { 
     return null; 
    } 

    @Override 
    public Comparable inspectMost() { 
     return null; 
    } 

    /* 
    right: (2 * n) + 2 
    left: (2 * n) + 1 
    parent: (1 - n)/2 
    */ 

    public int right() 
    { 
     return (2 * index) + 2; 
    } 

    public int left() 
    { 
     return (2 * index) + 1; 
    } 

    public int parent() 
    { 
     return Math.round((index - 1)/2); 
    } 

    @Override 
    public void add(Comparable c) 
    { 
     // Root node 
     if (tree[0] == null) { 
      tree[0] = new TreeNode(c); 
      return; 
     } 

     //this while loop is for tree traversal 
     while(tree[index] != null) { 
      if(c.compareTo(tree[index].getValue()) == 0) { 
       index += right() - index; 
       continue; 
      } 

      if(c.compareTo(tree[index].getValue()) > 0) { 

       index += right() - index; 
       continue; 
      } 

      if(c.compareTo(tree[index].getValue()) < 0) { 
       index += left() - index; 
       continue; 
      } 

     } 

     //this part is for place the new node 
     if(tree[index] == null) { 
      tree[index] = new TreeNode(c); 
      tree[index].setParent(tree[parent()]); 

      if(c.compareTo(tree[index].getValue()) == 0) 
       tree[parent()].setRightChild(tree[index]); 

      if(c.compareTo(tree[index].getValue()) > 0) 
       tree[parent()].setRightChild(tree[index]); 

      if(c.compareTo(tree[index].getValue()) < 0) 
       tree[parent()].setLeftChild(tree[index]); 


      index = 0; 
     } 

     return; 
    } 

    @Override 
    public Comparable getLeast() { 
     return null; 
    } 

    @Override 
    public Comparable getMost() { 
     return null; 
    } 

    @Override 
    public boolean isEmpty() { 
     return (tree[0] == null) ? true : false; 
    } 

    @Override 
    public int size() { 
     return tree.length; 
    } 
} 

我不能縫製定出爲什麼父母心不是被設置的行 "tree[index].setParent(tree[parent()])"

被稱爲?任何ide爲什麼發生這種情況?

+0

我會期望'this.parent = t;'而不是'this.parent = t.parent;'...... D'oh? –

回答

1

set方法應該是這樣的

//set parent 
public void setParent(TreeNode t) 
{ 
    this.parent = t; 
} 

這種方法將使TreeNode噸由this引用當前節點的父。

您正在使用的聲明將TreeNode t的父項設置爲當前節點的父項。