Ejemplo n.º 1
0
        /// <summary>
        /// Rotates the subtree to the left.
        /// </summary>
        /// <param name="sRoot"> Root of the subtree. </param>
        /// <param name="rightChild"> Right child. </param>
        /// <returns> Returns new root of the subtree.</returns>
        private static NodeAVL <TKey, TValue> LeftRotate(NodeAVL <TKey, TValue> sRoot, NodeAVL <TKey, TValue> rightChild)
        {
            //storing the grandchild of sRoot
            NodeAVL <TKey, TValue> temp = rightChild.Left;

            sRoot.Right = temp;

            //if right child has left son
            if (temp != null)
            {
                temp.Parent = sRoot;
            }

            rightChild.Left = sRoot;
            sRoot.Parent    = rightChild;

            //first case balance factor of right child is 0
            //can occur only during deletion
            if (rightChild.Balance == 0)
            {
                sRoot.Balance = 1; rightChild.Balance = -1;
            }

            //second case can happen during either deletion or insertion
            else
            {
                sRoot.Balance = rightChild.Balance = 0;
            }

            return(rightChild);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Rotates the subtree to the right.
        /// </summary>
        /// <param name="sRoot"> Root of the subtree.</param>
        /// <param name="leftChild"> Left child. </param>
        /// <returns> Returns new root of the subtree. </returns>
        private static NodeAVL <TKey, TValue> RightRotate(NodeAVL <TKey, TValue> sRoot, NodeAVL <TKey, TValue> leftChild)
        {
            //storing the grandchild of sRoot in temp
            NodeAVL <TKey, TValue> temp = leftChild.Right;

            sRoot.Left = temp;

            //if left child has right son
            if (temp != null)
            {
                temp.Parent = sRoot;
            }

            leftChild.Right = sRoot;
            sRoot.Parent    = leftChild;

            //first case happens only during deletion
            if (leftChild.Balance == 0)
            {
                sRoot.Balance = 1; leftChild.Balance = -1;
            }

            //second case happens during either deletion or insertion
            else
            {
                leftChild.Balance = sRoot.Balance = 0;
            }

            return(leftChild);
        }
Ejemplo n.º 3
0
        /**********           private helper methods.        *******/

        /// <summary>
        /// Gets node with specified key.
        /// </summary>
        /// <param name="key"> Key of node.</param>
        /// <returns> Returns node with specified key if it exists and null otherwise.</returns>
        private NodeAVL <TKey, TValue> GetNode(TKey key)
        {
            NodeAVL <TKey, TValue> temp = this.root;
            int comparison;

            while (temp != null)
            {
                //compare the given key with tree node key
                comparison = key.CompareTo(temp.Key);

                //if key is greater than node key
                //go to the right subtree
                if (comparison == 1)
                {
                    temp = temp.Right;
                }

                //else if key is less than node key
                //go to the left subtree
                else if (comparison == -1)
                {
                    temp = temp.Left;
                }

                //else return found node.
                else
                {
                    return(temp);
                }
            }

            //if no node is found with given key,return null.
            return(null);
        }
Ejemplo n.º 4
0
 public void Insert(int data)
 {
     if (nodeAVL == null)
     {
         nodeAVL = new NodeAVL();
     }
     if (nodeAVL.Key == null || nodeAVL.Key == data)
     {
         nodeAVL.Key = data;
         nodeAVL.Kvo++;
         return;
     }
     if (nodeAVL.Key > data)
     {
         if (nodeAVL.Left == null)
         {
             nodeAVL.Left = new NodeAVL();
         }
         Insert(data, nodeAVL.Left);
         nodeAVL = Balancer_tree(nodeAVL);
     }
     else
     {
         if (nodeAVL.Right == null)
         {
             nodeAVL.Right = new NodeAVL();
         }
         Insert(data, nodeAVL.Right);
         nodeAVL = Balancer_tree(nodeAVL);
     }
 }
Ejemplo n.º 5
0
 public NodeAVL(T key, V value)
 {
     Key    = key;
     Left   = Right = null;
     Height = 1;
     Item   = value;
 }
Ejemplo n.º 6
0
            private NodeAVL Balancer_tree(NodeAVL node)
            {
                int balance_point = Balance_point(node);

                if (balance_point == 2)
                {
                    if (Balance_point(node.Left) == 1)
                    {
                        node = Rotate_LL(node);
                    }
                    else
                    {
                        node = Rotate_LR(node);
                    }
                }
                else if (balance_point == -2)
                {
                    if (Balance_point(node.Right) == -1)
                    {
                        node = Rotate_RR(node);
                    }
                    else
                    {
                        node = Rotate_RL(node);
                    }
                }
                return(node);
            }
Ejemplo n.º 7
0
 public override void Delete(IPersistentStore store)
 {
     for (NodeAVL eavl = this.NPrimaryNode; eavl != null; eavl = eavl.nNext)
     {
         eavl.Delete();
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the value with the given key.
        /// </summary>
        /// <param name="key"> Key.</param>
        /// <returns> Returns the value with the given key. </returns>
        public TValue this[TKey key]
        {
            //gets the value
            get
            {
                NodeAVL <TKey, TValue> temp = this.GetNode(key);

                //if the key is not present in dictionary
                //throw an exception
                if (temp == null)
                {
                    throw new KeyNotFoundException("The key is not present in Dictionary.");
                }
                return(temp.Value);
            }

            //sets the value
            set
            {
                NodeAVL <TKey, TValue> temp = this.GetNode(key);

                //if the key is not present in dictionary
                //throw an exception
                if (temp == null)
                {
                    throw new KeyNotFoundException("The key is not present in Dictionary.");
                }
                temp.Value = value;
            }
        }
Ejemplo n.º 9
0
            private NodeAVL Rotate_LR(NodeAVL node)
            {
                NodeAVL newNode = node.Left;

                node.Left = Rotate_RR(newNode);
                return(Rotate_LL(node));
            }
Ejemplo n.º 10
0
 private void Insert(int data, NodeAVL node)
 {
     if (node.Key == null || node.Key == data)
     {
         node.Key = data;
         node.Kvo++;
         return;
     }
     if (data < node.Key)
     {
         if (node.Left == null)
         {
             node.Left = new NodeAVL();
         }
         Insert(data, node.Left);
         node.Left = Balancer_tree(node.Left);
     }
     if (data > node.Key)
     {
         if (node.Right == null)
         {
             node.Right = new NodeAVL();
         }
         Insert(data, node.Right);
         node.Right = Balancer_tree(node.Right);
     }
 }
Ejemplo n.º 11
0
            private NodeAVL Rotate_RL(NodeAVL node)
            {
                NodeAVL newNode = node.Right;

                node.Right = Rotate_LL(newNode);
                return(Rotate_RR(node));
            }
Ejemplo n.º 12
0
        /// <summary>
        /// Transplants the two subtrees.
        /// </summary>
        /// <param name="firstSubTree"> First subtree. </param>
        /// <param name="secondSubTree"> Second subtree. </param>
        private void Transplant(NodeAVL <TKey, TValue> firstSubTree, NodeAVL <TKey, TValue> secondSubTree)
        {
            //if first subtree has no parent,hence first subtree is the root of the tree
            if (firstSubTree.Parent == null)
            {
                this.root = secondSubTree;
            }

            //if first subtree is a left child
            else if (firstSubTree == firstSubTree.Parent.Left)
            {
                firstSubTree.Parent.Left = secondSubTree;
            }

            //if first tree is a right child
            else
            {
                firstSubTree.Parent.Right = secondSubTree;
            }

            //if second subtree is not null
            if (secondSubTree != null)
            {
                secondSubTree.Parent = firstSubTree.Parent;
            }
        }
Ejemplo n.º 13
0
            private NodeAVL Rotate_RR(NodeAVL node)
            {
                NodeAVL newNode = node.Right;

                node.Right   = newNode.Left;
                newNode.Left = node;
                return(newNode);
            }
Ejemplo n.º 14
0
            private int Balance_point(NodeAVL node)
            {
                int l             = Height(node.Left);
                int r             = Height(node.Right);
                int balance_point = l - r;

                return(balance_point);
            }
Ejemplo n.º 15
0
 public void ClearNonPrimaryNodes()
 {
     for (NodeAVL eavl = this.NPrimaryNode.nNext; eavl != null; eavl = eavl.nNext)
     {
         eavl.Delete();
         eavl.IBalance = 0;
     }
 }
Ejemplo n.º 16
0
 public override void SetPos(int pos)
 {
     base.Position = pos;
     for (NodeAVL eavl = base.NPrimaryNode; eavl != null; eavl = eavl.nNext)
     {
         ((NodeAVLDisk)eavl).iData = base.Position;
     }
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Inserts new item to the dictionary.
        /// </summary>
        /// <param name="key"> Key.</param>
        /// <param name="value"> Value.</param>
        private void Insert(TKey key, TValue value)
        {
            //constructing new node
            NodeAVL <TKey, TValue> newNode  = new NodeAVL <TKey, TValue>(key, value, 0, null, null, null);
            NodeAVL <TKey, TValue> tempNode = this.root;
            // NodeAVL<TKey, TValue> newNodeParent = null;
            int comparison;

            //iterating all over the tree to find the right place for insertion
            while (tempNode != null)
            {
                //save the parent
                newNode.Parent = tempNode;
                comparison     = key.CompareTo(tempNode.Key);

                if (comparison < 0)
                {
                    //go to the left subtree
                    tempNode = tempNode.Left;
                }
                else if (comparison > 0)
                {
                    //go to the right suntree
                    tempNode = tempNode.Right;
                }

                //otherwise if key is found throw exception cause key collisions is not allowed
                else
                {
                    throw new Exception("Item with that key already exists in the dictionary.");
                }
            }

            //newNode.Parent = newNodeParent;

            //if new node has no parent then make it the root of the tree
            if (newNode.Parent == null)
            {
                this.root = newNode;
            }

            else if (key.CompareTo(newNode.Parent.Key) < 0)
            {
                //make the new node a left child
                newNode.Parent.Left = newNode;
            }
            else
            {
                //make the new node  a right child
                newNode.Parent.Right = newNode;
            }

            //increment count of the dictionary
            ++this.Count;

            //rebalancing the tree after insertion to keep AVL tree arrangement
            this.InsertFixup(newNode);
        }
Ejemplo n.º 18
0
 public static void BrowerAVL_LNR(NodeAVL root)
 {
     if (root != null)
     {
         BrowerAVL_LNR(root.left);
         Console.WriteLine("node: {0}", root.data);
         BrowerAVL_LNR(root.right);
     }
 }
Ejemplo n.º 19
0
 private void WriteNodes(IRowOutputInterface _out)
 {
     _out.WriteSize(this.storageSize);
     for (NodeAVL eavl = base.NPrimaryNode; eavl != null; eavl = eavl.nNext)
     {
         eavl.Write(_out);
     }
     this._hasNodesChanged = false;
 }
Ejemplo n.º 20
0
 public override void Write(IRowOutputInterface output, IntLookup lookup)
 {
     output.WriteSize(this.storageSize);
     for (NodeAVL eavl = base.NPrimaryNode; eavl != null; eavl = eavl.nNext)
     {
         ((NodeAVLDisk)eavl).Write(output, lookup);
     }
     output.WriteData(base.RowData, base.table.ColTypes);
     output.WriteEnd();
 }
Ejemplo n.º 21
0
        public virtual NodeAVL GetNode(int index)
        {
            NodeAVL nPrimaryNode = this.NPrimaryNode;

            while (index-- > 0)
            {
                nPrimaryNode = nPrimaryNode.nNext;
            }
            return(nPrimaryNode);
        }
Ejemplo n.º 22
0
        public virtual NodeAVL InsertNode(int index)
        {
            NodeAVL node  = this.GetNode(index - 1);
            NodeAVL eavl2 = new NodeAVL(this)
            {
                nNext = node.nNext
            };

            node.nNext = eavl2;
            return(eavl2);
        }
Ejemplo n.º 23
0
        /*******************************************************************************************/
        /*******************************************************************************************/

        public void IncLevelAVL(NodeAVL child)
        {
            if (child is null)
            {
                Level = 1;
            }
            else if ((child.Level + 1) > Level)
            {
                Level = child.Level + 1;
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Gets the node with minimum key in the tree.
        /// </summary>
        /// <param name=""> Node. </param>
        /// <returns> Returns the node with minimum key. </returns>
        private static NodeAVL <TKey, TValue> MinimumKeyNode(NodeAVL <TKey, TValue> node)
        {
            NodeAVL <TKey, TValue> temp = node;

            //go to the left most node
            while (temp.Left != null)
            {
                temp = temp.Left;
            }

            return(temp);
        }
Ejemplo n.º 25
0
        public override void SetNewNodes()
        {
            int indexCount = base.table.GetIndexCount();

            base.NPrimaryNode = new NodeAVLDisk(this, 0);
            NodeAVL nPrimaryNode = base.NPrimaryNode;

            for (int i = 1; i < indexCount; i++)
            {
                nPrimaryNode.nNext = new NodeAVLDisk(this, i);
                nPrimaryNode       = nPrimaryNode.nNext;
            }
        }
Ejemplo n.º 26
0
        public override void Destroy()
        {
            this.ClearNonPrimaryNodes();
            NodeAVL nNext = this.NPrimaryNode.nNext;

            while (nNext != null)
            {
                NodeAVL eavl2 = nNext;
                nNext       = nNext.nNext;
                eavl2.nNext = null;
            }
            this.NPrimaryNode = null;
        }
Ejemplo n.º 27
0
        public virtual void SetNewNodes()
        {
            int indexCount = base.table.GetIndexCount();

            this.NPrimaryNode = new NodeAVL(this);
            NodeAVL nPrimaryNode = this.NPrimaryNode;

            for (int i = 1; i < indexCount; i++)
            {
                nPrimaryNode.nNext = new NodeAVL(this);
                nPrimaryNode       = nPrimaryNode.nNext;
            }
        }
Ejemplo n.º 28
0
            private int Height(NodeAVL current)
            {
                int h = 0;

                if (current != null)
                {
                    int l = Height(current.Left);
                    int r = Height(current.Right);
                    h = l >= r ? l : r;
                    h++;
                }
                return(h);
            }
Ejemplo n.º 29
0
        /// <summary>
        /// Does double left-right rotation.
        /// </summary>
        /// <param name="sRoot"> Root of the subtree.</param>
        /// <param name="leftChild"> Left Child. </param>
        /// <returns> Returns new root of the subtree.</returns>
        private static NodeAVL <TKey, TValue> LeftRightRotate(NodeAVL <TKey, TValue> sRoot, NodeAVL <TKey, TValue> leftChild)
        {
            NodeAVL <TKey, TValue> temp1, temp2, temp3;

            temp1           = leftChild.Right;
            temp2           = temp1.Left;
            leftChild.Right = temp2;

            //if the grandchild exists
            if (temp2 != null)
            {
                temp2.Parent = leftChild;
            }

            temp1.Left       = leftChild;
            leftChild.Parent = temp1;

            temp3      = temp1.Right;
            sRoot.Left = temp3;

            //if the grandchild exists
            if (temp3 != null)
            {
                temp3.Parent = sRoot;
            }
            temp1.Right  = sRoot;
            sRoot.Parent = temp1;

            //we need to update balance factors
            //first case,occurs during insertion or deletion
            if (temp1.Balance > 0)
            {
                sRoot.Balance = -1; leftChild.Balance = 0;
            }

            //second case,occurs only during deletion
            else if (temp1.Balance == 0)
            {
                sRoot.Balance = leftChild.Balance = 0;
            }
            //third case happens with deletion or insertion
            else
            {
                sRoot.Balance     = 0;
                leftChild.Balance = 1;
            }
            temp1.Balance = 0;

            //return new root of the tree
            return(temp1);
        }
Ejemplo n.º 30
0
 public override void SetInMemory(bool _in)
 {
     lock (this)
     {
         this._isInMemory = _in;
         if (!_in)
         {
             for (NodeAVL eavl = base.NPrimaryNode; eavl != null; eavl = eavl.nNext)
             {
                 eavl.SetInMemory(_in);
             }
         }
     }
 }