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

swapChildrenAtLevel

Language/Type: Java binary trees implementing IntTree
Related Links:
Author: Marty Stepp (on 2012/03/21)

Write a method swapChildrenAtLevel to be added to the IntTree class. Your method should accept an integer n as a parameter and swap the left and right children of all nodes at level n. In other words, after your method is run, any node at level n + 1 that used to be its parent's left child should now be its parent's right child and vice versa. For this problem, the overall root of a tree is defined to be at level 1, its children are at level 2, etc.

The table below shows the result of calling this method on an IntTree variable named tree.

IntTree tree = new IntTree();
...
tree.swapChildrenAtLevel(2);
Level Before Call After Call

1




2




3




4
                  +----+
                  | 42 |
                  +----+
               /          \
            /                \
       +----+                +----+
       | 19 |                | 65 |
       +----+                +----+
      /      \              /
     /        \            /
 +----+     +----+      +----+
 | 54 |     | 32 |      | 23 |
 +----+     +----+      +----+
             /
            /
         +----+
         | 12 |
         +----+
                    +----+
                    | 42 |
                    +----+
                 /          \
              /                \
         +----+                +----+
         | 19 |                | 65 |
         +----+                +----+
        /      \                     \     
       /        \                     \    
   +----+     +----+                +----+
   | 32 |     | 54 |                | 23 |
   +----+     +----+                +----+
    /
   /
+----+
| 12 |
+----+

If n is 0 or less than 0, your method should throw an IllegalArgumentException. If the tree is empty or does not have any nodes at the given level or deeper, it should not be affected by a call to your method.

For efficiency, your method should not traverse any parts of the tree that it does not need to traverse. Specifically, you should not access any nodes lower than level n + 1, because there is nothing there that would be changed.

You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the tree class nor create any data structures. Do not construct any new node objects or change the data of any nodes. Your solution must be recursive.

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.