2016-03-02 43 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