Exemple #1
0
        private AVL_Node <T> Rotate(AVL_Node <T> x, Direction direction)
        {
            AVL_Node <T> y;

            if (direction == Direction.Left) // left
            {
                if (x == null || x.Rigth == null)
                {
                    return(x);
                }

                y       = x.Rigth;
                x.Rigth = y.Left;
                y.Left  = x;
            }
            else // right
            {
                if (x == null || x.Left == null)
                {
                    return(x);
                }

                y       = x.Left;
                x.Left  = y.Rigth;
                y.Rigth = x;
            }

            x.Update();
            y.Update();

            return(y);
        }
Exemple #2
0
        private AVL_Node <T> Insert(AVL_Node <T> x, T item)
        {
            if (x == null)
            {
                x = new AVL_Node <T> {
                    Key = item, Height = 1, Size = 1, Left = null, Rigth = null
                };
                Count++;
            }
            else
            {
                if (item.CompareTo(x.Key) == 0)
                {
                    return(x);
                }

                if (item.CompareTo(x.Key) < 0)
                {
                    x.Left = Insert(x.Left, item);
                }
                else
                {
                    x.Rigth = Insert(x.Rigth, item);
                }

                x = Balance(x);
            }

            return(Balance(x));
        }
Exemple #3
0
        private AVL_Node <T> Balance(AVL_Node <T> x)
        {
            x.Update();

            if (x.Balance_Factor > 1)
            {
                if (x.Rigth.Balance_Factor <= 0)
                {
                    x.Rigth = Rotate(x.Rigth, Direction.Rigth);
                }
                x = Rotate(x, Direction.Left);
            }
            else
            if (x.Balance_Factor < -1)
            {
                if (x.Left.Balance_Factor >= 0)
                {
                    x.Left = Rotate(x.Left, Direction.Left);
                }
                x = Rotate(x, Direction.Rigth);
            }

            x.Update();
            return(x);
        }
Exemple #4
0
        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1" />.
        /// </summary>
        /// <returns>
        /// true if <paramref name="item" /> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />.
        /// </returns>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.</exception>
        public bool Remove(T item)
        {
            AVL_Node <T> temp;

            if (Contains(item, out temp))
            {
                C_Root = Remove(C_Root, item);
                return(true);
            }
            return(false);
        }
Exemple #5
0
        private AVL_Node <T> Remove(AVL_Node <T> x, T item)
        {
            if (x == null)
            {
                return(x);
            }
            if (x.Key.CompareTo(item) == 0)
            {
                if (x.Left == null && x.Rigth == null)
                {
                    return(null);
                }

                if (x.Left != null && x.Rigth == null)
                {
                    return(x.Left);
                }

                if (x.Left == null && x.Rigth != null)
                {
                    return(x.Rigth);
                }

                var t = x.Rigth;
                var p = x;
                while (t.Left != null)
                {
                    p = t;
                    t = t.Left;
                }
                if (p == x)
                {
                    return(x.Rigth);
                }

                p.Left  = t.Rigth;
                t.Rigth = x.Rigth;
                t.Left  = x.Left;
                return(t);
            }

            if (x.Key.CompareTo(item) > 0)
            {
                x.Left = Remove(x.Left, item);
            }
            else
            {
                x.Rigth = Remove(x.Rigth, item);
            }

            return(Balance(x));
        }
Exemple #6
0
        /// <summary>
        /// Get max valur of tree
        /// </summary>
        /// <returns></returns>
        public T Max()
        {
            if (C_Root == null)
            {
                throw new InvalidOperationException("The tree is empty");
            }

            AVL_Node <T> x = C_Root;

            while (x.Rigth != null)
            {
                x = x.Rigth;
            }

            return(x.Key);
        }
Exemple #7
0
        ///<summary>
        ///</summary>
        ///<param name="item"></param>
        ///<returns></returns>
        public AVL_Node <T> Contains(T item)
        {
            AVL_Node <T> x = C_Root;

            for (; ;)
            {
                if (x == null)
                {
                    return(null);
                }
                if (item.CompareTo(x.Key) == 0)
                {
                    return(x);
                }

                x = item.CompareTo(x.Key) < 0 ? x.Left : x.Rigth;
            }
        }
Exemple #8
0
 ///<summary>
 ///Determine if a item is in tree
 ///</summary>
 ///<param name="item">Item to searsh</param>
 ///<param name="output">the node that contain the item if the item is on </param>
 ///<returns></returns>
 public bool Contains(T item, out AVL_Node <T> output)
 {
     output = Contains(item);
     return(output == null ? false : true);
 }