使用Insertion方法后,BST的负责人不会更新

时间:2016-03-02 22:24:01

标签: java

我是JAVA的新手,如果无法理解我的Insert方法有什么问题。 头节点永远不会更新,也不会显示任何内容。

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()上使用,我得到树,但是data = 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("");
}

1 个答案:

答案 0 :(得分:0)

问题是你的函数Insert有一个名为head

的输入

这意味着在函数头不是类的头部,而是传递的值。并且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));

告诉我它是否有效;)