Ejemplo n.º 1
0
 /// <summary>
 ///     private helper method: Calculate the subtree height
 ///     this method takes the direct child's height property and return the maximum height
 /// </summary>
 /// <param name="node"> the node needs to get its subtree height </param>
 /// <returns> returns the height of the subtree</returns>
 private int GetSubtreeHeight(AvlNode <T> node)
 {
     if (node.IsLeafNode())
     {
         return(0);                   //no nodes below, height is 0 below
     }
     //two child, return the maximum height between left and right subtree.
     if (!node.HasOneChild())
     {
         return(Math.Max(node.Left.Height, node.Right.Height));
     }
     //Only one child, return that child's height
     return(node.Left?.Height ?? node.Right.Height);
 }
Ejemplo n.º 2
0
 /// <summary>
 ///     private helper method: calculate the balancing factor of given node
 /// </summary>
 /// <param name="node"> node need to calculate balancing factor</param>
 /// <returns></returns>
 private int GetBalancingFactor(AvlNode <T> node)
 {
     //no child, balancing factor is 0
     if (node.IsLeafNode())
     {
         return(0);
     }
     //has two child, balancing factor is height of left subtree - height of right subtree
     if (!node.HasOneChild())
     {
         return(node.Left.Height - node.Right.Height);
     }
     //only one child, return that child's height
     if (node.Left != null) //child on the left
     {
         return(node.Left.Height);
     }
     return(0 - node.Right.Height); //child on the right
 }