1
1
import { BinaryTree } from "../ts/BinaryTree/BinaryTree" ;
2
2
3
- describe ( ' BinaryTree' , function ( ) {
4
- describe ( ' add' , function ( ) {
5
- it ( ' should be able to add node' , function ( ) {
3
+ describe ( " BinaryTree" , function ( ) {
4
+ describe ( " add" , function ( ) {
5
+ it ( " should be able to add node" , function ( ) {
6
6
const tree = new BinaryTree < number > ( ) ;
7
7
tree . add ( 2 ) ;
8
8
expect ( tree . root . value ) . toEqual ( 2 ) ;
9
9
tree . add ( 1 ) ;
10
10
expect ( tree . root . left . value ) . toEqual ( 1 ) ;
11
11
} ) ;
12
- it ( ' should be able to add node' , function ( ) {
13
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
12
+ it ( " should be able to add node" , function ( ) {
13
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
14
14
tree . add ( 5 ) ;
15
15
expect ( tree . root . value ) . toEqual ( 5 ) ;
16
16
tree . add ( 6 ) ;
@@ -20,8 +20,8 @@ describe('BinaryTree', function () {
20
20
} ) ;
21
21
} ) ;
22
22
23
- it ( ' should be able to add node to binary tree with object values' , function ( ) {
24
- const tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
23
+ it ( " should be able to add node to binary tree with object values" , function ( ) {
24
+ const tree = new BinaryTree < any > ( ( a , b ) => a . amount > b . amount ) ;
25
25
tree . add ( { amount : 5 } ) ;
26
26
expect ( tree . root . value ) . toEqual ( { amount : 5 } ) ;
27
27
tree . add ( { amount : 6 } ) ;
@@ -30,35 +30,35 @@ describe('BinaryTree', function () {
30
30
expect ( tree . root . left . value ) . toEqual ( { amount : 4 } ) ;
31
31
} ) ;
32
32
33
- it ( ' should be able to walk the binary tree in order' , function ( ) {
34
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
33
+ it ( " should be able to walk the binary tree in order" , function ( ) {
34
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
35
35
tree . add ( 5 ) ;
36
36
tree . add ( 6 ) ;
37
37
tree . add ( 4 ) ;
38
38
tree . add ( 1 ) ;
39
39
tree . add ( 2 ) ;
40
- expect ( tree . inorderTreeWalk ( function ( pv , cv ) {
40
+ expect ( tree . inOrderTreeWalk ( function ( pv , cv ) {
41
41
pv . push ( cv ) ;
42
42
return pv ;
43
- } , [ ] ) ) . toEqual ( [ 1 , 2 , 4 , 5 , 6 ] ) ;
43
+ } , [ ] ) ) . toEqual ( [ 1 , 2 , 4 , 5 , 6 ] ) ;
44
44
} ) ;
45
45
46
- it ( ' should be able to walk the binary tree that has object values in order' , function ( ) {
47
- const tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
46
+ it ( " should be able to walk the binary tree that has object values in order" , function ( ) {
47
+ const tree = new BinaryTree < any > ( ( a , b ) => a . amount > b . amount ) ;
48
48
tree . add ( { amount : 6 } ) ;
49
49
tree . add ( { amount : 4 } ) ;
50
50
tree . add ( { amount : 5 } ) ;
51
51
tree . add ( { amount : 1 } ) ;
52
52
tree . add ( { amount : 2 } ) ;
53
- expect ( tree . inorderTreeWalk ( function ( pv , cv ) {
53
+ expect ( tree . inOrderTreeWalk ( function ( pv , cv ) {
54
54
pv . push ( cv . amount ) ;
55
55
return pv ;
56
- } , [ ] ) ) . toEqual ( [ 1 , 2 , 4 , 5 , 6 ] ) ;
56
+ } , [ ] ) ) . toEqual ( [ 1 , 2 , 4 , 5 , 6 ] ) ;
57
57
} ) ;
58
58
59
59
60
- it ( ' should be able to walk the binary tree in order' , function ( ) {
61
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
60
+ it ( " should be able to walk the binary tree in order" , function ( ) {
61
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
62
62
tree . add ( 5 ) ;
63
63
tree . add ( 6 ) ;
64
64
tree . add ( 4 ) ;
@@ -67,11 +67,11 @@ describe('BinaryTree', function () {
67
67
expect ( tree . reverseTreeWalk ( function ( pv , cv ) {
68
68
pv . push ( cv ) ;
69
69
return pv ;
70
- } , [ ] ) ) . toEqual ( [ 6 , 5 , 4 , 2 , 1 ] ) ;
70
+ } , [ ] ) ) . toEqual ( [ 6 , 5 , 4 , 2 , 1 ] ) ;
71
71
} ) ;
72
72
73
- it ( ' should be able to walk the binary tree in reverseorder' , function ( ) {
74
- const tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
73
+ it ( " should be able to walk the binary tree in reverseorder" , function ( ) {
74
+ const tree = new BinaryTree < any > ( ( a , b ) => a . amount > b . amount ) ;
75
75
tree . add ( { amount : 6 } ) ;
76
76
tree . add ( { amount : 4 } ) ;
77
77
tree . add ( { amount : 5 } ) ;
@@ -80,34 +80,34 @@ describe('BinaryTree', function () {
80
80
expect ( tree . reverseTreeWalk ( function ( pv , cv ) {
81
81
pv . push ( cv . amount ) ;
82
82
return pv ;
83
- } , [ ] ) ) . toEqual ( [ 6 , 5 , 4 , 2 , 1 ] ) ;
83
+ } , [ ] ) ) . toEqual ( [ 6 , 5 , 4 , 2 , 1 ] ) ;
84
84
} ) ;
85
85
86
- describe ( ' search' , function ( ) {
87
- it ( ' should return null when no root' , function ( ) {
88
- const tree = new BinaryTree < any > ( function ( a , b ) { return a > b ; } ) ;
86
+ describe ( " search" , function ( ) {
87
+ it ( " should return null when no root" , function ( ) {
88
+ const tree = new BinaryTree < any > ( ( a , b ) => a > b ) ;
89
89
expect ( tree . search ( 5 ) ) . toBeNull ( ) ;
90
90
} ) ;
91
91
92
- it ( ' should return element when found' , function ( ) {
93
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
92
+ it ( " should return element when found" , function ( ) {
93
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
94
94
tree . add ( 5 ) ;
95
95
expect ( tree . search ( 5 ) . value ) . toEqual ( 5 ) ;
96
96
tree . add ( 6 ) ;
97
97
expect ( tree . search ( 6 ) . value ) . toEqual ( 6 ) ;
98
98
} ) ;
99
99
100
- it ( ' should return null when no element was found' , function ( ) {
101
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
100
+ it ( " should return null when no element was found" , function ( ) {
101
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
102
102
tree . add ( 5 ) ;
103
103
expect ( tree . search ( 23 ) ) . toBeNull ( ) ;
104
104
tree . add ( 6 ) ;
105
105
expect ( tree . search ( 16 ) ) . toBeNull ( ) ;
106
106
} ) ;
107
107
} ) ;
108
108
109
- it ( ' should be able to get max value' , function ( ) {
110
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
109
+ it ( " should be able to get max value" , function ( ) {
110
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
111
111
expect ( tree . min ( ) ) . toBeNull ( ) ;
112
112
tree . add ( 1 ) ;
113
113
tree . add ( 2 ) ;
@@ -116,8 +116,8 @@ describe('BinaryTree', function () {
116
116
expect ( tree . min ( ) . value ) . toEqual ( 1 ) ;
117
117
} ) ;
118
118
119
- it ( ' should be able to get min value' , function ( ) {
120
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
119
+ it ( " should be able to get min value" , function ( ) {
120
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
121
121
expect ( tree . max ( ) ) . toBeNull ( ) ;
122
122
tree . add ( 1 ) ;
123
123
tree . add ( 2 ) ;
@@ -126,17 +126,17 @@ describe('BinaryTree', function () {
126
126
expect ( tree . max ( ) . value ) . toEqual ( 5 ) ;
127
127
} ) ;
128
128
129
- it ( ' should be able to get node successor' , function ( ) {
130
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
129
+ it ( " should be able to get node successor" , function ( ) {
130
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
131
131
tree . add ( 1 ) ;
132
132
tree . add ( 2 ) ;
133
133
tree . add ( 4 ) ;
134
134
tree . add ( 5 ) ;
135
135
expect ( tree . successor ( 2 ) . value ) . toEqual ( 4 ) ;
136
136
} ) ;
137
137
138
- it ( ' should be able to get node successor when it doesn\' t have right child' , function ( ) {
139
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
138
+ it ( " should be able to get node successor when it doesn\" t have right child" , function ( ) {
139
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
140
140
tree . add ( 1 ) ;
141
141
tree . add ( 10 ) ;
142
142
tree . add ( 9 ) ;
@@ -145,13 +145,13 @@ describe('BinaryTree', function () {
145
145
expect ( tree . successor ( 9 ) . value ) . toEqual ( 10 ) ;
146
146
} ) ;
147
147
148
- it ( ' should return null for node successor if tree is empty' , function ( ) {
149
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
148
+ it ( " should return null for node successor if tree is empty" , function ( ) {
149
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
150
150
expect ( tree . successor ( 2 ) ) . toBeNull ( ) ;
151
151
} ) ;
152
152
153
- it ( ' should return null if there is no successor' , function ( ) {
154
- const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
153
+ it ( " should return null if there is no successor" , function ( ) {
154
+ const tree = new BinaryTree < number > ( ( a , b ) => a > b ) ;
155
155
tree . add ( 1 ) ;
156
156
expect ( tree . successor ( 1 ) ) . toBeNull ( ) ;
157
157
tree . add ( 2 ) ;
@@ -161,17 +161,17 @@ describe('BinaryTree', function () {
161
161
expect ( tree . successor ( 4 ) . value ) . toEqual ( 5 ) ;
162
162
} ) ;
163
163
164
- describe ( 'delete' , function ( ) {
164
+ describe ( "remove" , function ( ) {
165
165
it ( `should return false if node was not found` , function ( ) {
166
166
const tree = new BinaryTree < number > ( ) ;
167
- expect ( tree . delete ( 1 ) ) . toEqual ( false ) ;
167
+ expect ( tree . remove ( 1 ) ) . toEqual ( false ) ;
168
168
} ) ;
169
169
170
- it ( `should remove node if it doesn' t have children` , function ( ) {
170
+ it ( `should remove node if it doesn" t have children` , function ( ) {
171
171
const tree = new BinaryTree < number > ( ) ;
172
172
tree . add ( 1 ) ;
173
173
expect ( tree . search ( 1 ) ) . not . toBeNull ( ) ;
174
- tree . delete ( 1 ) ;
174
+ tree . remove ( 1 ) ;
175
175
expect ( tree . search ( 1 ) ) . toBeNull ( ) ;
176
176
} ) ;
177
177
@@ -180,7 +180,7 @@ describe('BinaryTree', function () {
180
180
tree . add ( 2 ) ;
181
181
tree . add ( 1 ) ;
182
182
expect ( tree . search ( 2 ) ) . not . toBeNull ( ) ;
183
- tree . delete ( 2 ) ;
183
+ tree . remove ( 2 ) ;
184
184
expect ( tree . search ( 2 ) ) . toBeNull ( ) ;
185
185
expect ( tree . root . value ) . toEqual ( 1 ) ;
186
186
} ) ;
@@ -190,7 +190,7 @@ describe('BinaryTree', function () {
190
190
tree . add ( 1 ) ;
191
191
tree . add ( 2 ) ;
192
192
expect ( tree . search ( 1 ) ) . not . toBeNull ( ) ;
193
- tree . delete ( 1 ) ;
193
+ tree . remove ( 1 ) ;
194
194
expect ( tree . search ( 1 ) ) . toBeNull ( ) ;
195
195
expect ( tree . root . value ) . toEqual ( 2 ) ;
196
196
} ) ;
0 commit comments