2016-03-02 78 views
0

我是新的JAVA,如果不明白我的插入方法有什麼問題。 頭節點不會更新,也不會顯示任何內容。使用插入方法後BST的頭不更新

public class BinarySearchTree { 
private Node head; 

/** 
* This is a default constructor for the root of the binary search tree 
*/ 
public BinarySearchTree() { 
    head = null; 
} 


public Node Insert(Node head, Node node) { 
    if (head == null) 
     head = node; 
    else if (node.data < head.data) 
     head.left = Insert(head.left, node); 
    else if (node.data > head.data) 
     head.right = Insert(head.right, node); 

    return head; 

如果我在構造函數head = new Node()上使用樹,但數據爲0的節點添加到我的樹中。

我該如何預防?

謝謝

編輯:

public class Node { 
int data; 
Node right; 
Node left; 
Node parent; 

/** 
* Constructor for the root in binary search tree 
*/ 
public Node() { 

} 

public Node(int data) { 
    this.data = data; 
    this.right = null; 
    this.left = null; 
    //this.parent = null; 
} 

public Node(Node obj){ 
    if(obj != null){ 
     this.data = obj.data; 
     this.left = obj.left; 
     this.right = obj.right; 
    } 
} 


public void setData(int data, Node right, Node left, Node parent) { 
    this.data = data; 
    this.right = right; 
    this.left = left; 
    //this.parent = parent; 
} 

public int getData() { 
    return data; 
} 

public class BinarySearchTree { 
private Node head; 

/** 
* This is a default constructor for the root of the binary search tree 
*/ 
public BinarySearchTree() { 
    head = new Node(); 
} 


public Node Insert(Node head, Node node) { 
    if (head == null) 
     head = node; 
    else if (node.data < head.data) 
     head.left = Insert(head.left, node); 
    else if (node.data > head.data) 
     head.right = Insert(head.right, node); 

    return head; 
} 

//////////////////////////////////////////////////////////////////////////////// 
public void printInOrder(Node node) 
{ 
    if (node != null) 
    { 
     printInOrder(node.left); 
     System.out.print(node.data + " - "); 
     printInOrder(node.right); 
    } 
} 

public void printPostOrder(Node node) 
{ 
    if (node != null) 
    { 
     printPostOrder(node.left); 
     printPostOrder(node.right); 
     System.out.print(node.data + " - "); 
    } 
} 

public void printPreOrder(Node node) 
{ 
    if (node != null) 
    { 
     System.out.print(node.data + " - "); 
     printPreOrder(node.left); 
     printPreOrder(node.right); 
    } 
} 

public Node getHead(){ 
    return head; 
} 
//////////////////////////////////////////////////////////////////////////////// 

public static void main(String[] args) 
{ 
    BinarySearchTree f = new BinarySearchTree(); 
    /** 
    * Insert 
    */ 
    f.Insert(f.head, new Node(20)); 
    f.Insert(f.head, new Node(5)); 
    f.Insert(f.head, new Node(25)); 
    f.Insert(f.head, new Node(3)); 
    f.Insert(f.head, new Node(7)); 
    f.Insert(f.head, new Node(27)); 
    f.Insert(f.head, new Node(27)); 

    /** 
    * Print 
    */ 
    f.printInOrder(f.head); 
    System.out.println(""); 
    f.printPostOrder(f.head); 
    System.out.println(""); 
    f.printPreOrder(f.head); 
    System.out.println(""); 
} 
+0

可以喲你請發佈整個代碼?與節點類一起?另外插入應根據約定 – JeD

+0

寫在小寫字母感謝您的答覆。我編輯 – Silvering

回答

0

的問題是,你的函數插入有一個名爲頭輸入

這意味着在funtion頭不是類的頭,但傳遞的價值。和Java它是通過按值和良好...

試試這個,而不是你插入:

private Node recursiveInsert(Node head, Node node) { 
    if (head == null) 
     head = node; 
    else if (node.data < head.data) 
     head.left = recursiveInsert(head.left, node); 
    else if (node.data > head.data) 
     head.right = recursiveInsert(head.right, node); 

    return head; 
} 

public Node insert(Node node){ 
    if(this.head==null){ 
     this.head=node; 
    }else{ 
     recursiveInsert(this.head,node); 
    } 
    return this.head; 
} 

,並更改來電

f.Insert(f.head, new Node(20)); 
f.Insert(f.head, new Node(5)); 
f.Insert(f.head, new Node(25)); 
f.Insert(f.head, new Node(3)); 
f.Insert(f.head, new Node(7)); 
f.Insert(f.head, new Node(27)); 
f.Insert(f.head, new Node(27)); 

f.insert(new Node(20)); 
f.insert(new Node(5)); 
f.insert(new Node(25)); 
f.insert(new Node(3)); 

告訴我,如果它的工作;)

+0

謝謝你的回覆。這是工作,但我仍然獲得數據0的附加節點,雖然我沒有將0節點插入到樹中。 – Silvering

+0

它工作,如果你改變構造函數使頭= null – JeD

+0

你說得對。感謝您的幫助 – Silvering