コード例 #1
0
 private void preOrderTraversalRec(BinaryNode subTree)
 {
     if (subTree != null)
     {
         Console.Out.Write(subTree.getData());
         Console.Out.Write(" ");
         preOrderTraversalRec(subTree.getLeft());
         preOrderTraversalRec(subTree.getRight());
     }
 }
コード例 #2
0
 private bool hasValueRec(BinaryNode subTree, int value)
 {
     if (subTree == null)
     {
         return false;
     }
     else if (subTree.getData() == value)
     {
         return true;
     }
     else if (subTree.getData() > value)
     {
         return hasValueRec(subTree.getLeft(), value);
     }
     else
     {
         return hasValueRec(subTree.getRight(), value);
     }
 }
コード例 #3
0
 private int getHeightRec(BinaryNode subTree)
 {
     if (subTree == null)
     {
         return 0;
     }
     else
     {
         return Math.Max(getHeightRec(subTree.getLeft()), getHeightRec(subTree.getRight())) + 1;
     }
 }
コード例 #4
0
        //======Private Methods======
        private void deleteValueRec(BinaryNode prevNode, BinaryNode subTree, int value)
        {
            BinaryNode nextNode = null;
            if (subTree != null)
            {
                if (subTree.getData() == value)
                {
                    deleteNode(prevNode, subTree, value);
                }
                else if (subTree.getData() > value)
                {
                    nextNode = subTree.getLeft();

                    deleteValueRec(subTree, nextNode, value);
                }
                else
                {
                    nextNode = subTree.getRight();

                    deleteValueRec(subTree, nextNode, value);
                }
            }
        }
コード例 #5
0
 private void deleteNode(BinaryNode prev, BinaryNode curr, int value)
 {
     BinaryNode temp = null;
     if (prev == null)
     {
         if (root.getLeft() == null && curr.getRight() == null)
         {
             root = null;
         }
         else if (root.getLeft() != null && curr.getRight() == null)
         {
             root = root.getLeft();
         }
         else if (root.getLeft() == null && curr.getRight() != null)
         {
             root = root.getRight();
         }
         else
         {
             temp = root;
             root = getNDelNextInOrderNode(null, root);
             root.setLeft(temp.getLeft());
             root.setRight(temp.getRight());
         }
     }
     else
     {
         if (curr.getLeft() != null && curr.getRight() == null)
         {
             if (prev.getData() > value)
             {
                 prev.setLeft(curr.getLeft());
             }
             else
             {
                 prev.setRight(curr.getRight());
             }
         }
         else if (curr.getLeft() == null && curr.getRight() != null)
         {
             if (prev.getData() > value)
             {
                 prev.setLeft(curr.getRight());
             }
             else
             {
                 prev.setRight(curr.getRight());
             }
         }
         else
         {
             if (prev.getData() > value)
             {
                 temp = curr;
                 prev.setLeft(getNDelNextInOrderNode(prev, curr));
                 prev.getLeft().setLeft(curr.getLeft());
                 prev.getLeft().setRight(curr.getRight());
             }
             else
             {
                 temp = curr;
                 prev.setRight(getNDelNextInOrderNode(prev, curr));
                 prev.getRight().setLeft(curr.getLeft());
                 prev.getRight().setRight(curr.getRight());
             }
         }
     }
 }
コード例 #6
0
 private void addRec(BinaryNode subTree, BinaryNode toAdd)
 {
     if (subTree.getData() > toAdd.getData())
     {
         if(subTree.getLeft() == null){
             subTree.setLeft(toAdd);
         }else{
             addRec(subTree.getLeft(), toAdd);
         }
     }else{
         if(subTree.getRight() == null){
             subTree.setRight(toAdd);
         }else{
             addRec(subTree.getRight(), toAdd);
         }
     }
 }