简体   繁体   中英

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. 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

  • 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
    and search recursively into left subtree

    else set the predecessor as root and search recursively into right subtree

Edit: If successor or predecessor returns null print 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. The basic structure of an inOrder traversal is:

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. I'm using a simple 3 element array to store the predecessor, match, and successor.

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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