예제 #1
0
        public Node <int> deleteRecursive(Node <int> root, int key)
        {
            if (root == null)
            {
                return(root);
            }
            //if key less than root key, traverse left subtree
            if (key < root.getData())
            {
                root.setLeft(deleteRecursive(root.getLeft(), key));
            }
            //if key greater than root key, traverse right subtree
            else if (key > root.getData())
            {
                root.setRight(deleteRecursive(root.getRight(), key));
            }
            //found node to delete
            else
            {
                //node to delete has zero or one child node
                if (root.getLeft() == null)
                {
                    if (root.getRight() != null)
                    {
                        root.getRight().setParent(root.getParent());
                    }
                    return(root.getRight());
                }
                else if (root.getRight() == null)
                {
                    if (root.getLeft() != null)
                    {
                        root.getLeft().setParent(root.getParent());
                    }
                    return(root.getLeft());
                }

                //node to delete has two children, so find the smallest node on the right subtree
                //replace the node to delete with that node, then delete the smallest node on the right subtree
                root.setData(getMinValue(root.getRight()));
                root.setRight(deleteRecursive(root.getRight(), root.getData()));
            }

            return(root);
        }
예제 #2
0
        public Node <int> insertRecursive(Node <int> root, Node <int> parent, int key)
        {
            if (root == null)
            {
                return(new Node <int>(parent, key));
            }

            if (key < root.getData())
            {
                root.setLeft(insertRecursive(root.getLeft(), root, key));
            }
            else if (key > root.getData())
            {
                root.setRight(insertRecursive(root.getRight(), root, key));
            }
            return(root);
        }
예제 #3
0
 private void addElement(Node <T> root, T element)
 {
     if (this.root == null)
     {
         this.root = new Node <T>(element, null, null, null);
         numberOfNodes++;
     }
     else
     {
         if (comp.Compare(element, root.getElement()) < 0)//x es menor que y
         {
             if (root.getLeft() == null)
             {
                 root.setLeft(new Node <T>(element, root, null, null));
                 numberOfNodes++;
             }
             else
             {
                 addElement(root.getLeft(), element);
             }
         }
         else if (comp.Compare(element, root.getElement()) > 0)//x es mayor que y
         {
             if (root.getRight() == null)
             {
                 root.setRight(new Node <T>(element, root, null, null));
                 numberOfNodes++;
             }
             else
             {
                 addElement(root.getRight(), element);
             }
         }
         else//x es igual que y
         {
             throw new Exception("NO SE PERMITEN DUPLICADOS");
         }
     }
 }
예제 #4
0
        public T remove(Node <T> root, T element)
        {
            if (root == null)
            {
                return(default(T));
            }
            else if (comp.Compare(element, root.getElement()) == 0)
            {
                if (numberOfChildren(root) == 0)
                {
                    T aux = root.getElement();
                    if (root == this.root)
                    {
                        this.root = null;
                    }
                    else
                    {
                        if (comp.Compare(element, root.getParent().getLeft().getElement()) == 0)
                        {
                            root.getParent().setLeft(null);
                            root = null;
                        }
                        else
                        {
                            root.getParent().setRight(null);
                            root = null;
                        }
                    }
                    numberOfNodes--;
                    return(aux);
                }
                else if (numberOfChildren(root) == 1)
                {
                    T aux = root.getElement();
                    if (root == this.root)
                    {
                        if (root.getLeft() != null)
                        {
                            this.root = root.getLeft();
                        }
                        else
                        {
                            this.root = root.getRight();
                        }
                    }
                    else
                    {
                        if (root.getParent().getLeft() != null)
                        {
                            if (root.getLeft() != null)
                            {
                                root.getParent().setLeft(root.getLeft());
                            }
                            else
                            {
                                root.getParent().setLeft(root.getRight());
                            }
                        }
                        else
                        {
                            if (root.getLeft() != null)
                            {
                                root.getParent().setRight(root.getLeft());
                            }
                            else
                            {
                                root.getParent().setRight(root.getRight());
                            }
                        }
                    }
                    numberOfNodes--;
                    return(aux);
                }
                else//El que sustituirá será el más derecho de los izquierdos
                {
                    Node <T> next = root.getLeft();
                    T        aux  = root.getElement();
                    if (next.getRight() != null)
                    {
                        while (next.getRight() != null)
                        {
                            next = next.getRight();
                        }
                        root.setElement(next.getElement());
                        Node <T> father = next.getParent();
                        father.setRight(null);
                    }
                    else
                    {
                        root.setElement(next.getElement());
                        root.setLeft(null);
                    }

                    numberOfNodes--;
                    return(aux);
                }
            }
            else
            {
                if (comp.Compare(element, root.getElement()) < 0)
                {
                    return(remove(root.getLeft(), element));
                }
                else
                {
                    return(remove(root.getRight(), element));
                }
            }
        }