예제 #1
0
        private IEnumerable <BinaryNode <T> > Traversal(BinaryNode <T> Node)
        {
            if (Node != null)
            {
                if (Node.GetLeft() != null)
                {
                    foreach (BinaryNode <T> LeftNode in Traversal(Node.GetLeft()))
                    {
                        yield return(LeftNode);
                    }
                }

                yield return(Node);

                if (Node.GetRight() != null)
                {
                    foreach (BinaryNode <T> RightNode in Traversal(Node.GetRight()))
                    {
                        yield return(RightNode);
                    }
                }
            }
            else
            {
                yield return(null);
            }
        }
예제 #2
0
 private BinaryNode <T> SearchNode <E>(BinaryNode <T> node, Compare <E> compare, E element)
 {
     if (compare(node.Value, element) == 0)
     {
         return(node);
     }
     else
     {
         if (compare(node.Value, element) > 0)
         {
             if (node.GetLeft() == null)
             {
                 return(null);
             }
             else
             {
                 return(SearchNode(node.GetLeft(), compare, element));
             }
         }
         else
         {
             if (node.GetRight() == null)
             {
                 return(null);
             }
             else
             {
                 return(SearchNode(node.GetRight(), compare, element));
             }
         }
     }
 }
예제 #3
0
 private bool EditElement <E>(BinaryNode <T> node, Compare <E> compare, E element, T Item)
 {
     if (compare(node.Value, element) == 0)
     {
         node.Value = Item;
         return(true);
     }
     else
     {
         if (compare(node.Value, element) > 0)
         {
             if (node.GetLeft() == null)
             {
                 return(false);
             }
             else
             {
                 return(EditElement(node.GetLeft(), compare, element, Item));
             }
         }
         else
         {
             if (node.GetRight() == null)
             {
                 return(false);
             }
             else
             {
                 return(EditElement(node.GetRight(), compare, element, Item));
             }
         }
     }
 }
예제 #4
0
        private bool Eliminate2(BinaryNode <T> node)
        {
            // left
            BinaryNode <T> TempLeft = node.GetPadre().GetLeft();
            // right
            BinaryNode <T> TempRight = node.GetPadre().GetRight();

            BinaryNode <T> actual = node.GetLeft() != null?node.GetLeft() : node.GetRight();

            if (TempLeft == node)
            {
                node.GetPadre().SetLeft(actual);

                actual.SetPadre(node.GetPadre());
                node.SetRight(null);
                node.SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == node)
            {
                node.GetPadre().SetRight(actual);

                /* Eliminando todas las referencias hacia el nodo */
                actual.SetPadre(node.GetPadre());
                node.SetRight(null);
                node.SetLeft(null);
                count--;
                return(true);
            }

            return(false);
        }
예제 #5
0
        public bool Eliminate(BinaryNode <T> node)
        {
            bool NodeRight = node.GetRight() != null ? true : false;
            bool NodeLeft  = node.GetLeft() != null ? true : false;

            //If node does not exist.
            if (node == null)
            {
                return(false);
            }

            if (!NodeRight && !NodeLeft)
            {
                return(Eliminate1(node));
            }

            if (NodeRight && !NodeLeft)
            {
                return(Eliminate2(node));
            }

            if (!NodeRight && NodeLeft)
            {
                return(Eliminate2(node));
            }

            if (NodeRight && NodeLeft)
            {
                return(Eliminate3(node));
            }

            return(false);
        }
예제 #6
0
        private BinaryNode <T> RotateRight(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> left      = binaryNode.GetLeft();
            BinaryNode <T> leftRight = left.GetRight();
            BinaryNode <T> parent    = binaryNode.GetPadre();

            left.SetPadre(parent);
            left.SetRight(binaryNode);
            binaryNode.SetLeft(leftRight);
            binaryNode.SetPadre(left);


            if (leftRight != null)
            {
                leftRight.SetPadre(binaryNode);
            }

            if (binaryNode == this.root)
            {
                this.root = left;
            }
            else if (parent.GetLeft() == binaryNode)
            {
                parent.SetLeft(left);
            }
            else
            {
                parent.SetRight(left);
            }

            left.Balance--;
            binaryNode.Balance = -left.Balance;

            return(left);
        }
예제 #7
0
        public bool Eliminate(BinaryNode <T> binaryNode)
        {
            bool NodeRight = binaryNode.GetRight() != null ? true : false;
            bool NodeLeft  = binaryNode.GetLeft() != null ? true : false;

            //If note is empty.
            if (binaryNode == null)
            {
                return(false);
            }

            if (!NodeRight && !NodeLeft)
            {
                return(Delete1(binaryNode));
            }

            if (NodeRight && !NodeLeft)
            {
                return(Delete2(binaryNode));
            }


            if (!NodeRight && NodeLeft)
            {
                return(Delete2(binaryNode));
            }

            if (NodeRight && NodeLeft)
            {
                return(Delete3(binaryNode));
            }

            return(false);
        }
