File tree 6 files changed +119
-1
lines changed
src/_DataStructures_/Trees/BinarySearchTree
6 files changed +119
-1
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
41
41
- [ Binary Search Tree] ( src/_DataStructures_/Trees/BinarySearchTree )
42
42
- [ Find k<sup >th</sup > maximum in a BinarySearchTree] ( src/_DataStructures_/Trees/BinarySearchTree/find-kth-max )
43
43
- [ 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 )
44
47
- [ Suffix Tree] ( src/_DataStructures_/SuffixTree )
45
48
46
49
### Logical Problems
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
1
+ // eslint-disable-next-line no-unused-vars
1
2
const BST = require ( '../index' ) ;
2
3
3
4
// Inorder traversal returns a sorted array
Original file line number Diff line number Diff line change @@ -34,7 +34,6 @@ function findKthMin(rootNode, k) {
34
34
// myBST.add(1);
35
35
// myBST.add(0);
36
36
37
- // // find 3rd max
38
37
// console.log(findKthMin(myBST.root, 3));
39
38
40
39
module . exports = findKthMin ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments