示例#1
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);
        }
示例#2
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);
        }
示例#3
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));
        }
示例#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))
            {
                root = Remove(root, item);
                return(true);
            }
            return(false);
        }
示例#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));
        }
示例#6
0
        public AVL_Node <T> Contains(T item)
        {
            AVL_Node <T> x = 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;
            }
        }
示例#7
0
        public bool Contains(IComparable item, Func <T, IComparable> func, out AVL_Node <T> output)
        {
            AVL_Node <T> x = root;

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

                x = item.CompareTo(func(x.Key)) < 0 ? x.Left : x.Rigth;
            }
        }
示例#8
0
 /// <summary>
 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />.
 /// </summary>
 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only. </exception>
 public void Clear()
 {
     root  = null;
     Count = 0;
 }
示例#9
0
 public bool Contains(T item, out AVL_Node <T> output)
 {
     output = Contains(item);
     return(output == null ? false : true);
 }
示例#10
0
 /// <summary>
 /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
 /// </summary>
 /// <param name="item">The object to add to 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 void Add(T item)
 {
     root = Insert(root, item);
 }
示例#11
0
 public AVL_Tree()
 {
     root = null;
 }