示例#1
0
 public TValue this[TKey key]
 {
     get
     {
         return(Search(key));
     }
     set
     {
         NodeAVL <TKey, TValue> current = root;
         while (current != null)
         {
             if (key.Equals(current.Key))
             {
                 current.Value = value;
             }
             else if (key.CompareTo(current.Key) > 0)
             {
                 current = current.RightChild;
             }
             else
             {
                 current = current.LeftChild;
             }
         }
         if (current == null)
         {
             throw new Exception("That key doesn't exist.");
         }
     }
 }
示例#2
0
        public void Balance(NodeAVL <TKey, TValue> node)
        {
            if (node == null)
            {
                return;
            }
            if (node.BalancFactor < -1 && node.RightChild.BalancFactor == -1)
            {
                LeftRotation(node.RightChild.RightChild);
            }
            else if (node.BalancFactor < -1 && node.RightChild.BalancFactor == 1)
            {
                RightRotation(node.RightChild.LeftChild);
            }
            else if (node.BalancFactor < -1)
            {
                LeftRotation(node.RightChild);
            }
            else if (node.BalancFactor > 1 && node.LeftChild.BalancFactor == -1)
            {
                LeftRotation(node.LeftChild.RightChild);
            }
            else if (node.BalancFactor > 1 && node.LeftChild.BalancFactor == 1)
            {
                RightRotation(node.LeftChild.LeftChild);
            }
            else if (node.BalancFactor > 1)
            {
                RightRotation(node.LeftChild);
            }

            Balance(node.Parent);
        }
