private Node_AVL_Tree balance_tree(Node_AVL_Tree current) { int b_factor = balance_factor(current); if (b_factor > 1) { if (balance_factor(current.left) > 0) { current = RotateLL(current); } else { current = RotateLR(current); } } else if (b_factor < -1) { if (balance_factor(current.right) > 0) { current = RotateRL(current); } else { current = RotateRR(current); } } return(current); }
private Node_AVL_Tree RotateLR(Node_AVL_Tree parent) { Node_AVL_Tree pivot = parent.left; parent.left = RotateRR(pivot); return(RotateLL(parent)); }
private Node_AVL_Tree RotateRL(Node_AVL_Tree parent) { Node_AVL_Tree pivot = parent.right; parent.right = RotateLL(pivot); return(RotateRR(parent)); }
private Node_AVL_Tree Find(int target, Node_AVL_Tree current) { if (current == null) { return(null); } if (target < current.data) { if (target == current.data) { return(current); } else { return(Find(target, current.left)); } } else { if (target == current.data) { return(current); } else { return(Find(target, current.right)); } } }
private Node_AVL_Tree RotateLL(Node_AVL_Tree parent) { Node_AVL_Tree pivot = parent.left; parent.left = pivot.right; pivot.right = parent; return(pivot); }
private int balance_factor(Node_AVL_Tree current) { int l = getHeight(current.left); int r = getHeight(current.right); int b_factor = l - r; return(b_factor); }
private void InOrderDisplayTree(Node_AVL_Tree current) { if (current != null) { InOrderDisplayTree(current.left); Debug.Log("(" + current.data + ") "); InOrderDisplayTree(current.right); } }
public void Add(/*int data,*/ Edge_IA e) { Node_AVL_Tree newItem = new Node_AVL_Tree(/*data,*/ e); if (root == null) { root = newItem; } else { root = RecursiveInsert(root, newItem); } }
private int getHeight(Node_AVL_Tree current) { int height = 0; if (current != null) { int l = getHeight(current.left); int r = getHeight(current.right); int m = max(l, r); height = m + 1; } return(height); }
private Node_AVL_Tree RecursiveInsert(Node_AVL_Tree current, Node_AVL_Tree n) { if (current == null) { current = n; return(current); } else if (n.data < current.data) { current.left = RecursiveInsert(current.left, n); current = balance_tree(current); } else if (n.data > current.data) { current.right = RecursiveInsert(current.right, n); current = balance_tree(current); } return(current); }
public Node_AVL_Tree Find(int key) { if (root == null) { return(null); } Node_AVL_Tree n = Find(key, root); if (n == null) { return(null); } if (/*Find(key, root).data*/ n.data == key) { Debug.Log(key + " was found!"); return(n);//Find(key, root); } else { Debug.Log("Nothing found!"); return(null); } }
public void Delete(int target) {//and here root = Delete(root, target); }
private Node_AVL_Tree Delete(Node_AVL_Tree current, int target) { Node_AVL_Tree parent; if (current == null) { return(null); } else { //left subtree if (target < current.data) { current.left = Delete(current.left, target); if (balance_factor(current) == -2)//here { if (balance_factor(current.right) <= 0) { current = RotateRR(current); } else { current = RotateRL(current); } } } //right subtree else if (target > current.data) { current.right = Delete(current.right, target); if (balance_factor(current) == 2) { if (balance_factor(current.left) >= 0) { current = RotateLL(current); } else { current = RotateLR(current); } } } //if target is found else { if (current.right != null) { //delete its inorder successor parent = current.right; while (parent.left != null) { parent = parent.left; } current.data = parent.data; current.right = Delete(current.right, parent.data); if (balance_factor(current) == 2)//rebalancing { if (balance_factor(current.left) >= 0) { current = RotateLL(current); } else { current = RotateLR(current); } } } else { //if current.left != null return(current.left); } } } return(current); }