@@ -5,85 +5,61 @@ describe('Binary Search Tree', () => {
55
66 beforeEach ( ( ) => {
77 tree = new Tree ( )
8- } )
9-
10- test ( 'should add values to the tree' , ( ) => {
118 tree . addValue ( 10 )
129 tree . addValue ( 5 )
1310 tree . addValue ( 15 )
11+ tree . addValue ( 3 )
12+ tree . addValue ( 8 )
13+ } )
1414
15- expect ( tree . search ( 10 ) ) . toBe ( 10 )
15+ test ( 'should add values to the tree' , ( ) => {
16+ tree . addValue ( 12 )
17+
18+ expect ( tree . search ( 12 ) ) . toBe ( 12 )
1619 expect ( tree . search ( 5 ) ) . toBe ( 5 )
1720 expect ( tree . search ( 15 ) ) . toBe ( 15 )
1821 } )
1922
2023 test ( 'should perform in-order traversal' , ( ) => {
2124 const values = [ ]
2225 const output = ( val ) => values . push ( val )
23-
24- tree . addValue ( 10 )
25- tree . addValue ( 5 )
26- tree . addValue ( 15 )
27- tree . addValue ( 3 )
28- tree . addValue ( 8 )
29-
3026 tree . traverse ( output )
3127 expect ( values ) . toEqual ( [ 3 , 5 , 8 , 10 , 15 ] )
3228 } )
3329
3430 test ( 'should remove leaf nodes correctly' , ( ) => {
35- tree . addValue ( 10 )
36- tree . addValue ( 5 )
37- tree . addValue ( 15 )
38-
3931 tree . removeValue ( 5 )
4032 expect ( tree . search ( 5 ) ) . toBeNull ( )
4133 } )
4234
4335 test ( 'should remove nodes with one child correctly' , ( ) => {
44- tree . addValue ( 10 )
45- tree . addValue ( 5 )
46- tree . addValue ( 15 )
4736 tree . addValue ( 12 )
48-
4937 tree . removeValue ( 15 )
38+
5039 expect ( tree . search ( 15 ) ) . toBeNull ( )
5140 expect ( tree . search ( 12 ) ) . toBe ( 12 )
5241 } )
5342
5443 test ( 'should remove nodes with two children correctly' , ( ) => {
55- tree . addValue ( 10 )
56- tree . addValue ( 5 )
57- tree . addValue ( 15 )
58- tree . addValue ( 12 )
5944 tree . addValue ( 18 )
60-
6145 tree . removeValue ( 15 )
46+
6247 expect ( tree . search ( 15 ) ) . toBeNull ( )
63- expect ( tree . search ( 12 ) ) . toBe ( 12 )
6448 expect ( tree . search ( 18 ) ) . toBe ( 18 )
6549 } )
6650
6751 test ( 'should return null for non-existent values' , ( ) => {
68- tree . addValue ( 10 )
69- tree . addValue ( 5 )
70- tree . addValue ( 15 )
71-
7252 expect ( tree . search ( 20 ) ) . toBeNull ( )
7353 expect ( tree . search ( 0 ) ) . toBeNull ( )
7454 } )
7555
7656 test ( 'should handle removal of root node correctly' , ( ) => {
77- tree . addValue ( 10 )
78- tree . addValue ( 5 )
79- tree . addValue ( 15 )
80-
8157 tree . removeValue ( 10 )
8258 expect ( tree . search ( 10 ) ) . toBeNull ( )
8359 } )
8460
8561 test ( 'should handle empty tree gracefully' , ( ) => {
86- expect ( tree . search ( 10 ) ) . toBeNull ( )
87- tree . removeValue ( 10 ) // Should not throw
62+ expect ( tree . search ( 22 ) ) . toBeNull ( )
63+ tree . removeValue ( 22 ) // Should not throw
8864 } )
8965} )
0 commit comments