Esempio n. 1
0
        public void InOrderPredecessor_RootRight()
        {
            AVLNode<int> rootRight = new AVLNode<int>(50)
            {
                Right = new AVLNode<int>(75)
            };

            Assert.IsNull(rootRight.InOrderPredecessor);
        }
Esempio n. 2
0
        public void InOrderPredecessor_RootLeft()
        {
            AVLNode<int> rootLeft = new AVLNode<int>(50)
            {
                Left = new AVLNode<int>(25)
            };

            Assert.AreEqual<int>(25, rootLeft.InOrderPredecessor.Value);
        }
Esempio n. 3
0
        public void InOrderPredecessor_RootLeftRight()
        {
            AVLNode<int> rootLeftRight = new AVLNode<int>(50)
            {
                Left = new AVLNode<int>(25)
                {
                    Right = new AVLNode<int>(30)
                }
            };

            Assert.AreEqual<int>(30, rootLeftRight.InOrderPredecessor.Value);
        }
        public void AssertValidTree_InvalidBalance_Left()
        {
            var root = new AVLNode<int>(100);
            root.Left = new AVLNode<int>(50);
            root.Right = new AVLNode<int>(150);
            root.Left.Left = new AVLNode<int>(30);
            root.Left.Left.Left = new AVLNode<int>(20);

            AVLTree<int> bst = new AVLTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
Esempio n. 5
0
        public void ResetHeight()
        {
            var node = new AVLNode<int>(20);

            Assert.AreEqual<int>(0, node.Height);

            node.Left = new AVLNode<int>(10);

            Assert.AreEqual<int>(0, node.Height);

            node.ResetHeight();

            Assert.AreEqual<int>(1, node.Height);
        }
Esempio n. 6
0
 private int HeightOfNode(AVLNode n)
 {
     return(n is null ? -1 : n.Height);
 }
Esempio n. 7
0
        public void IsLeaf_False_Right()
        {
            var node = new AVLNode<int>(50);
            node.Right = new AVLNode<int>(70);

            Assert.IsFalse(node.IsLeaf);
        }
 /// <summary>
 /// clearing the tree
 /// </summary>
 public void Clear()
 {
     entries.Clear();
     AVLroot = null;
     count   = 0;
 }
Esempio n. 9
0
 private bool IsRigthHeavy(AVLNode n)
 {
     return(CalculateBalancedFactor(n) < -1 ? true : false);
 }
 public AVLNode GetNearestRight(TKey key)
 {
     return(AVLNode.GetNearestRight(_root, key, _comparison));
 }
Esempio n. 11
0
 public AVLNode(TKey key, TValue value, AVLNode parent)
 {
     Key    = key;
     Value  = value;
     Parent = parent;
 }
            internal static void AddItem(TKey key, TValue value, QueuedAVLTree <TKey, TValue> tree, ref int elementCount)
            {
                if (!tree.ContainsKey(key))
                {
                    tree.queue.CheckRoom(tree);

                    if (tree.head == null)
                    {
                        tree.head = new AVLNode(key, value, tree);
                        elementCount++;
                        goto LukeIDeletedYourFather;
                    }
                }

                AVLNode headNode = tree.head;
                int     compare  = key.CompareTo(headNode.key);

                while (true)
                {
                    if (compare < 0)
                    {
                        if (headNode.left == null)
                        {
                            headNode.left = new AVLNode(key: key, value: value, tree: tree)
                            {
                                head = headNode, isLeft = true
                            };
                            headNode._depthL = 1;
                            AVLNode.BalanceBubbleUp(headNode, tree);
                            elementCount++;
                            break;
                        }
                        else
                        {
                            headNode = headNode.left;
                            compare  = key.CompareTo(headNode.key);
                        }
                    }
                    else if (compare > 0)
                    {
                        if (headNode.right == null)
                        {
                            headNode.right = new AVLNode(key: key, value: value, tree: tree)
                            {
                                head = headNode, isLeft = false
                            };
                            headNode._depthR = 1;
                            AVLNode.BalanceBubbleUp(headNode, tree);
                            elementCount++;
                            break;
                        }
                        else
                        {
                            headNode = headNode.right;
                            compare  = key.CompareTo(headNode.key);
                        }
                    }
                    else
                    {
                        headNode.value = value;
                        break;
                    }
                }

                LukeIDeletedYourFather :;
            }
 private void SetHeight(AVLNode node)
 {
     node.height = 1 + Math.Max(GetHeight(node.leftChild), GetHeight(node.rightChild));
 }
 private int Height(AVLNode <T> node)
 {
     return(-1);
 }
            internal static void RemoveNode(AVLNode node, QueuedAVLTree <TKey, TValue> tree, bool callFromQueue = false)
            {
                if (!callFromQueue)
                {
                    tree.queue.Dequeue(node.linkedElement, tree);
                }

                if (node.right == null && node.left == null) // no children
                {
                    if (node.head == null)                   // was the top node
                    {
                        tree.head = null;
                    }
                    else
                    {
                        if (node.isLeft)
                        {
                            node.head.left    = null;
                            node.head._depthL = 0;
                        }
                        else
                        {
                            node.head.right   = null;
                            node.head._depthR = 0;
                        }

                        AVLNode.BalanceSelfBubbleUp(node.head, tree);
                    }
                }
                else if (node.right == null || node.left == null) // one child
                {
                    AVLNode child = node.right != null ? node.right : node.left;

                    if (node.head == null) // was the top node
                    {
                        tree.head  = child;
                        child.head = null;
                    }
                    else
                    {
                        child.isLeft = node.isLeft;

                        if (node.isLeft)
                        {
                            node.head.left     = child;
                            child.head         = node.head;
                            node.head._depthL -= 1;
                        }
                        else
                        {
                            node.head.right    = child;
                            child.head         = node.head;
                            node.head._depthR -= 1;
                        }

                        AVLNode.BalanceSelfBubbleUp(node.head, tree);
                    }
                }
                else // two children :O
                {
                    AVLNode child = node.right, childhead = node.head;

                    while (child.left != null)
                    {
                        childhead = child;
                        child     = child.left;
                    }

                    if (childhead != node.head)
                    {
                        if (child.right != null)
                        {
                            childhead.left     = child.right;
                            child.right.head   = childhead;
                            child.right.isLeft = true;
                            childhead._depthL--;
                        }
                        else
                        {
                            childhead.left    = null;
                            childhead._depthL = 0;
                        }

                        child.right = node.right;
                    }

                    child.left      = node.left;
                    child.left.head = child;
                    child.head      = node.head;
                    child.isLeft    = node.isLeft;

                    if (node.head == null)
                    {
                        tree.head = child;
                    }
                    else
                    {
                        if (node.isLeft)
                        {
                            node.head.left = child;
                        }
                        else
                        {
                            node.head.right = child;
                        }
                    }

                    if (childhead == node.head)
                    {
                        AVLNode.BalanceSelfBubbleUp(child, tree);
                    }
                    else
                    {
                        child.right.head = child;
                        AVLNode.BalanceSelfBubbleUp(childhead, tree);
                    }
                }

                tree.count--;
            }
Esempio n. 16
0
 /// <summary>
 ///
 /// Complexity: O(1).
 /// </summary>
 public void Clear()
 {
     root = null;
 }
Esempio n. 17
0
            /// <summary>
            ///
            /// Complexity: O(log n)
            /// </summary>

            public T GetMin()
            {
                AVLNode <T> result = GetMinNode();

                return(result != null ? result.Value : default(T));
            }
Esempio n. 18
0
            void Swap(AVLNode <T> a, AVLNode <T> b)
            {
                if (a == null || b == null)
                {
                    return;
                }

                bool aWasLeft = a.Parent != null && a.Parent.Left == a;
                bool bWasLeft = b.Parent != null && b.Parent.Left == b;

                AVLNode <T> tempLeft   = a.Left;
                AVLNode <T> tempRight  = a.Right;
                AVLNode <T> tempParent = a.Parent;
                int         tempHeight = a.Height;

                a.Left   = b.Left;
                a.Right  = b.Right;
                a.Parent = b.Parent;
                a.Height = b.Height;

                b.Left   = tempLeft;
                b.Right  = tempRight;
                b.Parent = tempParent;
                b.Height = tempHeight;

                // if 'b' was the left node, right node or parent of 'a'
                if (b.Left == b)
                {
                    b.Left = a;
                }
                else if (b.Right == b)
                {
                    b.Right = a;
                }
                else if (b.Parent == b)
                {
                    b.Parent = a;
                }

                // if 'a' was the left node, right node or parent of 'b'
                if (a.Left == a)
                {
                    a.Left = b;
                }
                else if (a.Right == a)
                {
                    a.Right = b;
                }
                else if (a.Parent == a)
                {
                    a.Parent = b;
                }

                // swapping all pointers as in the following lines
                // keeps all AVLNodes the user might store valid

                if (a.Left != null)
                {
                    a.Left.Parent = a;
                }
                if (a.Right != null)
                {
                    a.Right.Parent = a;
                }

                if (b.Left != null)
                {
                    b.Left.Parent = b;
                }
                if (b.Right != null)
                {
                    b.Right.Parent = b;
                }

                if (a.Parent != null && bWasLeft)
                {
                    a.Parent.Left = a;
                }
                else if (a.Parent != null)
                {
                    a.Parent.Right = a;
                }

                if (b.Parent != null && aWasLeft)
                {
                    b.Parent.Left = b;
                }
                else if (b.Parent != null)
                {
                    b.Parent.Right = b;
                }
            }
 public void Bound(TKey key, out AVLNode lower, out AVLNode upper)
 {
     AVLNode.Bound(_root, key, _comparison, out lower, out upper);
 }
 private int GetHeight(AVLNode node)
 {
     return(node == null ? -1 : node.height);
 }
Esempio n. 21
0
        public void ToString_RootRight()
        {
            AVLNode<int> node = new AVLNode<int>(50)
            {
                Right = new AVLNode<int>(75),
            };

            Assert.AreEqual<string>("50; Left=null; Right=75", node.ToString());
        }
 private bool IsLeftHeavy(AVLNode node)
 {
     return(GetBalanceFactor(node) > 1);
 }
 public void Add(TKey key, TValue value)
 {
     AVLNode.Add(ref _root, key, value, _comparison);
     _count++;
 }
 private bool IsRightHeavy(AVLNode node)
 {
     return(GetBalanceFactor(node) < -1);
 }
Esempio n. 25
0
        /// <summary>
        /// removing node with key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(TKey key)
        {
            AVLNode node = AVLroot;

            while (node != null)
            {
                if (key.CompareTo(node.Key) < 0)
                {
                    node = node.Left;
                }
                else if (key.CompareTo(node.Key) > 0)
                {
                    node = node.Right;
                }
                else
                {
                    AVLNode l = node.Left;
                    AVLNode r = node.Right;
                    if (l == null)
                    {
                        if (r == null)
                        {
                            if (node == AVLroot)
                            {
                                AVLroot = null;
                            }
                            else
                            {
                                AVLNode nodeP = node.Parent;
                                if (nodeP.Left == node)
                                {
                                    nodeP.Left = null;
                                    RemoveBalanceFactor(nodeP, -1);
                                }
                                else
                                {
                                    nodeP.Right = null;
                                    RemoveBalanceFactor(nodeP, 1);
                                }
                            }
                        }
                        else
                        {
                            Change(node, r);
                            RemoveBalanceFactor(node, 0);
                        }
                    }
                    else if (r == null)
                    {
                        Change(node, l);
                        RemoveBalanceFactor(node, 0);
                    }
                    else
                    {
                        AVLNode temp = r;
                        if (temp.Left == null)
                        {
                            AVLNode parent = node.Parent;

                            temp.Parent        = parent;
                            temp.Left          = l;
                            temp.balanceFactor = node.balanceFactor;

                            if (l != null)
                            {
                                l.Parent = temp;
                            }

                            if (node == AVLroot)
                            {
                                AVLroot = temp;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = temp;
                                }
                                else
                                {
                                    parent.Right = temp;
                                }
                            }

                            RemoveBalanceFactor(temp, 1);
                        }
                        else
                        {
                            while (temp.Left != null)
                            {
                                temp = temp.Left;
                            }
                            AVLNode parent     = node.Parent;
                            AVLNode tempParent = temp.Parent;
                            AVLNode tempRight  = temp.Right;
                            if (tempParent.Left == temp)
                            {
                                tempParent.Left = tempRight;
                            }
                            else
                            {
                                tempParent.Right = tempRight;
                            }
                            if (tempRight != null)
                            {
                                tempRight.Parent = tempParent;
                            }
                            temp.Parent        = parent;
                            temp.Left          = l;
                            temp.balanceFactor = node.balanceFactor;
                            temp.Right         = r;
                            r.Parent           = temp;
                            if (l != null)
                            {
                                l.Parent = temp;
                            }
                            if (node == AVLroot)
                            {
                                AVLroot = temp;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = temp;
                                }
                                else
                                {
                                    parent.Right = temp;
                                }
                            }
                            RemoveBalanceFactor(tempParent, -1);
                        }
                    }
                    count--;
                    entries.Remove(new AVLNode(key, this[key]));
                    return(true);
                }
            }
            return(false);
        }
 private int GetBalanceFactor(AVLNode node)
 {
     return(node == null ? 0 : GetHeight(node.leftChild) - GetHeight(node.rightChild));
 }
