Wednesday, April 2, 2014

Binary Tree - Finding Leave Counts, Max Depth, and Sum of All Leave's Depths - Java

For this exercise, you must create a random binary sort tree with 1023 nodes. The items in the tree can be real numbers, and you can create the tree by generating 1023 random real numbers and inserting them into the tree, using the usual insert() method for binary sort trees. Once you have the tree, you should compute and output the average depth of all the leaves in the tree and the maximum depth of all the leaves. To do this, you will need three recursive subroutines: one to count the leaves, one to find the sum of the depths of all the leaves, and one to find the maximum depth. The latter two subroutines should have an int-valued parameter, depth, which tells how deep in the tree you've gone. When you call the routine recursively, the parameter increases by 1. 

First I found an implementation of the BST structure online.

This one was fine: http://www.java-tips.org/java-se-tips/java.lang/binary-search-tree-implementation-in-java.html

Then I first did the counting of leaves.

Well a leaf is a leaf if there are no children, so when there are no children return 1.

So I had to account for that case as well as when only a left child exists and only a right child exists.

In these cases I recursively call the function with the child that exists.

There's also the case when there are two children. In this case for each child I recursively call the function and add them together.

Another case which I forgot about was if the node was null. In this case a 0 is returned.


    public int countLeaves(BinaryNode node)
    {
     if(node == null)
     {
      return 0;
     }

     if(node.left == null && node.right == null)
     {
      return 1;
     }

     if(node.left == null)
     {
      return countLeaves(node.right);
     }

     if(node.right == null)
     {
      return countLeaves(node.left);
     }

     return countLeaves(node.left) + countLeaves(node.right);
    }

For the sum of leave depths, it is similar to count leaves except we have an extra parameter to count the leave's depths. Also when we encounter a leaf we return the ++depth.


    public int sumOfLeavesDepths(BinaryNode node, int depth)
    {
     if(node == null)
     {
      return 0;
     }

     if(node.left == null && node.right == null)
     {
      return ++depth;
     }

     if(node.left == null)
     {
      return sumOfLeavesDepths(node.right, ++depth);
     }

     if(node.right == null)
     {
      return sumOfLeavesDepths(node.left, ++depth);
     }

     return sumOfLeavesDepths(node.left, ++depth) + sumOfLeavesDepths(node.right, ++depth);
    }

Finally for the max depth we have something similar to the sumOfLeavesDepths, but instead of returning the sum of the depths when we have two children, we take the max of their depths.


    public int maxDepth(BinaryNode node, int depth)
    {
     if(node == null)
     {
      return 0;
     }

     if(node.left == null && node.right == null)
     {
      return ++depth;
     }
 
     if(node.left == null)
     {
      return maxDepth(node.right, ++depth);
     }

     if(node.right == null)
     {
      return maxDepth(node.left, ++depth);
     }

     return Math.max(maxDepth(node.left, ++depth), maxDepth(node.right, ++depth));
    }

1 comment:

  1. OMG EEEERRR GOBBLE GOBBBLE AWWWWWW
    <3 <3 <3 <3 <3
    GUSTAVO (and Wanfang, okay, mainly wanfang)

    ReplyDelete