public Node(int key) { this.key = key; this.height = 1; this.left = null; this.right = null; }
private void RecalcHeight(Node node) { int hLeft = GetHeight(node.left); int hRight = GetHeight(node.right); node.height = (hLeft > hRight ? hLeft : hRight) + 1; }
public int Height(Node n) { if (n == null) return 0; else return Math.Max(Height(n.leftChild), Height(n.rightChild)) + 1; }
private static void BalanceTree(Node nodeToBalance) { if (nodeToBalance.balanceFactor == 2) { if (nodeToBalance.rightChild.balanceFactor == 1) //RR {//do a left rotation Console.WriteLine(nodeToBalance.data + " was RR unbalanced"); //nodeToBalance = LeftRotation(nodeToBalance); } else if (nodeToBalance.rightChild.balanceFactor == -1) //RL {//do a double left rotation Console.WriteLine(nodeToBalance.data + " was RL unbalanced"); //nodeToBalance = LeftRotation(nodeToBalance); //nodeToBalance = LeftRotation(nodeToBalance); } } else { if (nodeToBalance.leftChild.balanceFactor == 1) //LR {//do a right rotation Console.WriteLine(nodeToBalance.data + " was LR unbalanced"); //nodeToBalance = RightRotation(nodeToBalance); } else if (nodeToBalance.leftChild.balanceFactor == -1) //LL {//do a double right rotation Console.WriteLine(nodeToBalance.data + " was LL unbalanced"); //nodeToBalance = RightRotation(nodeToBalance); //nodeToBalance = RightRotation(nodeToBalance); } } }
//recursive node insert public void InsertNode(Node insertedNode, Node parentNode) { if (this.rootNode == null) rootNode = insertedNode; //reference: http://csharp.net-informations.com/string/csharp-string-compare.htm else if (string.Compare(insertedNode.data, parentNode.data) < 0) //if string0 is less than string1 { if (parentNode.leftChild == null) { parentNode.leftChild = insertedNode; insertedNode.parent = parentNode; } else InsertNode(insertedNode, parentNode.leftChild); } else if (string.Compare(insertedNode.data, parentNode.data) > 0) //if string0 is greater than string1 { if (parentNode.rightChild == null) { parentNode.rightChild = insertedNode; insertedNode.parent = parentNode; } else InsertNode(insertedNode, parentNode.rightChild); } else { } //Console.WriteLine("Value already in Tree"); //throw new Exception("Inserted node value equals parent node value, discard value"); }
private Node Balancing(Node node) { RecalcHeight(node); if (GetBalance(node) == 2) { if (GetBalance(node.right) < 0) { node.right = RotateRight(node.right); } return RotateLeft(node); } if (GetBalance(node) == -2) { if (GetBalance(node.left) > 0) { node.left = RotateLeft(node.left); } return RotateRight(node); } return node; }
public static void ReassignHeightsAndBalanceFactors(Node[] nodeList) { //reassign heights and balance factors each time a node is inserted for (int j = 0; j < nodeList.Length; j++) { nodeList[j].height = nodeList[j].Height(nodeList[j]); nodeList[j].balanceFactor = nodeList[j].BalanceFactor(nodeList[j].leftChild, nodeList[j].rightChild); } }
private Node RotateLeft(Node node) { Node root = node.right; node.right = root.left; root.left = node; RecalcHeight(node); RecalcHeight(root); return root; }
public int BalanceFactor(Node leftNode, Node rightNode) { try { return (leftNode.height - rightNode.height); } catch { return 0; } }
public void InOrderTraversal(Node current) { if (current != null) { // Visit the left child... InOrderTraversal(current.leftChild); // Output the value of the current node Console.WriteLine(current.data); // Visit the right child... InOrderTraversal(current.rightChild); } }
public void Delete(Node nodeToDelete, Node[] nodeList) { if (nodeToDelete.rightChild == null) { nodeToDelete = nodeToDelete.leftChild; } else if (nodeToDelete.rightChild.leftChild == null) { nodeToDelete = nodeToDelete.rightChild; } else { Node tempNode = getLeftMost(nodeToDelete.rightChild.leftChild); Delete(nodeToDelete.rightChild.leftChild, nodeList); nodeToDelete.data = tempNode.data; } Program.ReassignHeightsAndBalanceFactors(nodeList); Program.BalanceTreeIfUnbalanced(nodeList); }
private void ll_case(Node parent) { Node grandparent = parent.Parent; Node node = parent.Left; Node right = node.Right; if (parent == Root) { Root = node; Root.Type = TYPE.ROOT; } if (grandparent != null) grandparent.Left = node; if (parent.Type != TYPE.RIGHT) parent.Type = TYPE.RIGHT; parent.Left = right; node.Right = parent; node.Parent = grandparent; if (right != null) { right.Parent = parent; right.Type = TYPE.LEFT; } parent.Parent = node; }
// ---------------------------------- private bool do_find(Node node, double key) { if (node.Key == key) return true; else if (node.Key < key && node.Right != null) return do_find(node.Right, key); else if (node.Key > key && node.Left != null) return do_find(node.Left, key); else return false; }
// ---------------------------------- private void dfs(Node node, int level) { if (node != null) { dfs(node.Right, level + 1); Console.WriteLine(new String(' ', 3 * level) + node.Key); dfs(node.Left, level + 1); } }
private void balancing(Node node) { if (node != null) { if (node.Balance == 2) { if (node.Left != null && node.Left.Balance == -1) lr_case(node); ll_case(node); } else if (node.Balance == -2) { if (node.Right != null && node.Right.Balance == 1) rl_case(node); rr_case(node); } } if (node.Parent != null) balancing(node.Parent); }
// ---------------------------------- // Heigth of the Node // -1 -> root is null public int Height(Node node) { if (node != null) { bool is_left = node.Left == null; bool is_right = node.Right == null; if (!is_left && !is_right) return 1 + Math.Max(Height(node.Left), Height(node.Right)); else if (!is_left && is_right) return 1 + Height(node.Left); else if (is_left && !is_right) return 1 + Height(node.Right); else return 0; } else return -1; }
public void InOrderTravers(Node root) { if (root != null) { InOrderTravers(root.left); Output(root.key); InOrderTravers(root.right); } }
private void update_balance(Node added) { added.UpdateBalance(Balance(added)); if (added.Parent != null) update_balance(added.Parent); }
private void rr_case(Node parent) { Node grandparent = parent.Parent; Node node = parent.Right; Node left = node.Left; if (parent == Root) { Root = node; Root.Type = TYPE.ROOT; } if (grandparent != null) grandparent.Right = node; if (parent.Type != TYPE.LEFT) parent.Type = TYPE.LEFT; parent.Right = left; node.Left = parent; node.Parent = grandparent; if (left != null) { left.Parent = parent; left.Type = TYPE.RIGHT; } parent.Parent = node; }
// ---------------------------------- private void lr_case(Node root) { // From { { A | node | { B | right | C } } | root | .. } // To { { { A | node | B } | right | C } | root | .. } Node node = root.Left; Node right = node.Right; node.Right = right.Left; if (right.Left != null) { right.Left.Parent = node; right.Left = node; right.Left.Type = TYPE.RIGHT; } right.Type = TYPE.LEFT; node.Parent = right; right.Parent = node.Parent; root.Left = right; }
public void PostOrderTravers(Node root) { if (root != null) { PostOrderTravers(root.left); PostOrderTravers(root.right); Output(root.key); } }
private int GetBalance(Node node) { return GetHeight(node.right) - GetHeight(node.left); }
private int GetHeight(Node node) { return node != null ? node.height : 0; }
static void Main(string[] args) { Node root = new Node(4); Avl avlTree = new Avl(); avlTree.Insert(root, 3); avlTree.Insert(root, 5); avlTree.Insert(root, 7); avlTree.Insert(root, 1); avlTree.Insert(root, 6); avlTree.PreOrderTravers(root); avlTree.InOrderTravers(root); avlTree.PostOrderTravers(root); avlTree.Remove(root, 3); avlTree.Remove(root, 6); Console.WriteLine("Press any key..."); Console.ReadKey(); }
private void rl_case(Node root) { // From { .. | root | { { C | left | B } | node | A } } // To { .. | root | { C | left | { B | node | A } } } Node node = root.Right; Node left = node.Left; node.Left = left.Right; if (left.Right != null) { left.Right.Parent = node; left.Right = node; left.Right.Type = TYPE.LEFT; } left.Type = TYPE.RIGHT; node.Parent = left; left.Parent = node.Parent; root.Right = left; }
public Node Insert(Node node, int key) { if (node == null) { return new Node(key); } if (key < node.key) { node.left = Insert(node.left, key); } else { node.right = Insert(node.right, key); } return Balancing(node); }
// ---------------------------------- private Node simple_add(Node parent, ref Node node, double key, int level, TYPE type) { if (node != null) { if (node.Key > key) return simple_add(node, ref node.Left, key, level + 1, TYPE.LEFT); else return simple_add(node, ref node.Right, key, level + 1, TYPE.RIGHT); } else { node = new Node(parent, key, level, type); return node; } }
// ---------------------------------- // Constructor public AVLTree() { Root = null; }
// ---------------------------------- // Constructor public Node(Node parent, double key, int level, TYPE type) { Key = key; Type = type; Balance = 0; Parent = parent; Left = Right = null; }
// ---------------------------------- // Balance if the Node // -1 -> node is null public int Balance(Node node) { if (node != null) return Height(node.Left) - Height(node.Right); else return -1; }