Skip to content

Commit b9f292a

Browse files
committed
Kth Largest BST Implemented in JS
1 parent 51999f5 commit b9f292a

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
The Node class represents a single node in the BST. It has three properties: value to store the node's value, left to store the reference to the left child node, and right to store the reference to the right child node.
3+
*/
4+
class Node {
5+
constructor(value) {
6+
this.value = value;
7+
this.left = null;
8+
this.right = null;
9+
}
10+
}
11+
12+
/**
13+
the BST class represents the Binary Search Tree. It has one property: root to store the reference to the root node of the tree.
14+
*/
15+
class BST {
16+
constructor() {
17+
this.root = null;
18+
}
19+
20+
/**
21+
insert(value): This function inserts a new node with the given value into the BST. It uses the insertNode helper function for recursive insertion. The time complexity is O(log n) on average (O(n) in the worst case if the tree is heavily unbalanced), and the space complexity is O(1).
22+
* @param {number} value - The value to be inserted
23+
*/
24+
insert(value) {
25+
const newNode = new Node(value);
26+
27+
if (this.root === null) {
28+
this.root = newNode;
29+
} else {
30+
this.insertNode(this.root, newNode);
31+
}
32+
}
33+
34+
/**
35+
The insertNode(node, newNode) function is a helper function used by the insert(value) method in the BST class. It recursively inserts a new node (newNode).
36+
37+
Here's a breakdown of how the insertNode function works:
38+
39+
* It compares the value of the newNode with the value of the node to determine whether to go left or right in the BST.
40+
* If the value of newNode is less than the value of node, it checks if the left child of node is null. If it is, the newNode becomes the left child; otherwise, the function recursively calls itself with the left child node of node.
41+
* If the value of newNode is greater than or equal to the value of node, it checks if the right child of node is null. If it is, the newNode becomes the right child; otherwise, the function recursively calls itself with the right child node of node.
42+
* This process continues until a suitable position is found for the newNode in the BST.
43+
44+
The time complexity of the insertNode function depends on the structure of the BST. In the average case, when the tree is balanced, the time complexity is O(log n), where n is the number of nodes in the tree. This is because at each level of the tree, the function divides the remaining nodes to be searched by half. However, in the worst case scenario, when the tree is heavily unbalanced (e.g., resembles a linked list), the time complexity becomes O(n), where n is the number of nodes in the tree. This happens when all nodes are in a straight line from the root.
45+
46+
The space complexity of the insertNode function is O(log n) in the average case and O(n) in the worst case. This is due to the recursive calls that consume memory on the call stack. In the average case, the maximum number of recursive calls is limited by the height of the balanced tree, which is logarithmic to the number of nodes. In the worst case, where the tree is unbalanced, the maximum number of recursive calls is equal to the number of nodes in the tree.
47+
48+
* @param {Node} node - The current node being traversed
49+
* @param {Node} newNode - The new node to be inserted
50+
*/
51+
insertNode(node, newNode) {
52+
if (newNode.value < node.value) {
53+
if (node.left === null) {
54+
node.left = newNode;
55+
} else {
56+
this.insertNode(node.left, newNode);
57+
}
58+
} else {
59+
if (node.right === null) {
60+
node.right = newNode;
61+
} else {
62+
this.insertNode(node.right, newNode);
63+
}
64+
}
65+
}
66+
67+
/**
68+
findKthLargest(k): This function finds the Kth largest value in the BST. It first performs an in-order traversal of the tree to retrieve a sorted array of values. If K is larger than the number of nodes in the tree, it returns null. Otherwise, it returns the Kth largest value from the sorted array. The time complexity of this function is O(n), where n is the number of nodes in the tree. The space complexity is O(n) since it stores all the values in the array during the traversal.
69+
* @param {number} k - The Kth largest value to find
70+
* @returns {number|null} - The Kth largest value, or null if it doesn't exist
71+
*/
72+
findKthLargest(k) {
73+
if (k <= 0) {
74+
throw new Error('k should be a positive integer');
75+
}
76+
77+
const sortedValues = [];
78+
this.inOrderTraversal(this.root, sortedValues);
79+
80+
if (k > sortedValues.length) {
81+
return null;
82+
}
83+
84+
return sortedValues[sortedValues.length - k];
85+
}
86+
87+
/**
88+
inOrderTraversal(node, values): This is a helper function that performs an in-order traversal of the BST and stores the sorted values in the given array. It recursively visits the left subtree, then the current node, and finally the right subtree. The time complexity of this function is O(n), where n is the number of nodes in the tree, as it needs to visit each node exactly once. The space complexity is O(n) since it uses the array to store the values.
89+
* @param {Node} node - The current node being traversed
90+
* @param {Array} values - The array to store the sorted values
91+
*/
92+
inOrderTraversal(node, values) {
93+
if (node !== null) {
94+
this.inOrderTraversal(node.left, values);
95+
values.push(node.value);
96+
this.inOrderTraversal(node.right, values);
97+
}
98+
}
99+
}
100+
// Sample input and imlplementation
101+
/**
102+
5
103+
/ \
104+
3 7
105+
/ \ / \
106+
2 4 6 8
107+
108+
Now, let's find the 3rd largest value in the BST.
109+
110+
The sorted order of the tree is [2, 3, 4, 5, 6, 7, 8].
111+
112+
The 3rd largest value is 6.
113+
114+
Here's the updated tree with the 3rd largest value marked:
115+
5
116+
/ \
117+
3 7
118+
/ \ / \
119+
2 4 6* 8
120+
121+
As you can see, the 3rd largest value, 6, is marked with an asterisk (*).
122+
123+
*/
124+
// Create a new instance of BST
125+
const bst = new BST();
126+
127+
// Insert nodes into the BST
128+
bst.insert(5);
129+
bst.insert(3);
130+
bst.insert(7);
131+
bst.insert(2);
132+
bst.insert(4);
133+
bst.insert(6);
134+
bst.insert(8);
135+
136+
// Find the 3rd largest value
137+
const kthLargest = bst.findKthLargest(3);
138+
console.log(kthLargest); // Output: 6

0 commit comments

Comments
 (0)