@@ -28,29 +28,32 @@ function insertRecursively(root, data) {
2828
2929/*
3030O(Log(n)) time | Space O(1)
31-
32- For 1 insert operation, avg case is O(lgn) and worst case is O(n)
31+ For 1 insert operation, avg case is O(log(n)) and worst case is O(n)
3332For n insert operations, avg case is O(nlgn) and worst case is O(n^2)
3433*/
3534function insert ( root , data ) {
36- // If the tree is empty create new node.
37- let notToInsert = new Node ( data ) ;
38-
35+ const nodeToInsert = new Node ( data ) ;
36+ if ( root == null ) return nodeToInsert ;
3937 let current = root ;
40- let previous = null ;
38+ let previous = root ;
4139 while ( current != null ) {
4240 previous = current ;
4341 if ( data <= current . data ) current = current . left ;
4442 else current = current . right ;
4543 }
46-
47- if ( previous == null ) return notToInsert ;
48- else if ( data <= previous . data ) previous . left = notToInsert ;
49- else previous . right = notToInsert ;
44+ if ( data <= previous . data ) previous . left = nodeToInsert ;
45+ else previous . right = nodeToInsert ;
5046
5147 return root ;
5248}
53-
49+ class Node {
50+ constructor ( data ) {
51+ this . data = data ;
52+ this . left = null ;
53+ this . right = null ;
54+ }
55+ }
56+ // O(log n) time | space O(1)
5457function remove ( root , data ) {
5558 if ( root == null ) return root ;
5659 else if ( data < root . data ) root . left = remove ( root . left , data ) ;
@@ -60,25 +63,23 @@ function remove(root, data) {
6063 // case 1: no child
6164 if ( root . left == null && root . right == null ) {
6265 root = null ;
63- }
64-
65- // case 2: One child
66- else if ( root . left == null ) {
66+ } else if ( root . left == null ) {
67+ // case 2: One child
6768 let current = root ;
6869 root = root . right ;
6970 current = null ;
7071 } else if ( root . right == null ) {
7172 let current = root ;
7273 root = root . left ;
7374 current = null ;
74- }
75-
76- // case 3: Two child
77- else {
78- let current = findMin ( root . right ) ;
75+ } else {
76+ // case 3: Two child
77+ let current = findMin ( root . right ) ;
7978 root . data = current . data ;
80- root . right = remove ( root . right , current . data ) ;
79+ root . right = remove ( root . right , current . data ) ;
80+ }
8181 }
82+ return root ;
8283}
8384
8485// O(log(n)) time | O(log (n))
@@ -89,7 +90,7 @@ function searchRecursive(root, data) {
8990 else return search ( root . right , data ) ;
9091}
9192
92- // O(log(n)) time | O(log (n) )
93+ // O(log(n)) time | O(1 )
9394function search ( root , data ) {
9495 while ( root != null ) {
9596 if ( data === root . data ) return true ;
@@ -100,6 +101,7 @@ function search(root, data) {
100101 return false ;
101102}
102103
104+ // O(log(n)) time | O(1)
103105function findMin ( root ) {
104106 if ( root == null ) return root ;
105107
@@ -110,6 +112,7 @@ function findMin(root) {
110112 return root . data ;
111113}
112114
115+ // O(log(n)) time | O(1)
113116function findMax ( root ) {
114117 if ( root == null ) return root ;
115118
@@ -126,8 +129,8 @@ function findHeight(root) {
126129 return Math . max ( findHeight ( root . left ) , findHeight ( root . right ) ) + 1 ;
127130}
128131
129- describe ( " In Binary Search Tree" , ( ) => {
130- it ( " I can insert" , ( ) => {
132+ describe ( ' In Binary Search Tree' , ( ) => {
133+ it ( ' I can insert' , ( ) => {
131134 let root = null ;
132135 root = insert ( root , 15 ) ;
133136 root = insert ( root , 10 ) ;
@@ -139,7 +142,7 @@ describe("In Binary Search Tree", () => {
139142 expect ( root ) . toBeTruthy ( ) ;
140143 } ) ;
141144
142- it ( " I can insert recursively" , ( ) => {
145+ it ( ' I can insert recursively' , ( ) => {
143146 let root = null ;
144147 root = insertRecursively ( root , 15 ) ;
145148 root = insertRecursively ( root , 10 ) ;
@@ -152,7 +155,7 @@ describe("In Binary Search Tree", () => {
152155 } ) ;
153156} ) ;
154157
155- describe ( " I can perform below operations in BST" , ( ) => {
158+ describe ( ' I can perform below operations in BST' , ( ) => {
156159 let root = null ;
157160
158161 beforeEach ( ( ) => {
@@ -165,25 +168,30 @@ describe("I can perform below operations in BST", () => {
165168 root = insert ( root , 12 ) ;
166169 } ) ;
167170
168- it ( " I can search recursively" , ( ) => {
171+ it ( ' I can search recursively' , ( ) => {
169172 expect ( searchRecursive ( root , 8 ) ) . toBeTruthy ( ) ;
170173 expect ( searchRecursive ( root , 22 ) ) . toBeFalsy ( ) ;
171174 } ) ;
172175
173- it ( " I can search iteratively" , ( ) => {
176+ it ( ' I can search iteratively' , ( ) => {
174177 expect ( search ( root , 8 ) ) . toBeTruthy ( ) ;
175178 expect ( search ( root , 22 ) ) . toBeFalsy ( ) ;
176179 } ) ;
177180
178- it ( " I can find Minimum in BST" , ( ) => {
181+ it ( ' I can find Minimum in BST' , ( ) => {
179182 expect ( findMin ( root ) ) . toBe ( 8 ) ;
180183 } ) ;
181184
182- it ( " I can find Maximum in BST" , ( ) => {
185+ it ( ' I can find Maximum in BST' , ( ) => {
183186 expect ( findMax ( root ) ) . toBe ( 25 ) ;
184187 } ) ;
185188
186- it ( " I can find Height of BST" , ( ) => {
189+ it ( ' I can find Height of BST' , ( ) => {
187190 expect ( findHeight ( root ) ) . toBe ( 2 ) ;
188191 } ) ;
192+
193+ it ( 'I can find Delete 8 from BST' , ( ) => {
194+ remove ( root , 8 ) ;
195+ expect ( search ( root , 8 ) ) . toBeFalsy ( ) ;
196+ } ) ;
189197} ) ;
0 commit comments