private AvlNode Find(int target, AvlNode current) { if (target < current.data) { if (target == current.data) { return(current); } return(Find(target, current.left)); } if (target == current.data) { return(current); } return(Find(target, current.right)); }
private AvlNode RecursiveInsert(AvlNode current, AvlNode n) { if (current == null) { current = n; return(current); } 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); }
private AvlNode Delete(AvlNode current, int target) { AvlNode parent; if (current == null) { return(null); } //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); }
public void Delete(int target) { //and here root = Delete(root, target); }