Exercise 7 (Towers of Hanoi)

Towers of Hanoi with 4 rings

You must move all rings from the stick A to the stick C in the same order with the help of stick B, such that:

• You can move only one ring in one step from a stick to another

• You can put a ring only either on an empty stick or on a top of another ring whose diameter is bigger.

• The program prints out the movement of a ring in each step, e.g.: “Move the 3. ring from A to B!”

You should implement a recursive algorithm for the problem.

Exercise 8

Add to the class BinarySearchTree discussed on the class (download its source from here) a method

String search(int n)

which search n in the tree. The method returns the description, how to get to n.

E.g.:

Input: n = 11

Output: “Root: 16 Left 5 Right 10 Right 11”

Test the search() method!

Last modified: Wednesday, 2 April 2014, 3:20 PM