logo Practice-It logo

BJP3 Exercise 17.2: countEmpty

Language/Type: Java binary trees implementing IntTree
Related Links:
Author: Robert Baxter (on 2013/04/01)

Write a method countEmpty that returns the number of empty branches in a tree. An empty tree is considered to have one empty branch (the tree itself). For non-empty trees, your method(s) should count the total number of empty branches among the nodes of the tree. A leaf node has two empty branches. A node with one non-empty child has one empty branch. A node with two non-empty children (a full branch) has no empty branches. For example the tree below has 15 empty branches (indicated by circles):

                    +---+
                    | 0 |
                    +---+
                   /     \
               +---+     +---+
               | 4 |     | 6 |
               +---o     +---+
              /         /     \
          +---+     +---+     +---+
          | 3 |     | 0 |     | 1 |
          +---o     +---+     o---+
         /         /     \         \
     +---+     +---+     +---+     +---+
     | 4 |     | 3 |     | 8 |     | 5 |
     +---o     o---+     o---o     +---+
    /               \             /     \
+---+               +---+     +---+     +---+
| 1 |               | 9 |     | 2 |     | 7 |
o---o               o---o     o---o     o---o

Assume that you are adding this method to the IntTree class as defined below:

public class IntTree {
    private IntTreeNode overallRoot;
    ...
}
Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.