예제 #8
0
        private BinaryNode <T> RotateLeft(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> right     = binaryNode.GetRight();
            BinaryNode <T> rightLeft = right.GetLeft();
            BinaryNode <T> parent    = binaryNode.GetPadre();

            right.SetPadre(parent);
            right.SetLeft(binaryNode);
            binaryNode.SetRight(rightLeft);
            binaryNode.SetPadre(right);


            if (rightLeft != null)
            {
                rightLeft.SetPadre(binaryNode);
            }

            if (binaryNode == this.root)
            {
                this.root = right;
            }
            else if (parent.GetRight() == binaryNode)
            {
                parent.SetRight(right);
            }
            else
            {
                parent.SetLeft(right);
            }

            right.Balance++;
            binaryNode.Balance = -right.Balance;

            return(right);
        }
예제 #9
0
        private BinaryNode <T> RotateLeftRight(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> left           = binaryNode.GetLeft();
            BinaryNode <T> leftRight      = left.GetRight();
            BinaryNode <T> parent         = binaryNode.GetPadre();
            BinaryNode <T> leftRightRight = leftRight.GetRight();
            BinaryNode <T> leftRightLeft  = leftRight.GetLeft();

            leftRight.SetPadre(parent);
            binaryNode.SetLeft(leftRightRight);
            left.SetRight(leftRightLeft);
            leftRight.SetLeft(left);
            leftRight.SetRight(binaryNode);
            left.SetPadre(leftRight);
            binaryNode.SetPadre(leftRight);

            if (leftRightRight != null)
            {
                leftRightRight.SetPadre(binaryNode);
            }

            if (leftRightLeft != null)
            {
                leftRightLeft.SetPadre(left);
            }

            if (binaryNode == this.root)
            {
                SetRoot(leftRight);
            }
            else if (parent.GetLeft() == binaryNode)
            {
                parent.SetLeft(leftRight);
            }
            else
            {
                parent.SetRight(leftRight);
            }

            if (leftRight.Balance == -1)
            {
                binaryNode.Balance = 0;
                left.Balance       = 1;
            }
            else if (leftRight.Balance == 0)
            {
                binaryNode.Balance = 0;
                left.Balance       = 0;
            }
            else
            {
                binaryNode.Balance = -1;
                left.Balance       = 0;
            }

            leftRight.Balance = 0;

            return(leftRight);
        }
예제 #10
0
        private BinaryNode <T> RotateRightLeft(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> right          = binaryNode.GetRight();
            BinaryNode <T> rightLeft      = right.GetLeft();
            BinaryNode <T> parent         = binaryNode.GetPadre();
            BinaryNode <T> rightLeftLeft  = rightLeft.GetLeft();
            BinaryNode <T> rightLeftRight = rightLeft.GetRight();

            rightLeft.SetPadre(parent);
            binaryNode.SetRight(rightLeftLeft);
            right.SetLeft(rightLeftRight);
            rightLeft.SetRight(right);
            rightLeft.SetLeft(binaryNode);
            right.SetPadre(rightLeft);
            binaryNode.SetPadre(rightLeft);

            if (rightLeftLeft != null)
            {
                rightLeftLeft.SetPadre(binaryNode);
            }

            if (rightLeftRight != null)
            {
                rightLeftRight.SetPadre(right);
            }

            if (binaryNode == this.root)
            {
                SetRoot(rightLeft);
            }
            else if (parent.GetRight() == binaryNode)
            {
                parent.SetRight(rightLeft);
            }
            else
            {
                parent.SetLeft(rightLeft);
            }

            if (rightLeft.Balance == 1)
            {
                binaryNode.Balance = 0;
                right.Balance      = -1;
            }
            else if (rightLeft.Balance == 0)
            {
                binaryNode.Balance = 0;
                right.Balance      = 0;
            }
            else
            {
                binaryNode.Balance = 1;
                right.Balance      = 0;
            }

            rightLeft.Balance = 0;

            return(rightLeft);
        }
예제 #11
0
 private void PostOrder(BinaryNode <T> node, ref List <T> list)
 {
     if (node != null)
     {
         PostOrder(node.GetLeft(), ref list);
         PostOrder(node.GetRight(), ref list);
         list.Add(node.Value);
     }
 }
예제 #12
0
 private void InOrden(BinaryNode <T> node, ref List <T> list)
 {
     if (node != null)
     {
         InOrden(node.GetLeft(), ref list);
         list.Add(node.Value);
         InOrden(node.GetRight(), ref list);
     }
 }
