Exemplo n.º 1
0
        private void deleteNode(int data, BinaryNode currentRoot)
        {
            BinaryNode tempNode = search(data);

            if (tempNode.isLeaf())
            {
                if (position)
                {
                    parent.setLeftChild(null);
                }
                else
                {
                    parent.setRightChild(null);
                }
            }
            else if (tempNode.hasOneChild())
            {
                if (tempNode.getchildPosition())
                {
                    parent.setLeftChild(tempNode.getLeftChild());
                }
                else
                {
                    parent.setRightChild(tempNode.getRightChild());
                }
            }
            else
            {
                BinaryNode min = getMinor(tempNode.getRightChild());
                deleteNode(min.getData());
                tempNode.setData(min.getData());
            }
        }
Exemplo n.º 2
0
        private void insertion(int data, BinaryNode root, int whichChild)
        {
            if (whichChild == 0)
            {
                if (root.getLeftChild() == null)
                {
                    root.setLeftChild(new BinaryNode(data));
                }
                else
                {
                    add(data, root.getLeftChild());
                }
            }

            else if (whichChild == 1)
            {
                if (root.getRightChild() == null)
                {
                    root.setRightChild(new BinaryNode(data));
                }
                else
                {
                    add(data, root.getRightChild());
                }
            }
        }
Exemplo n.º 3
0
        /***********************************************/

        /*Finds the position at which the vertex shoud be inserted and inserts it*/
        private void insertion(int data, BinaryNode root, int whichChild)
        {
            int line = 0;

            if (whichChild == 0)
            {
                if (root.getLeftChild() == null)
                {
                    root.setLeftChild(new BinaryNode(data));

                    if (root != mainRoot)
                    {
                        root.getLeftChild().setPositionX(root.getPositionX() - 100);
                        root.getLeftChild().setPositionY(root.getPositionY() + 100);
                        line = 1;
                    }
                    else
                    {
                        root.getLeftChild().setPositionX(root.getPositionX() - 330);
                        root.getLeftChild().setPositionY(root.getPositionY() + 100);
                        line = 3;
                    }
                    graphNode(root.getLeftChild().getPositionX(), root.getLeftChild().getPositionY(), data, line);
                }
                else
                {
                    add(data, root.getLeftChild());
                }
            }

            else if (whichChild == 1)
            {
                if (root.getRightChild() == null)
                {
                    root.setRightChild(new BinaryNode(data));

                    if (root != mainRoot)
                    {
                        root.getRightChild().setPositionX(root.getPositionX() + 100);
                        root.getRightChild().setPositionY(root.getPositionY() + 100);
                        line = 2;
                    }
                    else
                    {
                        root.getRightChild().setPositionX(root.getPositionX() + 330);
                        root.getRightChild().setPositionY(root.getPositionY() + 100);
                        line = 4;
                    }

                    graphNode(root.getRightChild().getPositionX(), root.getRightChild().getPositionY(), data, line);
                }
                else
                {
                    add(data, root.getRightChild());
                }
            }
        }