@@ -201,217 +201,4 @@ private string TraversePo(Node nodeP)
201201
202202 public abstract void Insert ( T dataP ) ;
203203 public abstract bool Delete ( T dataP ) ;
204-
205- // The ToString method is simply here to help us debug.
206- // It is not really pretty, but using pre-order and spaces
207- // to make it easier to understand how the tree is
208- // constructed. It also displays the height of the tree.
209-
210- public override string ToString ( )
211- {
212- string returned = "Tree height: " + Height ( ) + "\n " ;
213- int tBalance = SubtreeBalance ( root ) ;
214- returned += "Tree balance: " + tBalance ;
215- if ( tBalance <= - 2 )
216- {
217- returned += " (Right-heavy)" ;
218- }
219- else if ( tBalance >= 2 )
220- {
221- returned += " (Left-heavy)" ;
222- }
223- returned += "\n " ;
224-
225- if ( root != null )
226- {
227- returned += Stringify ( root , 0 ) ;
228- }
229- return returned ;
230- }
231-
232- private string Stringify ( Node nodeP , int height )
233- {
234- string returned = "" ;
235- if ( nodeP != null )
236- {
237- for ( int i = 0 ; i < height ; i ++ )
238- {
239- returned += " " ;
240- }
241- returned += nodeP + "\n " ; // Calls Node's ToString method.
242- if ( nodeP . left != null )
243- {
244- returned +=
245- "L"
246- + " (h: "
247- + nodeP . left . Height
248- + ")"
249- + Stringify ( nodeP . left , height + 1 ) ;
250- }
251- if ( nodeP . right != null )
252- {
253- returned +=
254- "R"
255- + " (h: "
256- + nodeP . right . Height
257- + ")"
258- + Stringify ( nodeP . right , height + 1 ) ;
259- }
260- }
261- return returned ;
262- }
263-
264- protected int SubtreeBalance ( Node nodeP )
265- {
266- // Will return
267- // a negative number if subtree is right-heavy
268- // a positive number if subtree is left-heavy
269- // 0 if the subtree is perfectly balanced.
270- // The AVL tree will need to be re-balanced if the value
271- // returned is greater than or equal to 2, or
272- // less than or equal to -2.
273- // Stated differently, if the value returned is
274- // -1, 0 or 1, then no re-balancing will take place.
275- int balance ;
276- if ( nodeP == null )
277- {
278- balance = 0 ;
279- }
280- else if ( nodeP . left == null && nodeP . right == null )
281- {
282- balance = 0 ;
283- }
284- else if ( nodeP . left == null )
285- {
286- balance = - ( nodeP . right . Height + 1 ) ;
287- }
288- else if ( nodeP . right == null )
289- {
290- balance = nodeP . left . Height + 1 ;
291- }
292- else
293- {
294- balance = nodeP . left . Height - nodeP . right . Height ;
295- }
296- return balance ;
297- }
298-
299- /*
300- * Before
301- * nodeTop --> A
302- * / \
303- * nodeLeft--> B C
304- * / \
305- * D E <-- nodeLeft.right
306- *
307- * After
308- * B
309- * / \
310- * D A
311- * / \
312- * E C
313- */
314- protected Node RotateleftChild ( Node nodeTop ) // Aka left-left rotation
315- {
316- Node nodeLeft = nodeTop . left ;
317- nodeTop . left = nodeLeft . right ;
318- nodeLeft . right = nodeTop ;
319- return nodeLeft ; // attached to caller as the new top of this subtree
320- }
321-
322- /*
323- * Before
324- * nodeTop --> A
325- * / \
326- * B C <-- nodeRight
327- * / \
328- * D E
329- *
330- * After
331- * C
332- * / \
333- * A E
334- * / \
335- * B D
336- */
337- protected Node RotaterightChild ( Node nodeTop ) // Aka right-right rotation
338- {
339- Node nodeRight = nodeTop . right ;
340- nodeTop . right = nodeRight . left ;
341- nodeRight . left = nodeTop ;
342- return nodeRight ; // attached to caller as the new top of this subtree
343- }
344-
345- /*
346- * Before
347- * nodeP --> A
348- * / \
349- * B C
350- * / \ / \
351- * D E F G
352- *
353- * After RotaterightChild
354- * A
355- * / \
356- * E C
357- * / / \
358- * B F G
359- * /
360- * D
361- *
362- * After
363- * E
364- * / \
365- * B A
366- * / \
367- * D C
368- * / \
369- * F G
370- */
371-
372- protected Node DoubleleftChild ( Node nodeP )
373- {
374- nodeP . left = RotaterightChild ( nodeP . left ) ;
375- return RotateleftChild ( nodeP ) ;
376- }
377-
378- protected Node DoublerightChild ( Node nodeP )
379- {
380- nodeP . right = RotateleftChild ( nodeP . right ) ;
381- return RotaterightChild ( nodeP ) ;
382- }
383-
384- // Helper methods, to demonstrate
385- // tree rotations.
386- public void Doubleleft ( )
387- {
388- if ( root != null )
389- {
390- root = DoubleleftChild ( root ) ;
391- }
392- }
393-
394- public void Doubleright ( )
395- {
396- if ( root != null )
397- {
398- root = DoublerightChild ( root ) ;
399- }
400- }
401-
402- public void Rotateright ( )
403- {
404- if ( root != null )
405- {
406- root = RotaterightChild ( root ) ;
407- }
408- }
409-
410- public void Rotateleft ( )
411- {
412- if ( root != null )
413- {
414- root = RotateleftChild ( root ) ;
415- }
416- }
417204}
0 commit comments