Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfbee74

Browse files
authored
Merge pull request knaxus#62 from knaxus/problems
New Problem: kth max & min in BST
2 parents 19222fb + ab8b038 commit bfbee74

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
3737
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
3838

3939
- [Trees](src/_DataStructures_/Trees)
40-
- [Binary Search Tree](src/_DataStructures_/Trees/BST)
40+
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
41+
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
42+
- [Find k<sup>th</sup> minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min)
4143
- [Suffix Tree](src/_DataStructures_/SuffixTree)
4244

4345
### Logical Problems
File renamed without changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const BST = require('../index');
2+
3+
// Inorder traversal returns a sorted array
4+
function inOrderTraversal(root) {
5+
if (root === null) return [];
6+
let arr = [];
7+
// traverse left
8+
const left = inOrderTraversal(root.leftChild);
9+
arr = [...left, root.value];
10+
const right = inOrderTraversal(root.rightChild);
11+
return [...arr, ...right];
12+
}
13+
14+
function findKthMax(rootNode, k) {
15+
const arr = inOrderTraversal(rootNode);
16+
if (k <= 0 || k > arr.lenth) {
17+
throw new Error('Invalid value for K');
18+
}
19+
return arr[arr.length - k];
20+
}
21+
22+
// // create a BST
23+
// const myBST = new BST(6);
24+
25+
// myBST.add(2);
26+
// myBST.add(19);
27+
// myBST.add(14);
28+
// myBST.add(8);
29+
// myBST.add(5);
30+
// myBST.add(12);
31+
// myBST.add(33);
32+
// myBST.add(52);
33+
// myBST.add(1);
34+
35+
// // find 3rd max
36+
// console.log(findKthMax(myBST.root, 3));
37+
38+
module.exports = findKthMax;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
// Inorder traversal returns a sorted array
5+
function inOrderTraversal(root) {
6+
if (root === null) return [];
7+
let arr = [];
8+
// traverse left
9+
const left = inOrderTraversal(root.leftChild);
10+
arr = [...left, root.value];
11+
const right = inOrderTraversal(root.rightChild);
12+
return [...arr, ...right];
13+
}
14+
15+
function findKthMin(rootNode, k) {
16+
const arr = inOrderTraversal(rootNode);
17+
if (k <= 0 || k > arr.lenth) {
18+
throw new Error('Invalid value for K');
19+
}
20+
return arr[k - 1];
21+
}
22+
23+
// // create a BST
24+
// const myBST = new BST(6);
25+
26+
// myBST.add(2);
27+
// myBST.add(19);
28+
// myBST.add(14);
29+
// myBST.add(8);
30+
// myBST.add(5);
31+
// myBST.add(12);
32+
// myBST.add(33);
33+
// myBST.add(52);
34+
// myBST.add(1);
35+
// myBST.add(0);
36+
37+
// // find 3rd max
38+
// console.log(findKthMin(myBST.root, 3));
39+
40+
module.exports = findKthMin;
File renamed without changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /