简体   繁体   English

在Java中查找二进制搜索树的前任和后任

[英]Find predecessor and successor of a binary search tree in java

My project wants me to print out the predecessor and the successor of a binary search tree. 我的项目要我打印出二进制搜索树的前身和后继。 The assignment requires me to pass in the data of a node as an argument. 分配要求我传入节点的数据作为参数。 When I try to print out the predecessor of a given node's data, it gives me 0. I try tirelessly how to solve the problem and found no where. 当我尝试打印给定节点数据的前身时,它的值为0。我孜孜不倦地尝试如何解决问题,却找不到任何地方。 Hope you can find where the problem lies. 希望您能找到问题所在。

Here is the method: 方法如下:

  public void findPredecessor(Node root, int data)
  {
    int predecessor = 0;
    if(root != null)
    {
        if(root.data == data)
        {
            if(root.left != null)
            {
                Node n = root.left;
                while(n.right != null) n = n.right;
                predecessor=  n.data;
            }
        }
        else if(root.data < data)
        {
            predecessor = root.data;
            findPredecessor(root.left, data);
        }
    }
    System.out.print(predecessor);
}

public void printPredecessor(int data)
{
    findPredecessor(root, data);
}

Here's the pseudo-code for the right approach as i think of it: 这是我认为正确的方法的伪代码:

Input: root node,key | output: predecessor node, successor node 
  • If root is NULL then return 如果root为NULL,则返回

  • if key is found then 如果找到钥匙,那么

     a. If its left subtree is not null Then predecessor will be the right most child of left subtree or left child itself ie maximum value in left subtree b. If its right subtree is not null Then successor will be the lefmost child of right subtree or right child itself ie minimum value in right subtree. 

    return 返回

  • If key is smaller then root node set the successor as root 如果 key较小,则根节点将后继者设置为root
    and search recursively into left subtree 然后递归搜索到左子树

    else set the predecessor as root and search recursively into right subtree 否则 将前任设置为root并递归搜索到正确的子树

Edit: If successor or predecessor returns null print null. 编辑:如果后继或前任返回null,则输出null。

Example

                         (1)<--root
                            \
                              \
                               (3)
                               /
                              /
                           (2)<--targetKey
Initially,
 successor=null
 predecessor=null

 FunctionCall(root,successor,predecessor,targetKey):

         Here root's key is smaller than target key so,
         Set predecessor=root.

                          (1)   <--predecessor
                            \
                              \
                               (3)
                               /
                              /
                           (2)<--targetKey


         //Search recursively in right subtree
     FunctionCall(root.right,successor,predecessor,targetKey):

              Here root's key is greater than target key so,
         Set successor=root.
                          (1) <--predecessor
                            \
                              \
                               (3) <--successor
                               /
                              /
                           (2)<--targetKey

           //Search recursively in left subtree
     FunctionCall(root.left,successor,predecessor,targetKey):

               Root.key==targetKey
                 Also,
                Root.right==null
                Root.left==null
            Return

  Successor=3
  Predecessor=1

Hope it helps! 希望能帮助到你!

You can find the predecessor and successor of a node in a binary search tree by using an inOrder traversal. 您可以使用inOrder遍历在二分搜索树中找到节点的前任和后任。 The basic structure of an inOrder traversal is: inOrder遍历的基本结构是:

inOrder(Node n)
  if (n == null) return;
  inOrder(n.left)
  visit(n)
  inOrder(n.right)

In this case, when we visit a node we want to keep track of the predecessor, matched node, and successor, based on which we've already seen. 在这种情况下,当我们访问一个节点时,我们要跟踪已经看到的前任节点,匹配节点和后继节点。 Here's the basic logic: 这是基本逻辑:

visit(n)
  if n.val == searchVal
    match = n
  else if match == null
    predecessor = n
  else if successor == null
    successor = n;

Here's some Java code. 这是一些Java代码。 I'm using a simple 3 element array to store the predecessor, match, and successor. 我正在使用一个简单的3元素数组来存储前任,匹配和后继。

class Node
{
  int val;
  Node left, right;
}

static void inOrder(Node n, int val, Node[] seq)
{
  if(n == null) return;

  inOrder(n.left, val, seq);

  if(n.val == val) 
    seq[1] = n;
  else if(seq[1] == null)
    seq[0] = n;
  else if(seq[2] == null)
    seq[2] = n;

  inOrder(n.right, val, seq);   
}

public static void main(String[] args)
{
  Node root = buildTree();
  int searchVal = Integer.parseInt(args[0]);

  Node[] seq = new Node[3];
  inOrder(root, searchVal , seq);
  System.out.println("Predecessor: " + seq[0]);
  System.out.println("Match: " + seq[1]);
  System.out.println("Successor: " + seq[2]);
}

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

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