1
- import { BinaryTree } from "../ts/BinaryTree" ;
1
+ import { BinaryTree } from "../ts/BinaryTree/BinaryTree " ;
2
2
3
3
describe ( 'BinaryTree' , function ( ) {
4
4
describe ( 'add' , function ( ) {
5
5
it ( 'should be able to add node' , function ( ) {
6
- var tree = new BinaryTree < number > ( ) ;
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
12
it ( 'should be able to add node' , function ( ) {
13
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
13
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
14
14
tree . add ( 5 ) ;
15
15
expect ( tree . root . value ) . toEqual ( 5 ) ;
16
16
tree . add ( 6 ) ;
@@ -21,7 +21,7 @@ describe('BinaryTree', function () {
21
21
} ) ;
22
22
23
23
it ( 'should be able to add node to binary tree with object values' , function ( ) {
24
- var tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
24
+ const tree = new BinaryTree < any > ( function ( a , b ) { return 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 } ) ;
@@ -31,7 +31,7 @@ describe('BinaryTree', function () {
31
31
} ) ;
32
32
33
33
it ( 'should be able to walk the binary tree in order' , function ( ) {
34
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
34
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
35
35
tree . add ( 5 ) ;
36
36
tree . add ( 6 ) ;
37
37
tree . add ( 4 ) ;
@@ -44,7 +44,7 @@ describe('BinaryTree', function () {
44
44
} ) ;
45
45
46
46
it ( 'should be able to walk the binary tree that has object values in order' , function ( ) {
47
- var tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
47
+ const tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
48
48
tree . add ( { amount : 6 } ) ;
49
49
tree . add ( { amount : 4 } ) ;
50
50
tree . add ( { amount : 5 } ) ;
@@ -58,7 +58,7 @@ describe('BinaryTree', function () {
58
58
59
59
60
60
it ( 'should be able to walk the binary tree in order' , function ( ) {
61
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
61
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
62
62
tree . add ( 5 ) ;
63
63
tree . add ( 6 ) ;
64
64
tree . add ( 4 ) ;
@@ -71,7 +71,7 @@ describe('BinaryTree', function () {
71
71
} ) ;
72
72
73
73
it ( 'should be able to walk the binary tree in reverseorder' , function ( ) {
74
- var tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
74
+ const tree = new BinaryTree < any > ( function ( a , b ) { return a . amount > b . amount ; } ) ;
75
75
tree . add ( { amount : 6 } ) ;
76
76
tree . add ( { amount : 4 } ) ;
77
77
tree . add ( { amount : 5 } ) ;
@@ -85,20 +85,20 @@ describe('BinaryTree', function () {
85
85
86
86
describe ( ' search' , function ( ) {
87
87
it ( 'should return null when no root' , function ( ) {
88
- var tree = new BinaryTree < any > ( function ( a , b ) { return a > b ; } ) ;
88
+ const tree = new BinaryTree < any > ( function ( a , b ) { return a > b ; } ) ;
89
89
expect ( tree . search ( 5 ) ) . toBeNull ( ) ;
90
90
} ) ;
91
91
92
92
it ( 'should return element when found' , function ( ) {
93
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
93
+ const tree = new BinaryTree < number > ( function ( a , b ) { return 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
100
it ( 'should return null when no element was found' , function ( ) {
101
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
101
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
102
102
tree . add ( 5 ) ;
103
103
expect ( tree . search ( 23 ) ) . toBeNull ( ) ;
104
104
tree . add ( 6 ) ;
@@ -107,7 +107,7 @@ describe('BinaryTree', function () {
107
107
} ) ;
108
108
109
109
it ( 'should be able to get max value' , function ( ) {
110
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
110
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
111
111
expect ( tree . min ( ) ) . toBeNull ( ) ;
112
112
tree . add ( 1 ) ;
113
113
tree . add ( 2 ) ;
@@ -117,7 +117,7 @@ describe('BinaryTree', function () {
117
117
} ) ;
118
118
119
119
it ( 'should be able to get min value' , function ( ) {
120
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
120
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
121
121
expect ( tree . max ( ) ) . toBeNull ( ) ;
122
122
tree . add ( 1 ) ;
123
123
tree . add ( 2 ) ;
@@ -127,56 +127,56 @@ describe('BinaryTree', function () {
127
127
} ) ;
128
128
129
129
it ( 'should be able to get node successor' , function ( ) {
130
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
130
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
131
131
tree . add ( 1 ) ;
132
132
tree . add ( 2 ) ;
133
133
tree . add ( 4 ) ;
134
134
tree . add ( 5 ) ;
135
- expect ( tree . sucessor ( 2 ) . value ) . toEqual ( 4 ) ;
135
+ expect ( tree . successor ( 2 ) . value ) . toEqual ( 4 ) ;
136
136
} ) ;
137
137
138
138
it ( 'should be able to get node successor when it doesn\'t have right child' , function ( ) {
139
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
139
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
140
140
tree . add ( 1 ) ;
141
141
tree . add ( 10 ) ;
142
142
tree . add ( 9 ) ;
143
143
tree . add ( 8 ) ;
144
- expect ( tree . sucessor ( 8 ) . value ) . toEqual ( 9 ) ;
145
- expect ( tree . sucessor ( 9 ) . value ) . toEqual ( 10 ) ;
144
+ expect ( tree . successor ( 8 ) . value ) . toEqual ( 9 ) ;
145
+ expect ( tree . successor ( 9 ) . value ) . toEqual ( 10 ) ;
146
146
} ) ;
147
147
148
148
it ( 'should return null for node successor if tree is empty' , function ( ) {
149
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
150
- expect ( tree . sucessor ( 2 ) ) . toBeNull ( ) ;
149
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
150
+ expect ( tree . successor ( 2 ) ) . toBeNull ( ) ;
151
151
} ) ;
152
152
153
153
it ( 'should return null if there is no successor' , function ( ) {
154
- var tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
154
+ const tree = new BinaryTree < number > ( function ( a , b ) { return a > b ; } ) ;
155
155
tree . add ( 1 ) ;
156
- expect ( tree . sucessor ( 1 ) ) . toBeNull ( ) ;
156
+ expect ( tree . successor ( 1 ) ) . toBeNull ( ) ;
157
157
tree . add ( 2 ) ;
158
158
tree . add ( 4 ) ;
159
159
tree . add ( 5 ) ;
160
- expect ( tree . sucessor ( 5 ) ) . toBeNull ( ) ;
161
- expect ( tree . sucessor ( 4 ) . value ) . toEqual ( 5 ) ;
160
+ expect ( tree . successor ( 5 ) ) . toBeNull ( ) ;
161
+ expect ( tree . successor ( 4 ) . value ) . toEqual ( 5 ) ;
162
162
} ) ;
163
163
164
164
describe ( 'delete' , function ( ) {
165
165
it ( `should return false if node was not found` , function ( ) {
166
- var tree = new BinaryTree < number > ( ) ;
166
+ const tree = new BinaryTree < number > ( ) ;
167
167
expect ( tree . delete ( 1 ) ) . toEqual ( false ) ;
168
168
} ) ;
169
169
170
170
it ( `should remove node if it doesn't have children` , function ( ) {
171
- var tree = new BinaryTree < number > ( ) ;
171
+ const tree = new BinaryTree < number > ( ) ;
172
172
tree . add ( 1 ) ;
173
173
expect ( tree . search ( 1 ) ) . not . toBeNull ( ) ;
174
174
tree . delete ( 1 ) ;
175
175
expect ( tree . search ( 1 ) ) . toBeNull ( ) ;
176
176
} ) ;
177
177
178
178
it ( `should move the left child up if the node to remove has only left child and no right child` , function ( ) {
179
- var tree = new BinaryTree < number > ( ) ;
179
+ const tree = new BinaryTree < number > ( ) ;
180
180
tree . add ( 2 ) ;
181
181
tree . add ( 1 ) ;
182
182
expect ( tree . search ( 2 ) ) . not . toBeNull ( ) ;
@@ -186,7 +186,7 @@ describe('BinaryTree', function () {
186
186
} ) ;
187
187
188
188
it ( `should move the right child up if the node to remove has only right child and no left child` , function ( ) {
189
- var tree = new BinaryTree < number > ( ) ;
189
+ const tree = new BinaryTree < number > ( ) ;
190
190
tree . add ( 1 ) ;
191
191
tree . add ( 2 ) ;
192
192
expect ( tree . search ( 1 ) ) . not . toBeNull ( ) ;
0 commit comments