Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

removeMatchingLeaves

Related Links:
Author: Marty Stepp (on 2011/03/17)

Write a method removeMatchingLeaves that could be added to the IntTree class from lecture and section. The method accepts a second IntTree as its parameter and removes any leaf nodes (nodes without children) that "match" with a corresponding node in the other tree. For this problem, two nodes "match" if they contain the same data value and are located in exactly the same place in the tree relative to the trees' overall roots. For a node to be removed by your method, it must be a leaf in your tree; but it need not be a leaf in the other tree passed as a parameter. If the second tree doesn't contain a node at that corresponding location or the value there is different, it does not match.

For example, suppose two variables of type IntTree named tree1 and tree2 store the following elements:

tree1 tree2
                  +----+
                  |  3 |
                  +----+
               /          \
            /                \
       +----+                +----+
       |  4 |                |  7 |
       +----+                +----+
      /      \              /      \
     /        \            /        \
 +----+     +----+      +----+    +----+
 |  0 |     |  9 |      |  2 |    |  5 |
 +----+     +----+      +----+    +----+
             /  \
            /    \
        +----+  +----+
        |  8 |  |  6 |
        +----+  +----+
                  +----+
                  |  3 |
                  +----+
               /          \
            /                \
       +----+                +----+
       | 28 |                |  7 |
       +----+                +----+
      /      \              /
     /        \            /
 +----+     +----+      +----+
 |  0 |     |  9 |      |  2 |
 +----+     +----+      +----+
     \          \           \
      \          \           \
     +----+    +----+      +----+
     |  8 |    |  6 |      | 31 |
     +----+    +----+      +----+

The call of tree1.removeMatchingLeaves(tree2); would modify tree1 to store the elements below. The leaf nodes containing 0, 2, and 6 have been removed because they match the corresponding nodes in tree2. The other leaves from tree1, which contain the values 8 and 5, are not removed because they aren't matched in tree2.

tree1
                  +----+
                  |  3 |
                  +----+
               /          \
            /                \
       +----+                +----+
       |  4 |                |  7 |
       +----+                +----+
             \                     \
              \                     \
            +----+                +----+
            |  9 |                |  5 |
            +----+                +----+
             /
            /
        +----+
        |  8 |
        +----+

You may define private helper methods, but otherwise you may not call any other methods of the class nor create any data structures. You should not construct any new node objects or modify the tree passed in as the parameter. It may be helpful for you to note that your method can directly access the other tree's overallRoot field with an expression such as tree2.overallRoot (this is allowed in Java because they are objects of the same class).

Your solution must be recursive and utilize the x = change(x) pattern discussed in the Building Java Programs textbook.

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.