Esempio n. 27
0
 private int CalculateBalancedFactor(AVLNode n)
 {
     return(n is null ? 0 : HeightOfNode(n.LeftChild) - HeightOfNode(n.RigthChild));
 }
 public void Insert(int value)
 {
     root = Insert(root, value);
 }
Esempio n. 29
0
 private bool IsLeftHeavy(AVLNode n)
 {
     return(CalculateBalancedFactor(n) > 1 ? true : false);
 }
Esempio n. 30
0
 internal static int GetCount(AVLNode node)
 {
     return(1 + (node.left == null ? 0 : GetCount(node.left)) + (node.right == null ? 0 : GetCount(node.right)));
 }
Esempio n. 31
0
        public void IsLeaf_False_Left()
        {
            var node = new AVLNode<int>(50);
            node.Left = new AVLNode<int>(20);

            Assert.IsFalse(node.IsLeaf);
        }
Esempio n. 32
0
            internal static void RemoveNode(AVLNode node, object[] HashMap, int hash, ref int elementCount)
            {
                if (node.right == null && node.left == null) // no children
                {
                    if (node.head == null)                   // was the top node
                    {
                        HashMap[hash] = null;
                    }
                    else
                    {
                        if (node.isLeft)
                        {
                            node.head.left    = null;
                            node.head._depthL = 0;
                        }
                        else
                        {
                            node.head.right   = null;
                            node.head._depthR = 0;
                        }

                        AVLNode.BalanceSelfBubbleUp(node.head, HashMap, hash);
                    }
                }
                else if (node.right == null || node.left == null) // one child
                {
                    AVLNode child = node.right != null ? node.right : node.left;

                    if (node.head == null) // was the top node
                    {
                        HashMap[hash] = child;
                        child.head    = null;
                    }
                    else
                    {
                        child.isLeft = node.isLeft;

                        if (node.isLeft)
                        {
                            node.head.left     = child;
                            child.head         = node.head;
                            node.head._depthL -= 1;
                        }
                        else
                        {
                            node.head.right    = child;
                            child.head         = node.head;
                            node.head._depthR -= 1;
                        }

                        AVLNode.BalanceSelfBubbleUp(node.head, HashMap, hash);
                    }
                }
                else // two children :O
                {
                    AVLNode child = node.right, childhead = node.head;

                    while (child.left != null)
                    {
                        childhead = child;
                        child     = child.left;
                    }

                    if (childhead != node.head)
                    {
                        if (child.right != null)
                        {
                            childhead.left     = child.right;
                            child.right.head   = childhead;
                            child.right.isLeft = true;
                            childhead._depthL--;
                        }
                        else
                        {
                            childhead.left    = null;
                            childhead._depthL = 0;
                        }

                        child.right = node.right;
                    }

                    child.left      = node.left;
                    child.left.head = child;
                    child.head      = node.head;
                    child.isLeft    = node.isLeft;

                    if (node.head == null)
                    {
                        HashMap[hash] = child;
                    }
                    else
                    {
                        if (node.isLeft)
                        {
                            node.head.left = child;
                        }
                        else
                        {
                            node.head.right = child;
                        }
                    }

                    if (childhead == node.head)
                    {
                        AVLNode.BalanceSelfBubbleUp(child, HashMap, hash);
                    }
                    else
                    {
                        child.right.head = child;
                        AVLNode.BalanceSelfBubbleUp(childhead, HashMap, hash);
                    }
                }

                elementCount--;
            }
