[英]Java program to find the kth smallest element in a BST
我正在編寫一個 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;
}
}
它沒有打印任何東西。這就是問題所在。 好吧,我不太擅長編程,所以請原諒我在這里提出這樣的問題。
您的計數器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
,而不是前一次調用中的counter == k
- 因此您的代碼無法到達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
,每個節點只增加一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.