简体   繁体   English

在JAVA中更新二进制搜索树中的父节点

[英]Updating parent node in Binary search tree in JAVA

I am working on Binary search tree and I am having some problems in updating/adding the parent node of current node.Parent node are not getting updated correctly.This is what Iam trying to do: 我正在二叉搜索树上工作,并且在更新/添加当前节点的父节点时遇到一些问题。父节点未正确更新。这是Iam试图做的:

public class binarytree {

    private Node root;

static class Node{

    Node left;
    Node right;
    int value;
    Node parent;

    public Node(Node parent,int value)
        {
        this.value = value;
        this.left = null;
        this.right = null;
        this.parent = parent;
        }
    }

public void creatBST()
    {
    root = new Node(null,4);
    System.out.println("Binary Search tree with root = "+ root.value);
    insert(root,1);
    insert(root,2);
    insert(root,3);
    insert(root,6);
    insert(root,5);
    insert(root,7);
}

public void insert(Node node, int value)
    {
    Node prevnode = node;
    if(value < node.value)
        {
        if(node.left != null)
            {


            insert(node.left, value);   
            System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);
            }
        else
            {

            node.left = new Node(prevnode,value);
            }
        }
    else if(value > node.value)
        {
        if(node.right != null)
            {
            //prevnode = node;

            insert(node.right, value);
            System.out.println("Parent node of node"+ node.right.value+"is:"+prevnode.value);
            }
        else
            {

            node.right = new Node(prevnode,value);
            }
        }
    }
public static void main(String args [])
    {
    binarytree obj = new binarytree();
    obj.creatBST();
    //obj.printInOrder();
    //obj.check();
    }
}

The output which I am getting is: 我得到的输出是:

    Binary Search tree with root = 4
Parent node of node1is:4
Parent node of node2is:1
Parent node of node1is:4
Parent node of node6is:4
Parent node of node6is:4

which is not the correct output. 这不是正确的输出。

Need help me in determining how can I do it correctly. 需要帮助我确定如何正确执行操作。

It looks like the insertion of nodes is working properly. 看来插入节点工作正常。 The print statements are wrong because they are called after the recursive call to insert the next Node. 打印语句是错误的,因为在递归调用之后将其插入以插入下一个Node。 For example, after you do insert(root,1) your function works and you have the following structure: 例如,在执行insert(root,1)之后,您的函数将起作用,并且具有以下结构:

          4
         /
        1

Now when you call insert(root, 2) you end up at this point in the code: 现在,当您调用insert(root,2)时,您将在代码中的这一点结束:

if(node.left != null)
        {


        insert(node.left, value);   
        System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);
        }

insert(node.left, value) is called and then the print statement happens. 调用insert(node.left,value),然后执行print语句。 So while your new tree looks like this: 因此,当您的新树看起来像这样:

          4
         /
        1
         \
          2

The next print statement 下一个打印语句

        System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);

will print out node.left.value of 1 and prevnode.value of 4. You may want to change where your print statements are or write a new method that prints the nodes and parent nodes. 将打印出node.left.value为1和prevnode.value为4。您可能想要更改打印语句的位置,或编写一个新方法来打印节点和父节点。

Edit: I would run this method in your main method to test the tree structure: 编辑:我将在您的主要方法中运行此方法以测试树结构:

    public void printParents(Node n){
    if(n.left!= null){
        printParents(n.left);
    }
    if(n.right!=null){
        printParents(n.right);
    }
    if(n.parent != null){
        System.out.println("Parent of " + n.value + " is " + n.parent.value);
    }
    else{
        System.out.println("The root node is" + n.value);
    }
}

I called it here: 我在这里叫它:

public static void main(String args [])
{
binarytree obj = new binarytree();
obj.creatBST();
Node n = obj.root;
obj.printParents(n);
//obj.printInOrder();
//obj.check();
}

For starters, you might want to fix the insert method: 对于初学者,您可能需要修复insert方法:

Node currentNode;
public void setCurrentNode(Node node) {
    currentNode = node;
}

public boolean insert(int value) {
    //You should probably insert from an instance variable, not an explicit parameter
    Node n = new Node(currentNode, value);
    if (currentNode.left == null) currentNode.left = n;
    else if (currentNode.right == null) currentNode.right = n;
    else return false;    //If insert failed because currentNode was full
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM