Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1.  
  2.     class AVLNode
  3.     {
  4.        
  5.         public int value { get; private set; }
  6.  
  7.         public AVLNode lChild { get; private set; }
  8.         public AVLNode rChild { get; private set; }
  9.  
  10.         public int _height { get; private set; }
  11.  
  12.         public AVLNode(int value)
  13.         {
  14.             this.lChild = null;
  15.             this.rChild = null;
  16.             this.value = value;
  17.         }
  18.  
  19.         public static AVLNode insert(AVLNode node, int value)
  20.         {
  21.             if (node == null)
  22.                 node = new AVLNode(value);
  23.  
  24.             else if (node.value > value)
  25.             {
  26.                 node.lChild = insert(node.lChild, value);
  27.                 node = balance(node);
  28.             }
  29.  
  30.             else
  31.             {
  32.                 node.rChild = insert(node.rChild, value);
  33.                 node = balance (node);
  34.             }
  35.  
  36.             return node;
  37.         }
  38.  
  39.  
  40.         // Balance AVL Tree
  41.         public static AVLNode balance(AVLNode temp)
  42.         {
  43.             int bal_factor = difference(temp);
  44.  
  45.             if (bal_factor > 1)
  46.             {
  47.                 if (difference(temp.lChild) > 0)
  48.                     temp = ll_rotation(temp);
  49.  
  50.                 else
  51.                     temp = lr_rotation(temp);
  52.             }
  53.  
  54.             else if (bal_factor < -1)
  55.             {
  56.                 if (difference(temp.rChild) > 0)
  57.                     temp = rl_rotation(temp);
  58.  
  59.                 else
  60.                     temp = rr_rotation(temp);
  61.             }
  62.  
  63.             return temp;
  64.         }
  65.  
  66.         private static int height(AVLNode node)
  67.         {
  68.             int h = 0;
  69.             if (node != null)
  70.             {
  71.                 int l_height = height(node.lChild);
  72.                 int r_height = height(node.rChild);
  73.  
  74.                 int max_height = l_height > r_height ? l_height : r_height;
  75.                 h = max_height + 1;
  76.             }
  77.  
  78.             return h;
  79.         }
  80.  
  81.         private static int difference(AVLNode node)
  82.         {
  83.             int l_height = height(node.lChild);
  84.             int r_height = height(node.rChild);
  85.  
  86.             int b_factor = l_height - r_height;
  87.  
  88.             return b_factor;
  89.         }
  90.  
  91.         // Right - Right Rotation
  92.         private static  AVLNode rr_rotation(AVLNode parent)
  93.         {
  94.             AVLNode temp;
  95.             temp = parent.rChild;
  96.             parent.rChild = temp.lChild;
  97.             temp.lChild = parent;
  98.  
  99.             return temp;
  100.         }
  101.  
  102.         //Left - Left Rotation
  103.         private static AVLNode ll_rotation(AVLNode parent)
  104.         {
  105.             AVLNode temp;
  106.             temp = parent.lChild;
  107.             parent.lChild = temp.rChild;
  108.             temp.rChild = parent;
  109.  
  110.             return temp;
  111.         }
  112.  
  113.         // Left - Right Rotation
  114.         private static AVLNode lr_rotation(AVLNode parent)
  115.         {
  116.             AVLNode temp;
  117.             temp = parent.lChild;
  118.             parent.lChild = rr_rotation(temp);
  119.  
  120.             return ll_rotation(parent);
  121.         }
  122.  
  123.         // Right- Left Rotation
  124.         private static AVLNode rl_rotation(AVLNode parent)
  125.         {
  126.             AVLNode temp;
  127.             temp = parent.rChild;
  128.             parent.rChild = ll_rotation(temp);
  129.  
  130.             return rr_rotation(parent);
  131.         }
  132.  
  133.         public static int setRightSubtree(AVLNode root, int offset, int depth)
  134.         {
  135.             if (root == null) { return 0; }
  136.  
  137.             int width = 3;
  138.             int left  = setRightSubtree(root.lChild, offset,                depth + 1);
  139.             int right = setRightSubtree(root.rChild, offset + left + width, depth + 1);
  140.  
  141.             root.Location = new Point(373 + (offset + left) * 9, 40 * depth + 12);
  142.  
  143.             return left + right + width;
  144.         }
  145.  
  146.         public static int setLeftSubtree(AVLNode root, int offset, int depth)
  147.         {
  148.             if (root == null) { return 0; }
  149.  
  150.             int width = 3;
  151.             int right = setLeftSubtree(root.rChild, offset,                 depth + 1);
  152.             int left  = setLeftSubtree(root.lChild, offset + right + width, depth + 1);
  153.  
  154.             root.Location = new Point(319 - (offset + right) * 9, 40 * depth + 12);
  155.  
  156.             return left + right + width;
  157.         }
  158.  
  159.         public static AVLNode deleteNode(AVLNode node, int key)
  160.         {
  161.             if (node == null)
  162.                 return node;
  163.  
  164.             if ( key < node.value )
  165.                 node.lChild = deleteNode(node.lChild, key);
  166.  
  167.             else if( key > node.value )
  168.                 node.rChild = deleteNode(node.rChild, key);
  169.  
  170.             else
  171.             {
  172.                 if( (node.lChild == null) || (node.rChild == null) )
  173.                 {
  174.                     AVLNode temp = node.lChild != null ? node.lChild : node.rChild;
  175.  
  176.                     if (temp == null)
  177.                     {
  178.                         temp = node;
  179.                         node = null;
  180.                     }
  181.                     else
  182.                      node = temp;
  183.                 }
  184.                 else
  185.                 {
  186.                     AVLNode temp = minValueNode(node.rChild);
  187.                     node.value = temp.value;
  188.                     node.rChild = deleteNode(node.rChild, temp.value);
  189.                 }
  190.             }
  191.  
  192.             if (node == null)
  193.                 return node;
  194.  
  195.             node = balance(node);
  196.  
  197.             return node;
  198.         }
  199.  
  200.         public static AVLNode minValueNode(AVLNode node)
  201.         {
  202.             AVLNode current = node;
  203.  
  204.             while (current.lChild != null)
  205.                 current = current.lChild;
  206.  
  207.             return current;
  208.         }
  209.  
  210.         public static int getBalance(AVLNode N)
  211.         {
  212.             if (N == null)
  213.                 return 0;
  214.  
  215.             return N.lChild._height - N.rChild._height;
  216.         }
  217.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement