Java Reference
In-Depth Information
10. Write a method called equals that accepts another binary tree of integers as a parameter and compares the two trees
to see whether they are equal to each other. For example, if variables of type IntTree called t1 and t2 have been
initialized, then t1.equals(t2) will return true if the trees are equal and false otherwise. Two empty trees are
considered to be equal to each other.
11. Write a method called doublePositives that doubles all data values greater than 0 in a binary tree of integers.
12. Write a method called numberNodes that changes the data stored in a binary tree, assigning sequential integers
starting with 1 to each node so that a preorder traversal will produce the numbers in order ( 1, 2, 3, etc). For
example, if a variable t refers to reference tree #1, the call of t.numberNodes() ; would overwrite the existing data,
assigning values from 1 to 6 to the nodes so that a preorder traversal of the tree would produce 1, 2, 3, 4, 5, 6
as shown in the following diagram. Do not change the structure of the tree, only the values stored in the data fields.
Your method should return the number of nodes in the tree.
1
2
4
3
5
6
13. Write a method called removeLeaves that removes the leaves from a tree. A leaf is a node that has empty left and
right subtrees. If your method is called on an empty tree, the method does not change the tree because there are no
nodes of any kind (leaf or not).
14. Write a method called copy that returns a reference to a new IntTree that is an independent copy of the original
tree. Do not change the original tree.
15. Write a method called completeToLevel that accepts an integer n as a parameter and that adds nodes to a tree
to complete the first n levels. A level is complete if every possible node at that level is not null . We will use the
convention that the overall root is at level 1 , its children are at level 2 , and so on. You should preserve any existing
nodes in the tree. Any new nodes added to the tree should contain the value -1 . Your method should throw an
IllegalArgumentException if it is passed a value for a level that is less than 1 .
For example, if a variable called t refers to reference tree #2 and you make the call of t.completeToLevel(4); ,
the tree should change to the following tree:
2
8
1
0
-1
7
6
4
-1
-1
9
Search WWH ::




Custom Search