示例#1
0
文件: Program.cs 项目: Miltt/Console
 public Node(int key)
 {
     this.key = key;
     this.height = 1;
     this.left = null;
     this.right = null;
 }        
示例#2
0
文件: Program.cs 项目: Miltt/Console
        private void RecalcHeight(Node node)
        {
            int hLeft = GetHeight(node.left);
            int hRight = GetHeight(node.right);

            node.height = (hLeft > hRight ? hLeft : hRight) + 1;
        }
示例#3
0
 public int Height(Node n)
 {
     if (n == null)
         return 0;
     else
         return Math.Max(Height(n.leftChild), Height(n.rightChild)) + 1;
 }
示例#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);
         }
     }
 }
示例#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");
 }
示例#6
0
文件: Program.cs 项目: Miltt/Console
        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;
        }
示例#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);
     }
 }
示例#8
0
文件: Program.cs 项目: Miltt/Console
        private Node RotateLeft(Node node)
        {
            Node root = node.right;
            node.right = root.left;
            root.left = node;

            RecalcHeight(node);
            RecalcHeight(root);

            return root;
        }
示例#9
0
 public int BalanceFactor(Node leftNode, Node rightNode)
 {
     try
     {
         return (leftNode.height - rightNode.height);
     }
     catch
     {
         return 0;
     }
 }
示例#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);
            }
        }
示例#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);
 }
示例#12
0
文件: AVLTree.cs 项目: Beraliv/Csharp
        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;
        }
示例#13
0
文件: AVLTree.cs 项目: Beraliv/Csharp
 // ----------------------------------
 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;
 }
示例#14
0
文件: AVLTree.cs 项目: Beraliv/Csharp
 // ----------------------------------
 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);
     }
 }
示例#15
0
文件: AVLTree.cs 项目: Beraliv/Csharp
        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);
        }
示例#16
0
文件: AVLTree.cs 项目: Beraliv/Csharp
        // ----------------------------------
        // 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;
        }
示例#17
0
文件: Program.cs 项目: Miltt/Console
 public void InOrderTravers(Node root)
 {
     if (root != null)
     {
         InOrderTravers(root.left);
         Output(root.key);
         InOrderTravers(root.right);
     }
 }
示例#18
0
文件: AVLTree.cs 项目: Beraliv/Csharp
        private void update_balance(Node added)
        {
            added.UpdateBalance(Balance(added));

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