Ejemplo n.º 1
0
 public Node(int key)
 {
     this.key = key;
     this.height = 1;
     this.left = null;
     this.right = null;
 }        
Ejemplo n.º 2
0
        private void RecalcHeight(Node node)
        {
            int hLeft = GetHeight(node.left);
            int hRight = GetHeight(node.right);

            node.height = (hLeft > hRight ? hLeft : hRight) + 1;
        }
Ejemplo n.º 3
0
 public int Height(Node n)
 {
     if (n == null)
         return 0;
     else
         return Math.Max(Height(n.leftChild), Height(n.rightChild)) + 1;
 }
Ejemplo n.º 4
0
 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);
         }
     }
 }
Ejemplo n.º 5
0
 //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");
 }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
 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);
     }
 }
Ejemplo n.º 8
0
        private Node RotateLeft(Node node)
        {
            Node root = node.right;
            node.right = root.left;
            root.left = node;

            RecalcHeight(node);
            RecalcHeight(root);

            return root;
        }
Ejemplo n.º 9
0
 public int BalanceFactor(Node leftNode, Node rightNode)
 {
     try
     {
         return (leftNode.height - rightNode.height);
     }
     catch
     {
         return 0;
     }
 }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
 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);
 }
Ejemplo n.º 12
0
        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;
        }
Ejemplo n.º 13
0
 // ----------------------------------
 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;
 }
Ejemplo n.º 14
0
 // ----------------------------------
 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);
     }
 }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        // ----------------------------------
        // 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;
        }
Ejemplo n.º 17
0
 public void InOrderTravers(Node root)
 {
     if (root != null)
     {
         InOrderTravers(root.left);
         Output(root.key);
         InOrderTravers(root.right);
     }
 }
Ejemplo n.º 18
0
        private void update_balance(Node added)
        {
            added.UpdateBalance(Balance(added));

            if (added.Parent != null)
                update_balance(added.Parent);
        }
Ejemplo n.º 19
0
        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;
        }
Ejemplo n.º 20
0
        // ----------------------------------
        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;
        }
Ejemplo n.º 21
0
 public void PostOrderTravers(Node root)
 {
     if (root != null)
     {
         PostOrderTravers(root.left);
         PostOrderTravers(root.right);
         Output(root.key);
     }
 }
Ejemplo n.º 22
0
 private int GetBalance(Node node)
 {
     return GetHeight(node.right) - GetHeight(node.left);
 }
Ejemplo n.º 23
0
 private int GetHeight(Node node)
 {
     return node != null ? node.height : 0;
 }
Ejemplo n.º 24
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();
        }
Ejemplo n.º 25
0
        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;
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
 // ----------------------------------
 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;
     }
 }
Ejemplo n.º 28
0
 // ----------------------------------
 // Constructor
 public AVLTree()
 {
     Root = null;
 }
Ejemplo n.º 29
0
 // ----------------------------------
 // Constructor
 public Node(Node parent, double key, int level, TYPE type)
 {
     Key = key;
     Type = type;
     Balance = 0;
     Parent = parent;
     Left = Right = null;
 }
Ejemplo n.º 30
0
 // ----------------------------------
 // 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;
 }