File tree 5 files changed +81
-1
lines changed
src/_DataStructures_/Trees/BinarySearchTree
5 files changed +81
-1
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
37
37
- [ Doubly Linked List] ( src/_DataStructures_/DoublyLinkedList )
38
38
39
39
- [ 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 )
41
43
- [ Suffix Tree] ( src/_DataStructures_/SuffixTree )
42
44
43
45
### Logical Problems
File renamed without changes.
Original file line number Diff line number Diff line change
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 ;
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
+ // 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.
You can’t perform that action at this time.
0 commit comments