11using System ;
22using System . Collections . Generic ;
33
4- /*
4+ /*
55 * This implementation of AVL tree is
6- * "from scratch", and stores the height
7- * of a node as an attribute instead of
6+ * "from scratch", and stores the height
7+ * of a node as an attribute instead of
88 * re-comptuing it when needed.
9- * This class does not inherit from any
9+ * This class does not inherit from any
1010 * other class and is "standalone".
1111 */
1212
1313public class AVLTree < T >
1414 where T : IComparable < T >
15- // We need T to realize IComparable
16- // so that we can decide where to
17- // insert node, exactly like BSTrees.
15+ // We need T to realize IComparable
16+ // so that we can decide where to
17+ // insert node, exactly like BSTrees.
1818{
1919 private class Node
2020 {
2121 public T Data { get ; set ; }
2222 public Node left ;
2323 public Node right ;
24- // Height is implemented as an attribute
25- // with a property.
24+
25+ // Height is implemented as an attribute
26+ // with a property.
2627 private int height ;
2728 public int Height
2829 {
@@ -110,37 +111,39 @@ private int UpdateHeight(Node nodeP)
110111 return height ;
111112 }
112113
113- // The following will return
114- // a negative number if subtree is right-heavy
115- // a positive number if subtree is left-heavy
116- // 0 if the subtree is perfectly balanced.
114+ // The following will return
115+ // a negative number if subtree is right-heavy
116+ // a positive number if subtree is left-heavy
117+ // 0 if the subtree is perfectly balanced.
117118
118- private int SubtreeBalance ( Node nodeP )
119+ private int SubtreeBalance ( Node nodeP )
119120 {
120121 UpdateHeight ( nodeP . left ) ;
121122 UpdateHeight ( nodeP . right ) ;
122- // The resulting value is essentially
123- // nodeP.left.Height - nodeP.right.Height
124- // but we need to account for null values.
125- int balance = 0 ;
126- if ( ! ( nodeP == null ) &&
127- ! ( nodeP . left == null && nodeP . right == null ) )
128- {
129- // If nodeP is null, or if nodeP has no children,
130- // then balanceP is 0. Otherwise, we compute it:
131- if ( nodeP . left == null )
132- {
133- balance = - ( nodeP . right . Height + 1 ) ;
134- }
135- else if ( nodeP . right == null )
136- {
137- balance = nodeP . left . Height + 1 ;
138- }
139- else
140- {
141- balance = nodeP . left . Height - nodeP . right . Height ;
142- }
143- }
123+ // The resulting value is essentially
124+ // nodeP.left.Height - nodeP.right.Height
125+ // but we need to account for null values.
126+ int balance = 0 ;
127+ if (
128+ ! ( nodeP == null )
129+ && ! ( nodeP . left == null && nodeP . right == null )
130+ )
131+ {
132+ // If nodeP is null, or if nodeP has no children,
133+ // then balanceP is 0. Otherwise, we compute it:
134+ if ( nodeP . left == null )
135+ {
136+ balance = - ( nodeP . right . Height + 1 ) ;
137+ }
138+ else if ( nodeP . right == null )
139+ {
140+ balance = nodeP . left . Height + 1 ;
141+ }
142+ else
143+ {
144+ balance = nodeP . left . Height - nodeP . right . Height ;
145+ }
146+ }
144147 return balance ;
145148 }
146149
@@ -270,10 +273,10 @@ private Node DoublerightChild(Node nodeP)
270273 return RotaterightChild ( nodeP ) ;
271274 }
272275
273- // Note that the following method could also use
274- // SubtreeBalance to compute if the tree needs to be
275- // re-balanced, instead of using GetHeight directly.
276- private Node Insert ( T valueP , Node nodeP )
276+ // Note that the following method could also use
277+ // SubtreeBalance to compute if the tree needs to be
278+ // re-balanced, instead of using GetHeight directly.
279+ private Node Insert ( T valueP , Node nodeP )
277280 {
278281 if ( nodeP == null )
279282 return new Node ( valueP , null , null , 0 ) ;
@@ -330,7 +333,7 @@ private Node Insert(T valueP, Node nodeP)
330333 return nodeP ;
331334 }
332335
333- public bool Remove ( T value )
336+ public bool Remove ( T value )
334337 {
335338 return Remove ( value , ref root ) ;
336339 }
@@ -395,7 +398,7 @@ private bool Remove(T value, ref Node nodeP)
395398 // The ToString method is simply here to help us debug.
396399 // It is not really pretty, but using pre-order and spaces
397400 // to make it easier to understand how the tree is
398- // constructed.
401+ // constructed.
399402
400403 public override string ToString ( )
401404 {
@@ -428,4 +431,4 @@ private string Stringify(Node nodeP, int depth)
428431 }
429432 return returned ;
430433 }
431- }
434+ }
0 commit comments