1+ 'use strict' ;
2+ class Node {
3+ constructor ( data ) {
4+ this . data = data ;
5+ this . leftNode = this . rightNode = null ;
6+ }
7+ }
8+
9+ function insertionOfk ( root , k ) {
10+ if ( root == null ) return new Node ( k ) ;
11+ if ( root . data > k )
12+ root . leftNode = insertionOfk ( root . leftNode , k ) ;
13+ else if ( root . data < k )
14+ root . rightNode = insertionOfk ( root . rightNode , k ) ;
15+ return root ;
16+ }
17+
18+ function searchK ( root , k ) {
19+ if ( root == null ) return false ;
20+ else if ( root . data == k ) return true ;
21+ else if ( root . data > k ) return searchK ( root . leftNode , k ) ;
22+ else return searchK ( root . rightNode , k ) ;
23+ }
24+
25+ function inorderDisplay ( root , inOrderArr ) {
26+ if ( root == null ) return ;
27+ inorderDisplay ( root . leftNode , inOrderArr ) ;
28+ inOrderArr . push ( root . data ) ;
29+ inorderDisplay ( root . rightNode , inOrderArr ) ;
30+ }
31+
32+ let tree = null ;
33+ tree = insertionOfk ( tree , 4 ) ;
34+ tree = insertionOfk ( tree , 2 ) ;
35+ tree = insertionOfk ( tree , 3 ) ;
36+ tree = insertionOfk ( tree , 1 ) ;
37+ tree = insertionOfk ( tree , 6 ) ;
38+ tree = insertionOfk ( tree , 5 ) ;
39+ tree = insertionOfk ( tree , 7 ) ;
40+
41+ // Inorder display
42+ let inOrderArr = [ ] ;
43+ inorderDisplay ( tree , inOrderArr ) ;
44+ console . log ( 'Inorder - ' , inOrderArr . join ( ', ' ) ) ;
45+
46+ // Search K
47+ let k = 7 ;
48+ console . log ( k + ' is found ? - ' , searchK ( tree , k ) ) ;
0 commit comments