コード例 #1
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());
                }
            }
        }
コード例 #2
0
 //***********************************************************************************
 public BinaryNode getMinor(BinaryNode currentRoot)
 {
     if (currentRoot.getLeftChild() == null)
     {
         return(currentRoot);
     }
     else
     {
         return(getMinor(currentRoot.getLeftChild()));
     }
 }
コード例 #3
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());
            }
        }
コード例 #4
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());
                }
            }
        }
コード例 #5
0
 private void inOrder(BinaryNode currentRoot)
 {
     if (currentRoot != null)
     {
         inOrder(currentRoot.getLeftChild());
         Console.WriteLine(currentRoot.getData() + " ");
         inOrder(currentRoot.getRightChild());
     }
 }
コード例 #6
0
        private void countLeafs(BinaryNode currentRoot)
        {
            if (currentRoot != null)
            {
                countLeafs(currentRoot.getLeftChild());
                countLeafs(currentRoot.getRightChild());

                if (currentRoot.isLeaf())
                {
                    leafs++;
                }
            }
        }
コード例 #7
0
 private BinaryNode search(int data, BinaryNode currentRoot)
 {
     if (currentRoot == null)
     {
         return(null);
     }
     if (data == currentRoot.getData())
     {
         return(currentRoot);
     }
     parent = currentRoot;
     if (data < currentRoot.getData())
     {
         //if false the position is left
         position = true;
         return(search(data, currentRoot.getLeftChild()));
     }
     else
     {
         //if true the the position is right
         position = false;
         return(search(data, currentRoot.getRightChild()));
     }
 }