用于在BST中查找第k个最小元素的Java程序

时间:2016-05-11 14:38:52

标签: java binary-search-tree

我正在编写一个Java程序来查找BST中的第k个最小元素。

我已经就堆栈溢出这个问题进行了一些其他的帖子并且已经完成了他们的解决方案,但我无法理解我的代码有什么问题。 如果有人可以请告诉我为什么我的程序没有打印任何东西。

//Program to find the kth smallest element in the BST
public class KthSmallestElement {
static Node root;
//method to insert a key
Node insertRec(Node root, int key)
{
    //if the tree is empty
    if(root == null)
    {
       root = new Node(key);
       return root;
    }
    //otherwise recur down the tree
    else
    {
        if(key > root.key)
        {
            root.right = insertRec(root.right, key);
        }
        else
        {
            root.left = insertRec(root.left, key);
        }
        return root;
     }
}

//This method is for calling the insertRec() method
Node insert(int key)
    {
        root = insertRec(root, key);
        return root;
    }

//This method is for doing the inorder traversal of the tree
void kthSmallest(Node root, int k)
{
    int counter = 0;
    if(root == null)
        return;
   else
    {
             kthSmallest(root.left, k);
             counter++;

    if(counter == k)
        {
          System.out.println(root.key);
         }
        kthSmallest(root.right, k); 
    }
 }    

 //main method
public static void main(String[] args)
{
    KthSmallestElement tree = new KthSmallestElement();
    Node root = null;
    root = tree.insert(20);
    root =tree.insert(8);
    root =tree.insert(22);
    root =tree.insert(4);
    root= tree.insert(12);
    root =tree.insert(10);
    root =tree.insert(14);
    tree.kthSmallest(root, 3);
}

}

我的节点类如下:

//Class containing left and right child of current node and key value
public class Node {
int key;
Node left, right, parent;

//Constructor for the node
public Node(int item){
  key = item;
  left = right = parent= null;
}

}

它没有打印任何东西。这就是问题所在。 好的,我不太擅长编程所以请原谅我在这里提出这样的问题。

1 个答案:

答案 0 :(得分:1)

您的计数器k无法更新,counter具有方法范围,并且会在每次递归调用时被丢弃,从而导致问题。您需要通过所有方法调用创建一致的计数器:

int kthSmallest(Node root , int k){
    //empty root, don't change counter
    if(root == null)
        return k;
    else {
         //update counter - equivalent to k -= root.left.size()
         k = kthSmallest(root.left, k);

         if(k == 0) //kth element
         {
             System.out.println(root.key);
             return -1; //return some invalid counter
         }

         //currently visited node
         k--;

         //search right subtree
         return kthSmallest(root.right, k); 
    }
}

此代码使用k作为计数器,并从k向下计数到0,并返回k为0的节点。

您的代码不起作用,因为counter具有方法本地范围。这意味着counter的值仅对当前kthSmallest的调用有效。在下一个递归调用中,counter将被设置为0 - 请注意,这是内存中的 counter,而不是之前调用的那个 - 因此您的代码可以&# 39; t到达counter == k,除非k == 1

对于这棵树:

                                   1
                                /     \
                               2       3

对于k > 1

,程序流程的描述如下所示
//i'll apply IDs to uniquely identify each function-call and
// it's corresponding variables. the suffix #<Uppercase Letter>
// is used to identify a variable by corresponding method-call

kthElement(n , k): #A 
|   counter#A = 0 //initialize counter
|
|   kthElement(n.left , k): #B
|   |   counter#B = 0
|   |
|   |   kthElement(n.left.left , k): #C
|   |   |   counter#C = 0
|   |   |   n.left.left == null -> return from #D
|   |
|   |   counter#B++ -> counter#B = 1
|   |
|   |   counter#B != k -> don't print
|   |
|   |   kthElement(n.left.right , k): #D
|   |   |   counter#D = 0
|   |   |   n.left.right == null -> return from #D
|   |
|   |   end of method -> implicit return from #B
|   
|   counter#A++ -> counter#A = 1
|   ...
...

请注意kthElement的每次调用如何创建自己的counter,每个节点只增加一次。