Esempio n. 33
0
 public void IsLeaf_True()
 {
     var node = new AVLNode<int>(50);
     Assert.IsTrue(node.IsLeaf);
 }
 public void Clear()
 {
     _root  = null;
     _count = 0;
 }
Esempio n. 35
0
        public void RotateRight_FullTree()
        {
            var node = new AVLNode<int>(100);

            node.Left = new AVLNode<int>(50);
            node.Right = new AVLNode<int>(150);

            node.Left.Left = new AVLNode<int>(25);
            node.Left.Right = new AVLNode<int>(75);
            node.Right.Left = new AVLNode<int>(125);
            node.Right.Right = new AVLNode<int>(175);

            node = node.RotateRight();

            Assert.AreEqual<int>(50, node.Value);
            Assert.AreEqual<int>(25, node.Left.Value);
            Assert.AreEqual<int>(100, node.Right.Value);
            Assert.AreEqual<int>(75, node.Right.Left.Value);
            Assert.AreEqual<int>(150, node.Right.Right.Value);
            Assert.AreEqual<int>(125, node.Right.Right.Left.Value);
            Assert.AreEqual<int>(175, node.Right.Right.Right.Value);
        }
Esempio n. 36
0
        public void InOrderSuccessor_RootLeft()
        {
            AVLNode<int> rootLeft = new AVLNode<int>(50)
                {
                    Left = new AVLNode<int>(25)
                };

            Assert.IsNull(rootLeft.InOrderSuccessor);
        }
