/
BSTKthSmallest.java
77 lines (56 loc) · 2.34 KB
/
BSTKthSmallest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.hrishikeshmishra.practices.binarysearchtree;
import com.hrishikeshmishra.practices.binarytree.BTPrinter;
import com.hrishikeshmishra.practices.binarytree.BinaryTreeNode;
import java.util.Objects;
/**
* Problem:
* Find k-th smallest element in BST (Order Statistics in BST)
* Given root of binary search tree and K as input, find K-th smallest element in BST.
*
* @author hrishikesh.mishra
* @link http://hrishikeshmishra.com/find-k-th-smallest-element-in-bst-order-statistics-in-bst/
*/
public class BSTKthSmallest {
public static class ResultSet {
private int value;
private int counter;
}
public static int findKthSmallest(BinaryTreeNode<Integer> root, int k) {
ResultSet resultSet = new ResultSet();
findKthSmallest(root, k, resultSet);
if (resultSet.counter < k){
throw new RuntimeException("No such element");
}
return resultSet.value;
}
private static ResultSet findKthSmallest(BinaryTreeNode<Integer> node, int k, ResultSet resultSet) {
if (Objects.isNull(node) || resultSet.counter >= k) {
return resultSet;
}
findKthSmallest(node.getLeft(), k, resultSet);
resultSet.counter++;
if (resultSet.counter == k) {
resultSet.value = node.getData();
}
findKthSmallest(node.getRight(), k, resultSet);
return resultSet;
}
}
class BSTKthSmallestTest {
public static void main(String[] args) {
BinaryTreeNode<Integer> bstRoot = new BinaryTreeNode<>(20,
new BinaryTreeNode<>(3,
new BinaryTreeNode<>(2),
new BinaryTreeNode<>(10)),
new BinaryTreeNode<>(30,
new BinaryTreeNode<>(27,
new BinaryTreeNode<>(26),
new BinaryTreeNode<>(28)),
new BinaryTreeNode<>(35)));
BTPrinter.print(bstRoot);
System.out.println("2nd Smallest : " + BSTKthSmallest.findKthSmallest(bstRoot, 2));
System.out.println("4th Smallest : " + BSTKthSmallest.findKthSmallest(bstRoot, 4));
System.out.println("7th Smallest : " + BSTKthSmallest.findKthSmallest(bstRoot, 7));
System.out.println("70th Smallest : " + BSTKthSmallest.findKthSmallest(bstRoot, 70));
}
}