@@ -35,21 +35,13 @@ int height(){
35
35
}
36
36
37
37
int balance (){
38
- //System.out.println("checking the balance:");
39
38
if (right == null && left == null ){
40
39
return 0 ;
41
40
}else if (right == null ){
42
- // System.out.println("left.height: " + left.height());
43
- // System.out.println("The BALANCE: " + (1+ left.height()));
44
41
return (1 + left .height ());
45
42
}else if (left == null ){
46
- // System.out.println("right.height: " + right.height());
47
- // System.out.println("The BALANCE: " + (-1 -right.height()));
48
43
return (-1 - right .height ());
49
44
}else {
50
- // System.out.println("left.height: " + left.height());
51
- // System.out.println("right.height: " + right.height());
52
- // System.out.println("The BALANCE: " + (left.height() -right.height()));
53
45
return (left .height () - right .height ());
54
46
}
55
47
}
@@ -191,19 +183,19 @@ void insert(int number){
191
183
}
192
184
void preOrder (){
193
185
if (root != null ){
194
- System .out .println ("item: " + root .item );
195
- System .out .print ("balance: " );
196
- System .out .println (root .balance ());
197
- System .out .println ( " height: " + root .height ());
186
+ System .out .print ("item: " + root .item );
187
+ System .out .print (" balance: " );
188
+ System .out .print (root .balance ());
189
+ System .out .print ( " height: " + root .height ());
198
190
if (root .left != null ){
199
- System .out .println ( " left: " + root .left .item );
191
+ System .out .print ( " left: " + root .left .item );
200
192
}else {
201
- System .out .println ( " left: " + null );
193
+ System .out .print ( " left: " + null );
202
194
}
203
195
if (root .right != null ){
204
- System .out .println ("right: " + root .right .item );
196
+ System .out .println (" right: " + root .right .item );
205
197
}else {
206
- System .out .println ("right: " + null );
198
+ System .out .println (" right: " + null );
207
199
}
208
200
AVL aLeft = new AVL ();
209
201
aLeft .root = root .left ;
@@ -218,30 +210,53 @@ public static void main(String[] args) {
218
210
a .insert (8 );
219
211
a .preOrder ();
220
212
System .out .println ("--------------------------------------------" );
221
- System .out .println ("New Insert" );
213
+ System .out .println ("New Insert: " + 11 );
222
214
a .insert (11 );
223
215
a .preOrder ();
224
216
System .out .println ("--------------------------------------------" );
225
- System .out .println ("New Insert" );
217
+ System .out .println ("New Insert: " + 14 );
226
218
a .insert (14 );
227
219
a .preOrder ();
228
- System .out .println ("--------------------------------------------" );
229
- System .out .println ("New Insert" );
220
+ System .out .println ("--------------------------------------------" );
221
+ System .out .println ("New Insert: " + 20 );
230
222
a .insert (20 );
223
+ a .preOrder ();
224
+ System .out .println ("--------------------------------------------" );
225
+ System .out .println ("New Insert: " + 25 );
231
226
a .insert (25 );
227
+ a .preOrder ();
228
+ System .out .println ("--------------------------------------------" );
229
+ System .out .println ("New Insert: " + 10 );
232
230
a .insert (10 );
231
+ a .preOrder ();
232
+ System .out .println ("--------------------------------------------" );
233
+ System .out .println ("New Insert: " + 30 );
233
234
a .insert (30 );
235
+ a .preOrder ();
236
+ System .out .println ("--------------------------------------------" );
237
+ System .out .println ("New Insert: " + 27 );
234
238
a .insert (27 );
239
+ a .preOrder ();
240
+ System .out .println ("--------------------------------------------" );
241
+ System .out .println ("New Insert: " + 18 );
235
242
a .insert (18 );
243
+ a .preOrder ();
244
+ System .out .println ("--------------------------------------------" );
245
+ System .out .println ("New Insert: " + 4 );
236
246
a .insert (4 );
247
+ a .preOrder ();
248
+ System .out .println ("--------------------------------------------" );
249
+ System .out .println ("New Insert: " + 3 );
237
250
a .insert (3 );
251
+ a .preOrder ();
252
+ System .out .println ("--------------------------------------------" );
253
+ System .out .println ("New Insert: " + 2 );
238
254
a .insert (2 );
255
+ a .preOrder ();
256
+ System .out .println ("--------------------------------------------" );
257
+ System .out .println ("New Insert: " + 1 );
239
258
a .insert (1 );
240
259
a .preOrder ();
241
- System .out .println ("last" );
242
- a .root .balance ();
243
- System .out .println ("root balance: " );
244
- System .out .println (a .root .balance ());
245
260
246
261
}
247
262
}
0 commit comments