예제 #13
0
 private void PreOrder(BinaryNode <T> binaryNode, ref List <T> list)
 {
     if (binaryNode != null)
     {
         list.Add(binaryNode.Value);
         PreOrder(binaryNode.GetLeft(), ref list);
         PreOrder(binaryNode.GetRight(), ref list);
     }
 }
예제 #14
0
        private void Add(BinaryNode <T> value, BinaryNode <T> root)
        {
            if (root == null)
            {
                //Insert first element.

                this.myRoot(value);
                this.root.SetPadre(null);
                count++;
            }
            else
            {
                if (value.Value.CompareTo(root.Value) == 1)
                {
                    if (root.GetRight() == null)
                    {
                        root.SetRight(value);
                        root.GetRight().SetPadre(root);
                        count++;
                    }
                    else
                    {
                        Add(value, root.GetRight());
                    }
                }
                else
                {
                    if (root.GetLeft() == null)
                    {
                        root.SetLeft(value);
                        root.GetLeft().SetPadre(root);
                        count++;
                    }
                    else
                    {
                        Add(value, root.GetLeft());
                    }
                }
            }
        }
예제 #15
0
        private bool Delete3(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> binaryNodeMoreLeft = RouteLeft(binaryNode.GetRight());

            if (binaryNodeMoreLeft != null)
            {
                binaryNode.Value = binaryNodeMoreLeft.Value;

                Eliminate(binaryNodeMoreLeft);
                return(true);
            }

            return(false);
        }
예제 #16
0
        private bool Eliminate3(BinaryNode <T> node)
        {
            BinaryNode <T> LeftNode = SearchLeft(node.GetRight());

            if (LeftNode != null)
            {
                node.Value = LeftNode.Value;

                Eliminate(LeftNode);
                return(true);
            }

            return(false);
        }
예제 #17
0
        private void InsertBalance(BinaryNode <T> binaryNode, int balance)
        {
            while (binaryNode != null)
            {
                balance = (binaryNode.Balance += balance);

                if (balance == 0)
                {
                    return;
                }
                else if (balance == 2)
                {
                    if (binaryNode.GetLeft().Balance == 1)
                    {
                        RotateRight(binaryNode);
                    }
                    else
                    {
                        RotateLeftRight(binaryNode);
                    }

                    return;
                }
                else if (balance == -2)
                {
                    if (binaryNode.GetRight().Balance == -1)
                    {
                        RotateLeft(binaryNode);
                    }
                    else
                    {
                        RotateRightLeft(binaryNode);
                    }

                    return;
                }

                BinaryNode <T> parent = binaryNode.GetPadre();

                if (parent != null)
                {
                    balance = parent.GetLeft() == binaryNode ? 1 : -1;
                }

                binaryNode = parent;
            }
        }
예제 #18
0
        private void Add(BinaryNode <T> newElement, BinaryNode <T> root)
        {
            if (root == null)
            {
                this.SetRoot(newElement);
                count++;
                this.root.SetPadre(null);
            }
            else
            {
                BinaryNode <T> binaryNode = this.root;

                while (binaryNode != null)
                {
                    int compare = newElement.Value.CompareTo(binaryNode.Value);

                    if (compare < 0)
                    {
                        BinaryNode <T> left = binaryNode.GetLeft();

                        if (left == null)
                        {
                            binaryNode.SetLeft(newElement);
                            binaryNode.GetLeft().SetPadre(binaryNode);
                            count++;
                            InsertBalance(binaryNode, 1);
                            return;
                        }
                        else
                        {
                            binaryNode = left;
                        }
                    }
                    else
                    {
                        if (compare > 0)
                        {
                            BinaryNode <T> right = binaryNode.GetRight();

                            if (right == null)
                            {
                                binaryNode.SetRight(newElement);
                                binaryNode.GetRight().SetPadre(binaryNode);
                                count++;
                                InsertBalance(binaryNode, -1);
                                return;
                            }
                            else
                            {
                                binaryNode = right;
                            }
                        }
                        else
                        {
                            binaryNode.Value = newElement.Value;
                            return;
                        }
                    }
                }
            }
        }
예제 #19
0
        private bool Delete2(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> TempLeft  = binaryNode.GetPadre().GetLeft();
            BinaryNode <T> TempRight = binaryNode.GetPadre().GetRight();

            BinaryNode <T> current = binaryNode.GetLeft() != null?binaryNode.GetLeft() : binaryNode.GetRight();

            if (TempLeft == binaryNode)
            {
                binaryNode.GetPadre().SetLeft(current);

                current.SetPadre(binaryNode.GetPadre());
                binaryNode.SetRight(null);
                binaryNode.SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == binaryNode)
            {
                binaryNode.GetPadre().SetRight(current);

                current.SetPadre(binaryNode.GetPadre());
                binaryNode.SetRight(null);
                binaryNode.SetLeft(null);
                count--;
                return(true);
            }

            return(false);
        }