Esempio n. 37
0
        public void ToString_RootLeft()
        {
            AVLNode<int> node = new AVLNode<int>(50)
            {
                Left = new AVLNode<int>(25),
            };

            Assert.AreEqual<string>("50; Left=25; Right=null", node.ToString());
        }
Esempio n. 38
0
        public void AssertValidTree_InvalidChildren()
        {
            var root = new AVLNode<int>(100);
            root.Left = new AVLNode<int>(150);

            AVLTree<int> bst = new AVLTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
Esempio n. 39
0
        /// <inheritdoc />
        public TValue this[TKey key]
        {
            get
            {
                if (key == null)
                {
                    return(default(TValue));
                }

                int hash = Math.Abs(key.GetHashCode()) % _size;

                if (HashMap[hash] == null)
                {
                    return(default(TValue));
                }
                else if (HashMap[hash] is KeyValuePair <TKey, TValue> )
                {
                    if (((KeyValuePair <TKey, TValue>)HashMap[hash]).Key.Equals(key))
                    {
                        return(((KeyValuePair <TKey, TValue>)HashMap[hash]).Value);
                    }
                    else
                    {
                        return(default(TValue));
                    }
                }
                else // if HashMap[hash] is an AVL Node search for it
                {
                    AVLNode node    = (AVLNode)HashMap[hash];
                    int     compare = key.CompareTo(node.key);

                    while (true)
                    {
                        if (compare < 0)
                        {
                            node = node.left;

                            if (node != null)
                            {
                                compare = key.CompareTo(node.key);
                            }
                            else
                            {
                                return(default(TValue));
                            }
                        }
                        else if (compare > 0)
                        {
                            node = node.right;

                            if (node != null)
                            {
                                compare = key.CompareTo(node.key);
                            }
                            else
                            {
                                return(default(TValue));
                            }
                        }
                        else
                        {
                            return(node.value);
                        }
                    }
                }
            }

            set
            {
                Add(new KeyValuePair <TKey, TValue>(key, value));
            }
        }
 public IEnumerator <AVLNode> GetEnumerator()
 {
     return(AVLNode.EnumerateRoot(_root).GetEnumerator());
 }
Esempio n. 41
0
 public void InOrderSuccessor_Leaf()
 {
     AVLNode<int> leaf = new AVLNode<int>(50);
     Assert.IsNull(leaf.InOrderSuccessor);
 }
Esempio n. 42
0
 private static void UpdateDepth(AVLNode node)
 {
     node._depthL = node.left == null ? 0 : Math.Max(node.left._depthL, node.left._depthR) + 1;
     node._depthR = node.right == null ? 0 : Math.Max(node.right._depthL, node.right._depthR) + 1;
 }
Esempio n. 43
0
        public void InOrderSuccessor_RootRight()
        {
            AVLNode<int> rootRight = new AVLNode<int>(50)
            {
                Right = new AVLNode<int>(75)
            };

            Assert.AreEqual<int>(75, rootRight.InOrderSuccessor.Value);
        }
Esempio n. 44
0
 private AVLNode Double_Rotate_Right(AVLNode p)
 {
     p.right = Rotate_Right(p.right);
     p       = Rotate_Left(p);
     return(p);
 }
Esempio n. 45
0
    public AVLNode Delete(AVLNode p, int x)
    {
        AVLNode q, r, w;

        if (p != null)                      // daca nodul curent este diferit de NULL
        {
            if (x < p.key)                  //cheia care se doreste stearsa este mai mica decat informatia din nod
            {
                p.left = Delete(p.left, x); // se cauta cheia de sters in subarborele stang al nodului curent
            }
            else
            if (x > p.key)                    // daca cheia este mai mare
            {
                p.right = Delete(p.right, x); // se cauta in subarborele drept
            }
            else
            {                        //daca cheia este egala cu informatia din nodul curent
                Debug.Log("yess");
                q = p;               //un nod q devine p
                if (q.right == null) // daca copilul drept al lui q eate NULL
                {
                    p = q.left;      // atunci p devine q->stanga
                }
                else
                if (q.left == null)                 //altfel daca copilul stang al lui q este NULL
                {
                    p = q.right;                    // p devine q->dreapta
                }
                else
                {
                    w = q.left;          //altfel w este copilul stanga al lui q
                    r = q;               // r devine q
                    if (w.right != null) // daca copilul drept al lui w nun este NULL
                    {
                        while (w.right != null)
                        {
                            r = w;
                            w = w.right;
                        }
                        p.key   = w.key;
                        q       = w;
                        r.right = w.left;
                        r       = p.left;
                        w       = w.left;
                        if (r != null)
                        {
                            while ((r != w) && (r != null))
                            {
                                r = echilibrare(r);
                                r = r.right;
                            }
                        }
                    }
                    else
                    {
                        p.key  = w.key;
                        p.left = w.left;
                        q      = w;
                    }
                }
                q = null;        // se sterge q
            }
        }
        if (p != null)
        {
            p = echilibrare(p);             // se echilibreaza p daca nu este NULL
        }
        else
        {
            p = new AVLNode();
        }
        return(p);
    }
Esempio n. 46
0
        private static void ViewAVLLayer(AVLNode<int, int> node, Queue<AVLNode<int, int>> que)
        {
            if (node.Left != null)
                que.Enqueue(node.Left);
            if (node.Right != null)
                que.Enqueue(node.Right);
            Console.Write("{0}   ", node.Key);

            if (que.Count > 0)
            {
                ViewAVLLayer(que.Dequeue(), que);
            }
        }