Exemplo n.º 1
0
        //public void Add(T value)
        //{
        //    BinaryTreeElement<T> element = new BinaryTreeElement<T>(value);

        //    if (root == null)
        //    {
        //        root = element;
        //        return;
        //    }

        //    var iterator = root;

        //    while (true)
        //    {

        //        if (iterator.Value.CompareTo(value) == 1)
        //        {
        //            if (iterator.Left != null)
        //                iterator = iterator.Left;
        //            else
        //            {
        //                iterator.Left = element;
        //                element.Parent = iterator;
        //                if (element.Value.CompareTo(first.Value) == -1)
        //                    first = element;
        //                break;
        //            }
        //        }
        //        else if (iterator.Value.CompareTo(value) == -1)
        //        {
        //            if (iterator.Right != null)
        //                iterator = iterator.Right;
        //            else
        //            {
        //                iterator.Right = element;
        //                element.Parent = iterator;
        //                if (element.Value.CompareTo(last.Value) == 1)
        //                    last = element;
        //                break;
        //            }
        //        }
        //    }
        //}

        internal BinaryTreeElement <T> RecursiveAdd(BinaryTreeElement <T> previous, T value)
        {
            if (value.CompareTo(previous.Value) == -1)
            {
                if (previous.Left == null)
                {
                    BinaryTreeElement <T> el = new BinaryTreeElement <T>(value);
                    previous.Left = el;
                    el.Parent     = previous;

                    return(el);
                }
                else
                {
                    return(RecursiveAdd(previous.Left, value));
                }
            }
            if (value.CompareTo(previous.Value) == 1)
            {
                if (previous.Right == null)
                {
                    BinaryTreeElement <T> el = new BinaryTreeElement <T>(value);
                    previous.Right = el;
                    el.Parent      = previous;

                    return(el);
                }
                else
                {
                    return(RecursiveAdd(previous.Right, value));
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        private BinaryTreeElement <T> GetElement(BinaryTreeElement <T> iterator, T value)
        {
            if (iterator.Value.Equals(value))
            {
                return(iterator);
            }
            if (iterator.Value.CompareTo(value) == 1)
            {
                if (iterator.Left != null)
                {
                    return(GetElement(iterator.Left, value));
                }
                else
                {
                    return(null);
                }
            }
            else

            if (iterator.Right != null)
            {
                return(GetElement(iterator.Right, value));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        private void RotateRight(BinaryTreeElement <T> itr)
        {
            var esquerda = itr.Left;

            recalcHeigth = false;
            itr.Height--;
            itr.Height--;

            if (itr == root)
            {
                root            = esquerda;
                esquerda.Parent = null;
            }
            else
            if (itr.Parent.Left != null & itr.Parent.Left == itr)
            {
                itr.Parent.Left = esquerda;
                esquerda.Parent = itr.Parent;
            }
            else
            if (itr.Parent.Right != null && itr.Parent.Right == itr)
            {
                itr.Parent.Right = esquerda;
                esquerda.Parent  = itr.Parent;
            }
            itr.Left   = esquerda.Right;
            itr.Parent = esquerda;

            if (esquerda.Right != null)
            {
                esquerda.Right.Parent = itr;
            }
            esquerda.Right = itr;
        }
Exemplo n.º 4
0
        private void RotateLeft(BinaryTreeElement <T> itr)
        {
            var direita = itr.Right;

            recalcHeigth = false;
            itr.Height--;
            itr.Height--;

            if (itr == root)
            {
                root           = direita;
                direita.Parent = null;
            }
            else
            if (itr.Parent.Left != null & itr.Parent.Left == itr)
            {
                itr.Parent.Left = direita;
                direita.Parent  = itr.Parent;
            }
            else
            if (itr.Parent.Right != null && itr.Parent.Right == itr)
            {
                itr.Parent.Right = direita;
                direita.Parent   = itr.Parent;
            }
            itr.Right  = direita.Left;
            itr.Parent = direita;

            if (direita.Left != null)
            {
                direita.Left.Parent = itr;
            }

            direita.Left = itr;
        }
Exemplo n.º 5
0
 private BinaryTreeElement <T> getHighest(BinaryTreeElement <T> root)
 {
     if (root.Right != null)
     {
         return(getHighest(root.Right));
     }
     return(root);
 }
Exemplo n.º 6
0
 private void PreOrder(BinaryTreeElement <T> iterator)
 {
     preOrderBuffer.Add(iterator.Value);
     if (iterator.Left != null)
     {
         PreOrder(iterator.Left);
     }
     if (iterator.Right != null)
     {
         PreOrder(iterator.Right);
     }
 }
Exemplo n.º 7
0
 private void PrintInOrder2(BinaryTreeElement <T> previous)
 {
     if (previous.Left != null)
     {
         PrintInOrder2(previous.Left);
     }
     z.Add(previous.Value);
     if (previous.Right != null)
     {
         PrintInOrder2(previous.Right);
     }
 }
Exemplo n.º 8
0
 public void PrintInOrder1(BinaryTreeElement <T> iterator)
 {
     if (iterator.Left != null)
     {
         PrintInOrder1(iterator.Left);
     }
     x.Add(iterator.Value);
     if (iterator.Right != null)
     {
         PrintInOrder1(iterator.Right);
     }
 }
Exemplo n.º 9
0
        public void TryRotate(BinaryTreeElement <T> itr, bool isRemoving = false)
        {
            if (child.Height == itr.Height && !isRemoving)
            {
                itr.Height++;
            }

            if (itr.Left != null && itr.Height - itr.Left.Height > 2 || (itr.Left == null && itr.Height > 2))
            {
                if (isRemoving)
                {
                    itr.Height++;
                    itr.Right.Height++;
                }
                else
                if (added.CompareTo(itr.Right.Value) == -1)
                {
                    itr.Right.Height++;
                    itr.Right.Left.Height++;
                    RotateRight(itr.Right);
                }

                RotateLeft(itr);
            }
            else

            if (itr.Right != null && itr.Height - itr.Right.Height > 2 || (itr.Right == null && itr.Height > 2))
            {
                if (isRemoving)
                {
                    itr.Height++;
                    itr.Left.Height++;
                }
                else
                if (added.CompareTo(itr.Left.Value) == 1)
                {
                    itr.Left.Height++;
                    itr.Left.Right.Height++;
                    RotateLeft(itr.Left);
                }

                RotateRight(itr);
            }
            else if (isRemoving)
            {
                itr.Height--;
            }

            child = itr;
        }
Exemplo n.º 10
0
 public void RecursiveAdd(params T[] value)
 {
     foreach (var x in value)
     {
         if (root == null)
         {
             BinaryTreeElement <T> el = new BinaryTreeElement <T>(x);
             root = el;
         }
         else
         {
             RecursiveAdd(root, x);
         }
     }
 }
Exemplo n.º 11
0
        private void Remove(BinaryTreeElement <T> element)
        {
            var highest = getHighest(element);

            element.Value = highest.Value;

            if (highest.Parent.Right == highest)
            {
                highest.Parent.Right = null;
            }
            if (highest.Parent.Left == highest)
            {
                highest.Parent.Left = null;
            }
        }
Exemplo n.º 12
0
        public IEnumerable <T> RecursiveInOrder(BinaryTreeElement <T> iterator)
        {
            if (iterator.Left != null)
            {
                foreach (var x in RecursiveInOrder(iterator.Left))
                {
                    yield return(x);
                }
            }
            yield return(iterator.Value);

            if (iterator.Right != null)
            {
                foreach (var x in RecursiveInOrder(iterator.Right))
                {
                    yield return(x);
                }
            }
        }
Exemplo n.º 13
0
        internal new void RecursiveAdd(BinaryTreeElement <T> previous, T value)
        {
            BinaryTreeElement <T> el = new BinaryTreeElement <T>(value);

            if (value.CompareTo(previous.Value) == -1)
            {
                if (previous.Left == null)
                {
                    previous.Left = el;
                    el.Parent     = previous;
                    Console.WriteLine(value);
                    added = el.Value;
                    child = el;
                }
                else
                {
                    RecursiveAdd(previous.Left, value);
                }
            }
            if (value.CompareTo(previous.Value) == 1)
            {
                if (previous.Right == null)
                {
                    previous.Right = el;
                    el.Parent      = previous;
                    Console.WriteLine(value);
                    added = el.Value;
                    child = el;
                }
                else
                {
                    RecursiveAdd(previous.Right, value);
                }
            }
            if (recalcHeigth)
            {
                Console.WriteLine("Voltando:" + previous.Value);
                TryRotate(previous);
            }
        }
Exemplo n.º 14
0
        private void Remove(BinaryTreeElement <T> iterator, T value)
        {
            if (iterator.Value.Equals(value))
            {
                Remove(iterator);
            }
            else
            if (iterator.Value.CompareTo(value) == 1)
            {
                if (iterator.Left != null)
                {
                    Remove(iterator.Left, value);
                }
                else
                {
                    throw new Exception();
                }
            }

            else
            if (iterator.Value.CompareTo(value) == -1)
            {
                if (iterator.Right != null)
                {
                    Remove(iterator.Right, value);
                }
                else
                {
                    throw new Exception();
                }
            }

            if (recalcHeigth)
            {
                TryRotate(iterator, true);
            }
        }