-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstFindKthMax.js
58 lines (50 loc) · 1.47 KB
/
bstFindKthMax.js
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
// Given a BST, retrieve the kth maximum value.
class BinarySearchTreeNode {
constructor(val) {
this.val = val;
this.leftChild = null;
this.rightChild = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
addNode(newVal) {
!this.root ? this.root = new BinarySearchTreeNode(newVal) : this.insert(this.root, newVal);
}
insert(currentNode, newVal) {
if (!currentNode) {
currentNode = new BinarySearchTreeNode(newVal);
} else if (newVal < currentNode.val) {
currentNode.leftChild = this.insert(currentNode.leftChild, newVal);
} else {
currentNode.rightChild = this.insert(currentNode.rightChild, newVal);
}
return currentNode;
}
sortTree(currentNode, sortedTreeArr) {
if (currentNode) {
sortedTreeArr = this.sortTree(currentNode.leftChild, sortedTreeArr);
sortedTreeArr.push(currentNode.val);
sortedTreeArr = this.sortTree(currentNode.rightChild, sortedTreeArr);
}
return sortedTreeArr;
}
getKthMax(k) {
console.log(`Retrieving kth (${k}) max value from tree...`);
const sortedTreeArr = this.sortTree(this.root, []);
if (sortedTreeArr.length - k >= 0) {
return console.log(sortedTreeArr[sortedTreeArr.length - k])
} else {
return console.log(`Tree does not contain ${k} values`);
}
}
}
const myBST = new BinarySearchTree();
myBST.addNode(50);
myBST.addNode(25);
myBST.addNode(75);
myBST.addNode(100);
myBST.addNode(125);
myBST.getKthMax(3);