簡體   English   中英

在Java中查找二進制搜索樹的前任和后任

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

我的項目要我打印出二進制搜索樹的前身和后繼。 分配要求我傳入節點的數據作為參數。 當我嘗試打印給定節點數據的前身時,它的值為0。我孜孜不倦地嘗試如何解決問題,卻找不到任何地方。 希望您能找到問題所在。

方法如下:

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

這是我認為正確的方法的偽代碼:

Input: root node,key | output: predecessor node, successor node 
  • 如果root為NULL,則返回

  • 如果找到鑰匙,那么

     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. 

    返回

  • 如果 key較小,則根節點將后繼者設置為root
    然后遞歸搜索到左子樹

    否則 將前任設置為root並遞歸搜索到正確的子樹

編輯:如果后繼或前任返回null,則輸出null。

                         (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

希望能幫助到你!

您可以使用inOrder遍歷在二分搜索樹中找到節點的前任和后任。 inOrder遍歷的基本結構是:

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

在這種情況下,當我們訪問一個節點時,我們要跟蹤已經看到的前任節點,匹配節點和后繼節點。 這是基本邏輯:

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

這是一些Java代碼。 我正在使用一個簡單的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