Example #1
0
        public void DestroyNode(AVLNode node, int value)
        {
            if (node.left == null && node.right == null && node.value == value)
            {

            }
        }
Example #2
0
 public int GetWeight(AVLNode node)
 {
     if (node != null)
     {
         return GetWeight(node.left) + 1 + GetWeight(node.right);
     }
     else
         return 0;
 }
Example #3
0
 public int GetHeight(AVLNode node)
 {
     if (node != null)
     {
         int leftHeight = GetHeight(node.left);
         int rightHeight = GetHeight(node.right);
         return Math.Max(leftHeight, rightHeight) + 1;
     }
     else
         return 0;
 }
Example #4
0
 public void AddNode(AVLNode node, int value)
 {
     // Goes to right
     if (value > node.value)
     {
         if (node.right == null)
             node.right = new AVLNode(value);
         else
             AddNode(node.right, value);
     }
     // Goes to left
     else if (value < node.value)
     {
         if (node.left == null)
             node.left = new AVLNode(value);
         else
             AddNode(node.left, value);
     }
 }
Example #5
0
 private void SetHeight(AVLNode node) => node.height = 1 + Math.Max(GetHeight(node.right), GetHeight(node.left));
Example #6
0
        private void RefreshAVLTreeHelper(int x, int y, AVLNode aux, Graphics g, int factor)
        {
            Pen nodePen = new Pen(nodeColor);
            Pen linePen = new Pen(lineColor);
            if (aux == null)
                return;
            else
            {
                string value = aux.value.ToString();
                Font font = new Font("Arial", 8);
                SolidBrush solidbrush = new SolidBrush(nodeColor);

                g.DrawEllipse(nodePen, x, y, size, size);
                //g.FillEllipse(solidbrush, x, y, size, size);
                g.DrawString(value, font, solidbrush, x, y - size);

                RefreshAVLTreeHelper(x - factor, y + 30, aux.left, g, factor / 2);
                RefreshAVLTreeHelper(x + factor, y + 30, aux.right, g, factor / 2);
                if (aux.left != null)
                    g.DrawLine(linePen, x + 7, y + size, x - factor + 7, y + 30);
                if (aux.right != null)
                    g.DrawLine(linePen, x + 7, y + size, x + factor + 7, y + 30);
            }
        }
Example #7
0
 public void Insert(int value)
 {
     root = Insert(root, value);
 }
Example #8
0
 private int BalanceFactor(AVLNode node)
 {
     return((node == null) ? 0 : height(node.leftChild) - height(node.rightChild));
 }
Example #9
0
 private bool IsRightHeavy(AVLNode node)
 {
     return(BalanceFactor(node) < -1);
 }
Example #10
0
 private bool IsLeftHeavy(AVLNode node)
 {
     return(BalanceFactor(node) > 1);
 }
Example #11
0
 private bool IsPerfect(AVLNode node)
 {
     return((node.leftChild.height) == (node.rightChild.height) &&
            node.leftChild.size == node.rightChild.size);
 }
Example #12
0
 public void Insert(int val) => root = Insert(val, root);
Example #13
0
 private int GetBalanceFactor(AVLNode node) => (node == null) ? 0 : GetHeight(node.left) - GetHeight(node.right);
Example #14
0
 private bool IsLeftHeavy(AVLNode node) => GetBalanceFactor(node) > 1;
Example #15
0
 public AVLNode FindMin(AVLNode node)
 {
     while (node.left != null)
         node = node.left;
     return node;
 }
Example #16
0
 public AVLNode FindMax(AVLNode node)
 {
     while (node.right != null)
         node = node.right;
     return node;
 }
Example #17
0
 private int Size(AVLNode node)
 {
     return(node == null ? 1 : node.size);
 }
Example #18
0
 public void AddRoot(int value)
 {
     root = new AVLNode(value);
 }
Example #19
0
 private void SetSize(AVLNode node)
 {
     node.size = Size(node.leftChild) + Size(node.rightChild);
 }
Example #20
0
 private bool IsRightHeavy(AVLNode node) => GetBalanceFactor(node) < -1;
Example #21
0
 public void NewAVLTree()
 {
     root = null;
 }
Example #22
0
 private int GetHeight(AVLNode node) => (node == null) ? -1 : node.height;
Example #23
0
 private void SetHeight(AVLNode node)
 {
     node.height = Math.Max(height(node.leftChild), height(node.rightChild)) + 1;
 }
Example #24
0
 public AVLNode(int value)
 {
     this.value = value;
     this.left = null;
     this.right = null;
 }
Example #25
0
 private int Height(AVLNode node)
 {
     return(node == null ? -1 : node.height);
 }