简体   繁体   English

Java二进制搜索树找到父级

[英]java binary search tree find parent

im working on a method to find the parent of anode. 我正在研究寻找阳极母体的方法。 I start at the root and then go down the leaves as long as they are not null and not the node of the child. 我从根开始,然后沿着叶子走,只要它们不为空且不是子节点。

below is my code, its a little messy because im trying to test it to see whats going wrong. 下面是我的代码,有点混乱,因为我正在尝试对其进行测试以查看出现了什么问题。

The tree that i have is 我有的树是

        10
      /    \
     2     20
      \   / \
       3 18 22
            /
           21

The x that is being passed in is 20 so 10 is the parent but when i run it 22 comes out as the parent. 被传入的x是20,所以10是父级,但是当我运行它时,22成为父级。 the while loop seems to not be working, is it the way ive written it? while循环似乎不起作用,是我写的方式吗?

public Node<E> findParent(E x)
{
Node<E> node = root;

System.out.println("node is " + node.getData() + " before the search");
System.out.println("The value of x is " + x);
System.out.println("The value of node.getRight is " + node.getRight().getData());
boolean test = !node.getRight().getData().equals(x);
System.out.println("does nodes data equal x " + test);
while(((node!=null) && (node.getLeft()!=null) && (!node.getLeft().getData().equals(x))) || 
 ((node != null) && (node.getRight()!=null) && (!node.getRight().getData().equals(x))))
{ System.out.println("why didnt it stop");
    if(x.compareTo(node.getData()) < 0)
    {
        node = node.getLeft();
    }
    else
    {
        node = node.getRight();
    }
}
 System.out.println("node is " + node.getData() + " after the search");
return node;
}

I would do it differently: do the recursion in an auxiliary method that is passed the current node and the current parent node. 我会做不同的事情:在传递当前节点和当前父节点的辅助方法中进行递归。 It makes everything much simpler: 它使一切变得更加简单:

public Node<E> findParent(E x) {
    return findParent(x, root, null);
}

public Node<E> findParent(E x, Node<E> node, Node<E> parent)
{
    if (node == null) {
        return null;
    } else if (!node.getData().equals(x)) {
        parent = findParent(x, node.getLeft(), node);
        if (parent == null) {
            parent = findParent(x, node.getRight(), node);
        }
    }
    return parent;
}
private static void myparent(int data, Node R) 
{
    if( (R.left!=null && R.left.data == data) || (R.right!=null) &&(R.right.data == data) )
    {
        pRoot = R;
        return;
    }
    if (R.data <= data)
        myparent(data, R.right);
    else
        myparent(data, R.left);
}

Where "Data" is the value of the node whose parent we need to search and R is the root node of the BST. 其中“数据”是我们需要搜索其父节点的节点的值,R是BST的根节点。 pRoot is my global data structure as I used it at other operations on BST. pRoot是我在BST上的其他操作中使用的全局数据结构。

This is just pseudocode. 这只是伪代码。 when you are at a node x , check for the key (of child whose parent is to be found) in the left and right node. 当您在节点x ,请检查左右节点中的(要找到其父项的子项)键。 If they match, then you are already at the parent. 如果它们匹配,那么您已经在父级了。 If not then it's safe to move in the direction of the element and perform again. 如果不是,那么沿元素方向移动并再次执行是安全的。

Note that we check in the next level before proceeding. 请注意,在继续之前,我们将检入下一级。

while(node!=null){

  if(node.left==key || node.right==key) return node;

  if(key<node.data) node=node.left;

  else node=node.right;

  return null;
}

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

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