Skip to content

Commit e78eae5

Browse files
authored
Merge pull request knaxus#63 from knaxus/trees
3 New Problems on BST
2 parents 597cd0e + 28e73b5 commit e78eae5

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
4141
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
4242
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
4343
- [Find k<sup>th</sup> minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min)
44+
- [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors)
45+
- [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst)
46+
- [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root)
4447
- [Suffix Tree](src/_DataStructures_/SuffixTree)
4548

4649
### Logical Problems
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
/** You should go through this conversation here:
5+
* https://github.com/knaxus/problem-solving-javascript/pull/63
6+
*/
7+
8+
function findAncestors(root, value) {
9+
/**
10+
* search the given node and meanwhile
11+
* keep pushing the visited nodes
12+
*/
13+
if (root === null) return false;
14+
15+
if (value < root.value) {
16+
const left = findAncestors(root.leftChild, value);
17+
if (left) {
18+
return [...left, root.value];
19+
}
20+
return false;
21+
}
22+
23+
if (value > root.value) {
24+
const right = findAncestors(root.rightChild, value);
25+
if (right) {
26+
return [...right, root.value];
27+
}
28+
return false;
29+
}
30+
31+
if (value === root.value) return [];
32+
return false;
33+
}
34+
35+
// create a BST
36+
// const myBST = new BST(6);
37+
// myBST.add(4);
38+
// myBST.add(9);
39+
// myBST.add(2);
40+
// myBST.add(5);
41+
// myBST.add(14);
42+
// myBST.add(8);
43+
// myBST.add(12);
44+
// myBST.add(10);
45+
46+
// console.log(findAncestors(myBST.root, 10));
47+
// console.log(findAncestors(myBST.root, 101));
48+
49+
module.exports = findAncestors;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
function findKNodes(root, k) {
5+
let arr = [];
6+
7+
if (root === null) return [];
8+
if (k === 0) return [...arr, root.value];
9+
10+
const left = findKNodes(root.leftChild, k - 1);
11+
arr = [...arr, ...left];
12+
13+
const right = findKNodes(root.rightChild, k - 1);
14+
arr = [...arr, ...right];
15+
return arr;
16+
}
17+
18+
// create a BST
19+
// const myBST = new BST(6);
20+
21+
// myBST.add(2);
22+
// myBST.add(19);
23+
// myBST.add(14);
24+
// myBST.add(8);
25+
// myBST.add(5);
26+
// myBST.add(12);
27+
// myBST.add(33);
28+
// myBST.add(52);
29+
// myBST.add(1);
30+
31+
// console.log(findKNodes(myBST.root, 2));
32+
33+
module.exports = findKNodes;

src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line no-unused-vars
12
const BST = require('../index');
23

34
// Inorder traversal returns a sorted array

src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ function findKthMin(rootNode, k) {
3434
// myBST.add(1);
3535
// myBST.add(0);
3636

37-
// // find 3rd max
3837
// console.log(findKthMin(myBST.root, 3));
3938

4039
module.exports = findKthMin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
function findHeightOfBST(root) {
5+
let leftHeight = 0;
6+
let rightHeight = 0;
7+
8+
if (root === null) return 0;
9+
leftHeight = findHeightOfBST(root.leftChild);
10+
rightHeight = findHeightOfBST(root.rightChild);
11+
12+
if (leftHeight > rightHeight) {
13+
return leftHeight + 1;
14+
}
15+
return rightHeight + 1;
16+
}
17+
18+
// create a BST
19+
// const myBST = new BST(6);
20+
// myBST.add(4);
21+
// myBST.add(9);
22+
// myBST.add(2);
23+
// myBST.add(5);
24+
// myBST.add(14);
25+
// myBST.add(8);
26+
// myBST.add(12);
27+
// myBST.add(10);
28+
29+
// // console.log(myBST.root);
30+
// console.log(myBST.traversePreorder());
31+
// console.log(findHeightOfBST(myBST.root));
32+
33+
module.exports = findHeightOfBST;

0 commit comments

Comments
 (0)