示例#3
0
        public bool Contains(KeyValuePair <TKey, TValue> item)
        {
            NodeAVL <TKey, TValue> current = this.root;

            while (current != null)
            {
                if (current.Key.Equals(item.Key))
                {
                    if (item.Value.Equals(current.Value))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (current.Key.CompareTo(item.Key) > 0)
                {
                    current = current.LeftChild;
                }
                else
                {
                    current = current.RightChild;
                }
            }

            return(false);
        }
示例#4
0
        public void Add(TKey key, TValue value)
        {
            NodeAVL <TKey, TValue> node = new NodeAVL <TKey, TValue>(key, value);
            NodeAVL <TKey, TValue> current;

            if (this.root == null)
            {
                this.root = node;
            }
            else
            {
                current = this.root;
                while (current.LeftChild != null && current.RightChild != null)
                {
                    if (current.Key.CompareTo(key) > 0)
                    {
                        current = current.LeftChild;
                    }
                    else
                    {
                        current = current.RightChild;
                    }
                }
                if (current.Key.CompareTo(key) > 0 && current.LeftChild == null)
                {
                    current.LeftChild = node;
                    node.Parent       = current;
                    return;
                }
                else if (current.Key.CompareTo(key) <= 0 && current.RightChild == null)
                {
                    current.RightChild = node;
                    node.Parent        = current;
                    return;
                }
                else if (current.Key.CompareTo(key) > 0 && current.LeftChild != null)
                {
                    current = current.LeftChild;
                }
                else
                {
                    current = current.RightChild;
                }
                if (current.Key.CompareTo(key) > 0)
                {
                    current.LeftChild = node;
                    node.Parent       = current;
                    current           = node;
                    RightRotation(current);
                }
                else
                {
                    current.RightChild = node;
                    node.Parent        = current;
                    current            = node;
                    LeftRotation(current);
                }
            }
        }
示例#5
0
 public bool IsLeaf(NodeAVL <TKey, TValue> node)
 {
     if (node.LeftChild == null && node.RightChild == node)
     {
         return(true);;
     }
     return(false);
 }
示例#6
0
 public void PrintInside(NodeAVL <TKey, TValue> element)
 {
     if (element != null)
     {
         Console.WriteLine(element.Value + " " + element.Key + " " + element.Height + " " + element.BalancFactor);
         Console.WriteLine("Left Child: ");
         PrintInside(element.LeftChild);
         Console.WriteLine("Right Child: ");
         PrintInside(element.RightChild);
     }
 }
示例#7
0
 public void RightRotation(NodeAVL <TKey, TValue> current)
 {
     if (current == null || current.Parent == null)
     {
         return;
     }
     if (current.Parent.Parent == null)
     {
         current.Parent.LeftChild  = current.RightChild;
         current.RightChild        = current.Parent;
         current.Parent            = null;
         current.RightChild.Parent = current;
         root = current;
     }
     else if (current.Parent.Parent.LeftChild == current.Parent)
     {
         current = current.Parent;
         if (current.Parent != root)
         {
             if (current.Parent.Parent.LeftChild == current.Parent)
             {
                 current.Parent.Parent.LeftChild = current;
             }
             else
             {
                 current.Parent.Parent.RightChild = current;
             }
             current.Parent.LeftChild  = current.RightChild;
             current.RightChild        = current.Parent;
             current.Parent            = current.Parent.Parent;
             current.RightChild.Parent = current;
         }
         else
         {
             current.Parent.LeftChild  = current.RightChild;
             current.RightChild        = current.Parent;
             current.Parent            = current.Parent.Parent;
             current.RightChild.Parent = current;
             root = current;
         }
     }
     else
     {
         current.Parent.Parent.RightChild = current;
         current.Parent.LeftChild         = current.LeftChild;
         current.RightChild        = current.Parent;
         current.Parent            = current.Parent.Parent;
         current.RightChild.Parent = current;
         current = current.RightChild;
         LeftRotation(current);
     }
 }
示例#8
0
        /// <summary>
        /// Method to find the value with given key.
        /// It returns value if tree contains node with that , and default value otherwise.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TValue Search(TKey key)
        {
            NodeAVL <TKey, TValue> current = root;

            while (current != null)
            {
                if (key.Equals(current.Key))
                {
                    return(current.Value);
                }
                else if (key.CompareTo(current.Key) > 0)
                {
                    current = current.RightChild;
                }
                else
                {
                    current = current.LeftChild;
                }
            }
            return(default(TValue));
        }
示例#9
0
        public bool ContainsKey(TKey key)
        {
            NodeAVL <TKey, TValue> current = this.root;

            while (current != null)
            {
                if (current.Key.Equals(key))
                {
                    return(true);
                }
                else if (current.Key.CompareTo(key) > 0)
                {
                    current = current.LeftChild;
                }
                else
                {
                    current = current.RightChild;
                }
            }

            return(false);
        }
示例#10
0
        public NodeAVL <TKey, TValue> Delete(TKey key)
        {
            NodeAVL <TKey, TValue> node;
            NodeAVL <TKey, TValue> current;
            NodeAVL <TKey, TValue> toReturn;

            if (root.Key.Equals(key))
            {
                if (IsLeaf(root))
                {
                    root = null;
                    return(null);
                }
                else if (root.RightChild != null)
                {
                    node    = this.root;
                    current = root.RightChild;
                    while (current.LeftChild != null)
                    {
                        current = current.LeftChild;
                    }

                    if (current.Parent.LeftChild == current)
                    {
                        toReturn = current.Parent;
                        current.Parent.LeftChild = current.RightChild;
                        root            = current;
                        root.RightChild = node.RightChild;
                    }
                    else
                    {
                        toReturn        = root;
                        root            = current;
                        root.RightChild = current.RightChild;
                    }

                    root.LeftChild = node.LeftChild;
                    return(toReturn);
                }
                else
                {
                    root = root.LeftChild;
                    return(root);
                }
            }
            else
            {
                current = root;
                while (current != null && !current.Key.Equals(key))
                {
                    if (current.Key.CompareTo(key) > 0)
                    {
                        current = current.LeftChild;
                    }
                    else
                    {
                        current = current.RightChild;
                    }
                }
                if (current == null)
                {
                    return(null);
                }
                else if (IsLeaf(current))
                {
                    if (current.Parent.LeftChild == current)
                    {
                        current.Parent.LeftChild = null;
                    }
                    else
                    {
                        current.Parent.RightChild = null;
                    }
                    current = current.Parent;
                }
                else if (current.RightChild != null && current.LeftChild != null)
                {
                    node = current.RightChild;
                    while (node.LeftChild != null)
                    {
                        node = node.LeftChild;
                    }
                    toReturn = node.Parent;
                    current.LeftChild.Parent = node;
                    if (node.Parent.LeftChild == node)
                    {
                        node.Parent.LeftChild     = node.RightChild;
                        current.RightChild.Parent = node;
                        node.RightChild           = current.RightChild;
                    }

                    if (current.Parent.LeftChild == current)
                    {
                        current.Parent.LeftChild = node;
                    }
                    else
                    {
                        current.Parent.RightChild = node;
                    }
                    node.Parent    = current.Parent;
                    node.LeftChild = current.LeftChild;
                    return(toReturn);
                }
                else if (current.RightChild != null)
                {
                    if (current.Parent.LeftChild == current)
                    {
                        current.Parent.LeftChild = current.RightChild;
                    }
                    else
                    {
                        current.Parent.RightChild = current.RightChild;
                    }
                    if (current.RightChild != null)
                    {
                        current.RightChild.Parent = current.Parent;
                    }
                    return(current.Parent);
                }
                else
                {
                    if (current.Parent.LeftChild == current)
                    {
                        current.Parent.LeftChild = current.LeftChild;
                    }
                    else
                    {
                        current.Parent.RightChild = current.LeftChild;
                    }
                    if (current.LeftChild != null)
                    {
                        current.LeftChild.Parent = current.Parent;
                    }
                    return(current.Parent);
                }
            }
            return(current);
        }
示例#11
0
 public void Clear()
 {
     root = null;
 }
示例#12
0
 public AVLDictionary(TKey key, TValue value)
 {
     root = new NodeAVL <TKey, TValue>(key, value);
 }