public void DestroyNode(AVLNode node, int value) { if (node.left == null && node.right == null && node.value == value) { } }
public int GetWeight(AVLNode node) { if (node != null) { return GetWeight(node.left) + 1 + GetWeight(node.right); } else return 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; }
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); } }
private void SetHeight(AVLNode node) => node.height = 1 + Math.Max(GetHeight(node.right), GetHeight(node.left));
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); } }
public void Insert(int value) { root = Insert(root, value); }
private int BalanceFactor(AVLNode node) { return((node == null) ? 0 : height(node.leftChild) - height(node.rightChild)); }
private bool IsRightHeavy(AVLNode node) { return(BalanceFactor(node) < -1); }
private bool IsLeftHeavy(AVLNode node) { return(BalanceFactor(node) > 1); }
private bool IsPerfect(AVLNode node) { return((node.leftChild.height) == (node.rightChild.height) && node.leftChild.size == node.rightChild.size); }
public void Insert(int val) => root = Insert(val, root);
private int GetBalanceFactor(AVLNode node) => (node == null) ? 0 : GetHeight(node.left) - GetHeight(node.right);
private bool IsLeftHeavy(AVLNode node) => GetBalanceFactor(node) > 1;
public AVLNode FindMin(AVLNode node) { while (node.left != null) node = node.left; return node; }
public AVLNode FindMax(AVLNode node) { while (node.right != null) node = node.right; return node; }
private int Size(AVLNode node) { return(node == null ? 1 : node.size); }
public void AddRoot(int value) { root = new AVLNode(value); }
private void SetSize(AVLNode node) { node.size = Size(node.leftChild) + Size(node.rightChild); }
private bool IsRightHeavy(AVLNode node) => GetBalanceFactor(node) < -1;
public void NewAVLTree() { root = null; }
private int GetHeight(AVLNode node) => (node == null) ? -1 : node.height;
private void SetHeight(AVLNode node) { node.height = Math.Max(height(node.leftChild), height(node.rightChild)) + 1; }
public AVLNode(int value) { this.value = value; this.left = null; this.right = null; }
private int Height(AVLNode node) { return(node == null ? -1 : node.height); }