AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
Hanoi.java
Go to the documentation of this file.
1package aud.example;
2
3import aud.Stack;
4
54class Hanoi {
55
57 public static void recursive_solve(int n) {
58 move_towers(n,0,2,1);
59 }
60
62 private static void move_disk(int from,int to) {
63 System.out.println("move disk from "+from+" to "+to);
64 }
66 //@<hanoi:movetowers
67 private static void move_towers(int n,int from,int to,int free) {
68 if (n==1)
69 move_disk(from,to);
70 else {
71 move_towers(n-1,from,free,to);
72 move_disk(from,to);
73 move_towers(n-1,free,to,from);
74 }
75 }
76 //@>hanoi:movetowers
77
79 public static void recursive_solve_1(int n) {
80 move_towers_1(n,0,2,1);
81 }
82
84 private static void move_towers_1(int n,int from,int to,int free) {
85 while (n>1) {
86 move_towers_1(n-1,from,free,to);
87 move_disk(from,to);
88
89 --n;
90 int t=from; from=free; free=t;
91 }
92
93 move_disk(from,to); // n=1
94 }
95
97 public static void iterative_solve(int n) {
98 move_towers_iterative(n,0,2,1);
99 }
100
106 private static class Configuration {
107 Configuration(int n,int from,int to,int free) {
108 this.n=n;
109 this.from=from;
110 this.to=to;
111 this.free=free;
112 }
113 int n;
114 int from, to, free;
115 }
116
118 private static void move_towers_iterative(int n,int from,int to,int free) {
119
120 Stack<Configuration> stack=new Stack<Configuration>();
121
122 boolean done=false;
123
124 DESCEND: do {
125 while (n>1) {
126 stack.push(new Configuration(n,from,to,free)); // save
127 --n;
128 int t=to; to=free; free=t;
129 }
130
131 do {
132 while (n>1) {
133
134 move_disk(from,to);
135
136 --n;
137 int t=from; from=free; free=t;
138
139 continue DESCEND; // essentially a GOTO (ugly!)
140 // alternatively copy 1st while loop (we have n==1!)
141 }
142 move_disk(from,to); // n=1
143
144 if (stack.is_empty()) {
145 done=true;
146 }
147 else {
148 Configuration c=stack.pop();
149 n=c.n; from=c.from; to=c.to; free=c.free; // restore
150 }
151 } while (!done);
152 } while (!done);
153 }
154
155
156
158 public static class InorderIterator implements java.util.Iterator<String> {
159
165 protected class Node extends Configuration {
166 public Node(int n,int from,int to,int free) {
167 super(n,from,to,free);
168 }
169 @Override public String toString() {
170 return "move disk from "+from+" to "+to;
171 }
172
174 public Node getLeft() {
175 return n>1 ? new Node(n-1,from,free,to) : null;
176 }
178 public Node getRight() {
179 return n>1 ? new Node(n-1,free,to,from) : null;
180 }
181 }
182
183 Stack<Node> stack_ = new Stack<Node>();
184
185 InorderIterator(int n) {
186 stack_.push(new Node(n,0,2,1));
187 descendLeft();
188 }
189
190 private void descendLeft() {
191 Node node=stack_.top();
192 for (node=node.getLeft();node!=null;node=node.getLeft())
193 stack_.push(node);
194 }
195
196 @Override public String next() {
197 Node node=stack_.pop();
198 if (node.getRight()!=null) {
199 stack_.push(node.getRight());
200 descendLeft();
201 }
202
203 return node.toString();
204 }
205
206 @Override public boolean hasNext() {
207 return !stack_.is_empty();
208 }
210 @Override public void remove() {
211 throw new UnsupportedOperationException();
212 }
213 }
214
216 public static class Inorder implements Iterable<String>{
217 int n_;
218 Inorder(int n) { n_=n; }
219 @Override public java.util.Iterator<String> iterator() {
220 return new InorderIterator(n_);
221 }
222 }
223
227 public static Inorder solution(int n) {
228 return new Inorder(n);
229 }
230
231
233 public static void main(String args[]) {
234 int n=Integer.parseInt(args[0]);
235 System.out.println("recursive solution:\n");
236 recursive_solve(n);
237
238 System.out.println("\nrecursive solution w/o end-recursion:\n");
239 recursive_solve_1(n);
240
241 System.out.println("\niterative solution:\n");
242 iterative_solve(n);
243
244 System.out.println("\niterative solution using inorder iterator:\n");
245 for (String move : Hanoi.solution(n)) {
246 System.out.println(move);
247 }
248 }
249}
Implementation of a stack based on aud.Vector.
Definition: Stack.java:8
void push(T x)
Push x onto stack.
Definition: Stack.java:31
boolean is_empty()
Is stack empty?
Definition: Stack.java:14
T pop()
Pop element from stack.
Definition: Stack.java:23
T top()
Get stack top.
Definition: Stack.java:17
Solution state interpreted as a binary tree.
Definition: Hanoi.java:165
Node getLeft()
left: from -> free (replaces first recusive call to move_towers)
Definition: Hanoi.java:174
Node(int n, int from, int to, int free)
Definition: Hanoi.java:166
Node getRight()
left: free -> to (replaces second recusive call to move_towers)
Definition: Hanoi.java:178
AuD lecture: Data structures, algorithms, examples.