Ejemplo n.º 1
0
 private void BFS(AVLCountNodeKey <TKey, TValue> currentNode, LinkedList <KeyValuePair <TKey, TValue> > result)
 {
     if (currentNode == null)
     {
         return;
     }
     if (currentNode.IsLeaf)
     {
         foreach (var item in currentNode.Values.Select(v => new KeyValuePair <TKey, TValue> {
             Key = currentNode.Key, Value = v.Value
         }))
         {
             result.AddLast(item);
         }
     }
     else
     {
         BFS(currentNode.LeftChild, result);
         foreach (var item in currentNode.Values.Select(v => new KeyValuePair <TKey, TValue> {
             Key = currentNode.Key, Value = v.Value
         }))
         {
             result.AddLast(item);
         }
         BFS(currentNode.RigthChild, result);
     }
 }
Ejemplo n.º 2
0
        private void UpdateCount(AVLCountNodeKey <TKey, TValue> node)
        {
            if (node == null)
            {
                return;
            }

            var currentNode = node;

            while (currentNode != null)
            {
                var currentNodeLeft  = currentNode.LeftChild;
                var currentNodeRight = currentNode.RigthChild;

                if (currentNode.Count != (currentNodeLeft != null ? currentNodeLeft.Count : 0) + (currentNodeRight != null ? currentNodeRight.Count : 0) + (uint)currentNode.Values.Count)
                {
                    currentNode.Count = (currentNodeLeft != null ? currentNodeLeft.Count : 0) + (currentNodeRight != null ? currentNodeRight.Count : 0) + (uint)currentNode.Values.Count;
                }
                else
                {
                    break;
                }
                currentNode = currentNode.Father;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Search Less Than or Equals To
        /// </summary>
        /// <param name="key"></param>
        /// <param name="resultNode"></param>
        /// <returns></returns>
        public bool SearchLTE(TKey key, out AVLCountNodeKey <TKey, TValue> resultNode)
        {
            resultNode = null;
            AVLCountNodeKey <TKey, TValue> lastNotNull = root;

            while (lastNotNull != null)
            {
                var result = Comparer.Compare(lastNotNull.Key, key);
                if (result == 0)
                {
                    resultNode = lastNotNull;
                    return(true);
                }
                else if (result < 0)
                {
                    resultNode = lastNotNull;
                    if (lastNotNull.RigthChild == null)
                    {
                        break;
                    }
                    lastNotNull = lastNotNull.RigthChild;
                }
                else
                {
                    if (lastNotNull.LeftChild == null)
                    {
                        break;
                    }
                    lastNotNull = lastNotNull.LeftChild;
                }
            }
            return(resultNode != null);
        }
Ejemplo n.º 4
0
        private ulong CountLessThan(AVLCountNodeKey <TKey, TValue> node, TKey value)
        {
            if (node == null)
            {
                return(0);
            }

            ulong result = 0;

            while (node != null)
            {
                var compResult = Comparer.Compare(value, node.Key);
                if (compResult > 0)
                {
                    var leftChild = node.LeftChild as AVLCountNodeKey <TKey, TValue>;
                    result += (leftChild != null ? leftChild.Count : 0) + (ulong)node.Values.Count;
                    node    = node.RigthChild;
                }
                else
                {
                    node = node.LeftChild;
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        private void UpdateHeightFrom(AVLCountNodeKey <TKey, TValue> node)
        {
            if (node == null)
            {
                return;
            }

            var nodeCurrent    = node;
            int maxchildHeight = nodeCurrent.LeftChild == null ? -1 : nodeCurrent.LeftChild.Height;

            maxchildHeight = (nodeCurrent.RigthChild != null && maxchildHeight < nodeCurrent.RigthChild.Height) ? nodeCurrent.RigthChild.Height : maxchildHeight;
            while (maxchildHeight + 1 != nodeCurrent.Height)
            {
                nodeCurrent.Height = maxchildHeight + 1;
                nodeCurrent        = nodeCurrent.Father;
                if (nodeCurrent != null)
                {
                    maxchildHeight = nodeCurrent.LeftChild == null ? -1 : nodeCurrent.LeftChild.Height;
                    maxchildHeight = (nodeCurrent.RigthChild != null && maxchildHeight < nodeCurrent.RigthChild.Height) ? nodeCurrent.RigthChild.Height : maxchildHeight;
                }
                else
                {
                    break;
                }
            }

            if (UpdateSomeThingLikeHeightFrom != null)
            {
                UpdateSomeThingLikeHeightFrom(node);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Cambia los padres cuando no son padres e hijos entre ellos
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        private void SwapFathersEspecial(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2)
        {
            var father1           = node1.Father;
            var father2           = node2.Father;
            var node1IsRightChild = node1.IsRightChild();
            var node2IsRightChild = node2.IsRightChild();

            node2.Father = father1;
            if (node1IsRightChild)
            {
                father1.RigthChild = node2;
            }
            else if (father1 != null)
            {
                father1.LeftChild = node2;
            }

            node1.Father = father2;
            if (node2IsRightChild)
            {
                father2.RigthChild = node1;
            }
            else if (father2 != null)
            {
                father2.LeftChild = node1;
            }
        }
Ejemplo n.º 7
0
        private void SwapWithOneFatherOfTheOther(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2)
        {
            var node1Father       = node1.Father;
            var node1IsRightChild = node1.IsRightChild();
            var node2IsRightChild = node2.IsRightChild();
            var node1RightChild   = node1.RigthChild;
            var node2RightChild   = node2.RigthChild;
            var node1LeftChild    = node1.LeftChild;
            var node2LeftChild    = node2.LeftChild;

            node1.Father = node2;
            node2.Father = node1Father;

            if (node1IsRightChild)
            {
                node2.Father.RigthChild = node2;
            }
            else
            {
                if (node1Father != null)
                {
                    node2.Father.LeftChild = node2;
                }
            }

            if (node2IsRightChild)
            {
                node2.RigthChild = node1;
                node2.LeftChild  = node1LeftChild;
                if (node2.LeftChild != null)
                {
                    node2.LeftChild.Father = node2;
                }
            }
            else
            {
                node2.LeftChild  = node1;
                node2.RigthChild = node1RightChild;
                if (node2.RigthChild != null)
                {
                    node2.RigthChild.Father = node2;
                }
            }

            node1.RigthChild = node2RightChild;
            if (node1.RigthChild != null)
            {
                node1.RigthChild.Father = node1;
            }
            node1.LeftChild = node2LeftChild;
            if (node1.LeftChild != null)
            {
                node1.LeftChild.Father = node1;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Insertar y permite tener llaves repetidas . Almacena una lista
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public AVLCountNodeKey <TKey, TValue> InsertRepeat(TKey key, TValue value)
        {
            if (root == null)
            {
                root = new AVLCountNodeKey <TKey, TValue>(key, value);
                Count++;
                root.Count = 1;
                return(root);
            }

            AVLCountNodeKey <TKey, TValue> lastNotNull;

            if (Search(key, out lastNotNull))
            {
                lastNotNull.Values.AddLast(new AVLCountNodeValue <TValue, AVLCountNodeKey <TKey, TValue> >
                {
                    Father = lastNotNull
                    ,
                    Value = value
                });
                Count++;
                UpdateCount(lastNotNull);
                return(lastNotNull);
            }
            else
            {
                AVLCountNodeKey <TKey, TValue> result = null;
                var resultCompare = Comparer.Compare(lastNotNull.Key, key);
                if (resultCompare < 0)
                {
                    lastNotNull.RigthChild = new AVLCountNodeKey <TKey, TValue>(key, value)
                    {
                        Father = lastNotNull, Count = 1
                    };
                    result = lastNotNull.RigthChild;
                }
                else
                {
                    lastNotNull.LeftChild = new AVLCountNodeKey <TKey, TValue>(key, value)
                    {
                        Father = lastNotNull, Count = 1
                    };
                    result = lastNotNull.LeftChild;
                }
                UpdateHeightFrom(lastNotNull);
                UpdateBalanceAndCount(lastNotNull);
                Count++;
                return(result);
            }
        }
Ejemplo n.º 9
0
        internal void UpdateFather(AVLCountNodeKey <TKey, TValue> node)
        {
            if (Father == null)
            {
                return;
            }

            if (Father.LeftChild == node)
            {
                Father.LeftChild = this;
            }
            else
            {
                Father.RigthChild = this;
            }
        }
Ejemplo n.º 10
0
        private void UpdateBalanceAndCount(AVLCountNodeKey <TKey, TValue> node)
        {
            if (node == null)
            {
                return;
            }
            var nodeCount = node;

            UpdateCount(nodeCount);

            while (node != null && node.Balance == Balance.Balanced)
            {
                node = node.Father;
            }

            if (node == null)
            {
                return;
            }

            if (node.Balance == Balance.LeftUnBalanced)
            {
                if (node.LeftChild.SimpleBalance == Balance.RightUnBalanced)
                {
                    node.LeftChild.RotateToLeft();
                }
                node.RotateToRight();
                if (node == root)
                {
                    root = node.Father;
                }
            }
            else if (node.Balance == Balance.RightUnBalanced)
            {
                if (node.RigthChild.SimpleBalance == Balance.LeftUnBalanced)
                {
                    node.RigthChild.RotateToRight();
                }
                node.RotateToLeft();
                if (node == root)
                {
                    root = node.Father;
                }
            }
            UpdateHeightFrom(node.Father.Father);
        }
Ejemplo n.º 11
0
        public AVLCountNodeKey <TKey, TValue> Maximum(AVLCountNodeKey <TKey, TValue> avlNodeRoot)
        {
            if (avlNodeRoot == null)
            {
                return(null);
            }
            AVLCountNodeKey <TKey, TValue> lastNotNull = avlNodeRoot;

            while (true)
            {
                if (lastNotNull.RigthChild != null)
                {
                    lastNotNull = lastNotNull.RigthChild;
                    continue;
                }
                return(lastNotNull);
            }
        }
Ejemplo n.º 12
0
        private void SwapRightChildEspecial(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2)
        {
            var node1RightChild = node1.RigthChild;
            var node2RightChild = node2.RigthChild;

            node1.RigthChild = node2RightChild;
            if (node1.RigthChild != null)
            {
                node1.RigthChild.Father = node1;
            }


            node2.RigthChild = node1RightChild;
            if (node2.RigthChild != null)
            {
                node2.RigthChild.Father = node2;
            }
        }
Ejemplo n.º 13
0
        private void SwapLeftChildEspecial(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2)
        {
            var node1LeftChild = node1.LeftChild;
            var node2LeftChild = node2.LeftChild;

            node1.LeftChild = node2LeftChild;
            if (node1.LeftChild != null)
            {
                node1.LeftChild.Father = node1;
            }


            node2.LeftChild = node1LeftChild;
            if (node2.LeftChild != null)
            {
                node2.LeftChild.Father = node2;
            }
        }
Ejemplo n.º 14
0
        void Swap(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2)
        {
            if (node1 == root)
            {
                root = node2;
            }
            if (node2 == root)
            {
                root = node1;
            }

            var node1IsRightChild = node1.IsRightChild();
            var node2IsRightChild = node2.IsRightChild();
            var node1Height       = node1.Height;

            node1.Height = node2.Height;
            node2.Height = node1Height;

            var node1Father = node1.Father;
            var node2Father = node2.Father;

            var node1LeftChild = node1.LeftChild;
            var node2LeftChild = node2.LeftChild;

            var node1RightChild = node1.RigthChild;
            var node2RightChild = node2.RigthChild;

            if (!((node1.Father == node2) || (node2.Father == node1)))
            {
                SwapFathersEspecial(node1, node2);
                SwapRightChildEspecial(node1, node2);
                SwapLeftChildEspecial(node1, node2);
            }

            else if (node1.Father == node2)
            {
                SwapWithOneFatherOfTheOther(node2, node1);
            }
            else
            {
                SwapWithOneFatherOfTheOther(node1, node2);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Insertar si key no existe en el MyAVLTree
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public AVLCountNodeKey <TKey, TValue> Insert(TKey key, TValue value)
        {
            if (root == null)
            {
                root = new AVLCountNodeKey <TKey, TValue>(key, value);
                Count++;
                this.root.Count = 1;
                return(root);
            }

            AVLCountNodeKey <TKey, TValue> lastNotNull;

            if (Search(key, out lastNotNull))
            {
                throw new Exception("This key has been inserted");
            }
            AVLCountNodeKey <TKey, TValue> result = null;
            var resultCompare = Comparer.Compare(lastNotNull.Key, key);

            if (resultCompare < 0)
            {
                lastNotNull.RigthChild = new AVLCountNodeKey <TKey, TValue>(key, value)
                {
                    Father = lastNotNull, Count = 1
                };
                result = lastNotNull.RigthChild;
            }
            else
            {
                lastNotNull.LeftChild = new AVLCountNodeKey <TKey, TValue>(key, value)
                {
                    Father = lastNotNull, Count = 1
                };
                result = lastNotNull.LeftChild;
            }
            UpdateHeightFrom(lastNotNull);
            UpdateBalanceAndCount(lastNotNull);
            Count++;
            return(result);
        }
Ejemplo n.º 16
0
        public bool TryRemove(TKey key, bool justOneElement)
        {
            AVLCountNodeKey <TKey, TValue> lastNotNull;

            if (Search(key, out lastNotNull))
            {
                if (justOneElement && lastNotNull.Values.Count > 1)
                {
                    lastNotNull.Values.RemoveFirst();
                    UpdateCount(lastNotNull);
                    Count--;
                    return(true);
                }
                var candidate = Minimum(lastNotNull.RigthChild);
                if (candidate != null)
                {
                    if (candidate.RigthChild != null)
                    {
                        var aux = candidate.RigthChild;
                        Swap(candidate, candidate.RigthChild);
                        UpdateCount(aux);
                    }
                }
                else
                {
                    candidate = Maximum(lastNotNull.LeftChild);
                    if (candidate != null && candidate.LeftChild != null)
                    {
                        var aux = candidate.LeftChild;
                        Swap(candidate, candidate.LeftChild);
                        UpdateCount(aux);
                    }
                }

                if (candidate != null)
                {
                    Swap(candidate, lastNotNull);
                    var lastNotFullFather = lastNotNull.Father;
                    if (lastNotNull.IsRightChild())
                    {
                        lastNotNull.Father.RigthChild = null;
                    }
                    else if (lastNotNull.Father != null)
                    {
                        lastNotNull.Father.LeftChild = null;
                    }
                    UpdateHeightFrom(lastNotFullFather);
                    UpdateBalanceAndCount(lastNotFullFather);
                }
                else
                {
                    var lastnotNUllFather = lastNotNull.Father;
                    if (lastNotNull.IsRightChild())
                    {
                        lastNotNull.Father.RigthChild = null;
                    }
                    else if (lastNotNull.Father != null)
                    {
                        lastNotNull.Father.LeftChild = null;
                    }
                    if (root == lastNotNull)
                    {
                        root = null;
                    }

                    UpdateHeightFrom(lastnotNUllFather);
                    UpdateBalanceAndCount(lastnotNUllFather);
                }

                Count--;
                return(true);
            }
            return(false);
        }