57 public static void recursive_solve(
int n) {
62 private static void move_disk(
int from,
int to) {
63 System.out.println(
"move disk from "+from+
" to "+to);
67 private static void move_towers(
int n,
int from,
int to,
int free) {
71 move_towers(n-1,from,free,to);
73 move_towers(n-1,free,to,from);
79 public static void recursive_solve_1(
int n) {
80 move_towers_1(n,0,2,1);
84 private static void move_towers_1(
int n,
int from,
int to,
int free) {
86 move_towers_1(n-1,from,free,to);
90 int t=from; from=free; free=t;
97 public static void iterative_solve(
int n) {
98 move_towers_iterative(n,0,2,1);
106 private static class Configuration {
107 Configuration(
int n,
int from,
int to,
int free) {
118 private static void move_towers_iterative(
int n,
int from,
int to,
int free) {
120 Stack<Configuration> stack=
new Stack<Configuration>();
126 stack.push(
new Configuration(n,from,to,free));
128 int t=to; to=free; free=t;
137 int t=from; from=free; free=t;
144 if (stack.is_empty()) {
148 Configuration c=stack.pop();
149 n=c.n; from=c.from; to=c.to; free=c.free;
158 public static class InorderIterator
implements java.util.Iterator<String> {
165 protected class Node extends Configuration {
166 public Node(
int n,
int from,
int to,
int free) {
167 super(n,from,to,free);
170 return "move disk from "+from+
" to "+to;
175 return n>1 ?
new Node(n-1,from,free,to) :
null;
179 return n>1 ?
new Node(n-1,free,to,from) :
null;
185 InorderIterator(
int n) {
186 stack_.
push(
new Node(n,0,2,1));
190 private void descendLeft() {
191 Node node=stack_.
top();
192 for (node=node.getLeft();node!=
null;node=node.getLeft())
196 @Override
public String next() {
197 Node node=stack_.
pop();
198 if (node.getRight()!=
null) {
199 stack_.
push(node.getRight());
203 return node.toString();
206 @Override
public boolean hasNext() {
210 @Override
public void remove() {
211 throw new UnsupportedOperationException();
216 public static class Inorder
implements Iterable<String>{
218 Inorder(
int n) { n_=n; }
219 @Override
public java.util.Iterator<String> iterator() {
220 return new InorderIterator(n_);
227 public static Inorder solution(
int n) {
228 return new Inorder(n);
233 public static void main(String args[]) {
234 int n=Integer.parseInt(args[0]);
235 System.out.println(
"recursive solution:\n");
238 System.out.println(
"\nrecursive solution w/o end-recursion:\n");
239 recursive_solve_1(n);
241 System.out.println(
"\niterative solution:\n");
244 System.out.println(
"\niterative solution using inorder iterator:\n");
245 for (String move : Hanoi.solution(n)) {
246 System.out.println(move);
Implementation of a stack based on aud.Vector.
void push(T x)
Push x onto stack.
boolean is_empty()
Is stack empty?
T pop()
Pop element from stack.
Solution state interpreted as a binary tree.
Node getLeft()
left: from -> free (replaces first recusive call to move_towers)
Node(int n, int from, int to, int free)
Node getRight()
left: free -> to (replaces second recusive call to move_towers)
AuD lecture: Data structures, algorithms, examples.