-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_TreeClass.java
443 lines (382 loc) · 11.3 KB
/
AVL_TreeClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package p9_package;
public class AVL_TreeClass
{
private class Node
{
private char data;
private Node rightChildRef;
private Node leftChildRef;
Node( Node copied )
{
//copy constructor for AVL tree node
data = copied.data;
leftChildRef = null;
rightChildRef = null;
}
Node(char inData)
{
data = inData;
leftChildRef = null;
rightChildRef = null;
}
Node(char inData, AVL_TreeClass.Node leftRef, AVL_TreeClass.Node rightRef)
{
//Initialization constructor for data and child references
data = inData;
leftChildRef = leftRef;
rightChildRef = rightRef;
}
}
private static char DASH = 45;
private final char NULL_CHAR = 0;
private boolean rowStartFlag;
private static final char SPACE = 32;
private Node treeRoot;
public AVL_TreeClass()
{
this.treeRoot = null;
rowStartFlag = false;
//Default class constructor
}
public AVL_TreeClass( AVL_TreeClass copied)
{
this.treeRoot = copyConstructorHelper(copied.treeRoot);
rowStartFlag = false;
}
public void clearTree()
{
this.treeRoot = null;
}
private Node copyConstructorHelper(Node wkgRef)
{
//Note: Uses preorder strategy to copy nodes
if(wkgRef !=null)
{
Node node = wkgRef;
node.leftChildRef = copyConstructorHelper(wkgRef.leftChildRef);
node.rightChildRef = copyConstructorHelper(wkgRef.rightChildRef);
return node;
}
return null;
}
private void displayChars(int numChars,
char outChar)
{
if(numChars > 0)
{
System.out.print(outChar);
displayChars(numChars -1, outChar);
}
}
public int findTreeHeight()
{
return getTreeHeight(treeRoot);
}
private int getBalanceFactor(Node wkgLocalRef)
{
// /gets balance factor indicating if tree is unbalanced from given root down
//The balance factor is found by subtracting the left child's height from the
//right child's height.
if(wkgLocalRef == null)
{
return 0;
}
return getTreeHeight(wkgLocalRef.rightChildRef)-
getTreeHeight(wkgLocalRef.leftChildRef);
}
private int getMax(int one,
int other)
{
//Finds maximum of two given numbers
if(one > other)
{
return one;
}
else
{
return other;
}
}
private int getTreeHeight(Node wkgLocalRef)
{
//Tree height helper method
if(wkgLocalRef != null)
{
int leftHeight = getTreeHeight(wkgLocalRef.leftChildRef);
int rightHeight = getTreeHeight(wkgLocalRef.rightChildRef);
//uses getMax
if(getMax(leftHeight, rightHeight) ==
leftHeight)
{
return leftHeight + 1;
}
else
{
return rightHeight + 1;
}
}
return -1;
}
public void inOrderDisplay()
{
inOrderDisplayHelper(this.treeRoot);
}
private void inOrderDisplayHelper(AVL_TreeClass.Node wkgLocalRef)
{
if(wkgLocalRef != null)
{
//go left
inOrderDisplayHelper(wkgLocalRef.leftChildRef);
System.out.print(wkgLocalRef.data);
//go right
inOrderDisplayHelper(wkgLocalRef.rightChildRef);
}
}
public void insert(char inData)
{
treeRoot = insertHelper(treeRoot, inData);
}
private Node insertHelper(Node wkgLocalRef,
char inData)
{
if(wkgLocalRef != null)
{
if( wkgLocalRef.data > inData)
{
wkgLocalRef.leftChildRef = insertHelper(wkgLocalRef.leftChildRef, inData);
}
else if(wkgLocalRef.data < inData)
{
wkgLocalRef.rightChildRef = insertHelper(wkgLocalRef.rightChildRef, inData);
}
else
{
wkgLocalRef.data = inData;
}
}
else
{
wkgLocalRef = new Node(inData);
//find balance factor and assing it to a variable
int balanceFactor = getBalanceFactor(wkgLocalRef);
//Once the balance factor is found, there are five possible conditions:
//If the balance factor is not more than one
//or less than negative one,
//nothing happens and the method completes
if( balanceFactor != 0)
{
//If the balance factor is greater than one,
//and the new data is less than its parent data,
//there is a Left Left condition
if(balanceFactor > 1 && inData < wkgLocalRef.data)
{
//there is a Left Left condition
wkgLocalRef = rotateLeft(rotateLeft(wkgLocalRef));
}
//If the balance factor is less than negative one,
//and the new data is greater than its parent data,
else if(balanceFactor < -1 && inData > wkgLocalRef.data)
{
//there is a Right Right condition
wkgLocalRef = rotateRight(rotateRight(wkgLocalRef));
}
//If the balance factor is greater than one,
//and the new data is greater than its parent data,
else if(balanceFactor > 1 && inData > wkgLocalRef.data)
{
//there is a Left Right condition
wkgLocalRef = rotateLeft(rotateRight(wkgLocalRef));
}
//If the balance factor is less than negative one,
//and the new data is less than its parent data,
else if(balanceFactor < -1 && inData < wkgLocalRef.data)
{
//there is a Right Left condition
wkgLocalRef = rotateRight(rotateLeft(wkgLocalRef));
}
}
}
return wkgLocalRef;
}
public boolean isEmpty()
{
return treeRoot == null;
}
private Node rotateLeft(Node wkgLocalRef)
{
//assign the right childs left child to temporary
Node newParent = wkgLocalRef.rightChildRef;
Node subTree = newParent.leftChildRef;
newParent.leftChildRef = wkgLocalRef;
wkgLocalRef.rightChildRef = subTree;
displayTreeStructure();
return newParent;
}
private Node rotateRight(Node wkgLocalRef)
{
// assign the left child right child to a temporary
Node newParent = wkgLocalRef.leftChildRef;
Node subTree = newParent.rightChildRef;
//assign the reference tof hte node to left nodes right child./
newParent.rightChildRef = wkgLocalRef;
//assing temporary to left child
wkgLocalRef.leftChildRef = subTree;
displayTreeStructure();
return newParent;
}
public char search(char searchData)
{
//Searches for data in AVL Tree given char with necessary key
return searchHelper(treeRoot, searchData);
}
private char searchHelper(AVL_TreeClass.Node wkgLocalRef,
char searchData)
{
if(wkgLocalRef != null)
{
if(wkgLocalRef.data > searchData)
{
return searchHelper(wkgLocalRef.leftChildRef, searchData);
}
else if(wkgLocalRef.data < searchData)
{
return searchHelper(wkgLocalRef.rightChildRef, searchData);
}
return wkgLocalRef.data;
}
return NULL_CHAR;
}
private int toPower(int base,
int exponent)
{
//Local recursive method to calculate exponentiation with integers
if(exponent > 0)
{
return base * toPower(base, exponent -1);
}
return 1;
}
/**
* [NOT ASSIGNED] Displays text-graphical representation of one level/line
* of the AVL tree
*
* @param workingNode node reference at current recursive level
*
* @param nodeHeight height of tree plus two
* for current height of nodes, including lowermost null children
*
* @param displayLevel level of tree at which the current line
* of display is to be presented
*
* @param workingLevel current level during recursive actions
*/
private void displayAtTreeLevel( Node workingNode, int nodeHeight,
int displayLevel, int workingLevel )
{
char charOut = workingNode.data;
if( workingLevel == displayLevel )
{
displayValue( charOut, nodeHeight, workingLevel );
return;
}
if( workingNode.leftChildRef != null )
{
displayAtTreeLevel( workingNode.leftChildRef, nodeHeight,
displayLevel, workingLevel + 1 );
}
else
{
displayEmptyNodeSpaces( nodeHeight, displayLevel, workingLevel + 1 );
}
if( workingNode.rightChildRef != null )
{
displayAtTreeLevel( workingNode.rightChildRef, nodeHeight,
displayLevel, workingLevel + 1 );
}
else
{
displayEmptyNodeSpaces( nodeHeight, displayLevel, workingLevel + 1 );
}
}
/**
* [NOT ASSIGNED] Method that displays null or blank nodes
* for a tree at null locations
* <p>
* Note: used by displayAtTreeLevel
*
* @param nodeHeight height of tree plus two
* for current height of nodes, including lowermost null children
*
* @param displayLevel level of the tree at which
* the display will be applied
*
* @param workingLevel level of tree just below
* non-null node at which method is currently working
*/
private void displayEmptyNodeSpaces( int nodeHeight,
int displayLevel, int workingLevel )
{
int nodesToDisplay = toPower( 2, displayLevel - workingLevel );
char charOut = SPACE;
if( displayLevel == workingLevel )
{
charOut = DASH;
}
while( nodesToDisplay > 0 )
{
displayValue( charOut, nodeHeight, displayLevel );
nodesToDisplay--;
}
}
/**
* [NOT ASSIGNED] Displays text-graphical representation of AVL tree
*
*/
public void displayTreeStructure()
{
int displayLevel, nodeHeight = getTreeHeight( treeRoot ) + 2;
int workingLevel = 1;
if( treeRoot != null )
{
for( displayLevel = 1; displayLevel <= nodeHeight; displayLevel++ )
{
rowStartFlag = true;
displayAtTreeLevel( treeRoot, nodeHeight,
displayLevel, workingLevel );
System.out.println();
}
}
else
{
System.out.println( "\nEmpty Tree - No Display");
}
}
/**
* [NOT ASSIGNED] Method used to display a character or color letter
* along with calculated leading spaces
* <p>
* Note: used in displayAtTreeLevel and displayEmptyNodeSpaces
*
* @param data data value to display, either letter or color data
*
* @param nodeHeight height of tree plus two
* for current height of nodes, including lowermost null children
*
* @param workingLevel current level during recursive actions
*/
private void displayValue( char data, int nodeHeight, int workingLevel )
{
int leadingSpaces;
if( rowStartFlag )
{
leadingSpaces = toPower( 2, nodeHeight - workingLevel );
rowStartFlag = false;
}
else
{
leadingSpaces = toPower( 2, nodeHeight - workingLevel + 1 ) - 1;
}
displayChars( leadingSpaces, SPACE );
System.out.print( data );
}
}