Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. J. O. Blech Dipl.-Inform. M. J. Gawkowski Dipl.-Inform. N. Rauch
TU Kaiserslautern
Fachbereich Informatik AG Softwaretechnik
Lösungvorschlag zum Übungsblatt 10:
Software-Entwicklung I (WS 2007/08)
Aufgabe 1 Geflechte
1 class DataSet {
2 public int key;
3 public String value;
4 5
6 public DataSet(int k, String v){
7 key = k;
8 value = v;
9 }
10 public String toString(){
11 return ("("+key+","+value+")");
12 }
13 }
14
15 class DataSetList {
16 public DataSet elem;
17 public DataSetList next;
18
19 public DataSetList(DataSet ds){
20 elem = ds;
21 next = null;
22 }
23 public DataSetList(DataSet ds, DataSetList l){
24 elem = ds;
25 next = l;
26 }
27 public DataSetList Cons(DataSet ds){
28 return new DataSetList(ds,this);
29 }
30 public DataSet hd(){ return elem;}
31 public DataSetList tl(){ return next;}
32
33 public String toString(){
34 String res = "[";
35 DataSetList tmp = this;
36 while (tmp.next != null) {
37 res = res + tmp.elem.toString() + ",";
38 tmp = tmp.next;
39 }
40 res = res + tmp.elem.toString() + "]";
41 return res;
42 }
43 }
44 45 46
47 class BinTree {
48 int elem;
49 BinTree left, right;
50
51 public BinTree(int k){
52 elem = k;
53 left=null;
54 right=null;
55 }
56
57 public BinTree(int k, BinTree t1, BinTree t2){
58 elem = k;
59 left=t1;
60 right=t2;
61 }
62
63 public boolean has_no_sons(){
64 return left==null && right==null;
65 }
66 public boolean has_both_sons(){
67 return left!=null && right!=null;
68 }
69 public boolean has_only_left_son(){
70 return left!=null && right==null;
71 }
72 public boolean has_only_right_son(){
73 return left==null && right!=null;
74 }
75 public boolean has_left(){
76 return left!=null;
77 }
78 public boolean has_right(){
79 return right!=null;
80 }
81 }
82
83 class NodeStack {
84 BinTree elem;
85 NodeStack next;
86
87 public NodeStack(){
88 elem = null;
89 next=null;
90 }
91 public NodeStack(BinTree ref){
92 elem = ref;
93 next=null;
94 }
95 public NodeStack(BinTree ref,NodeStack stack){
96 elem = ref;
97 next=stack;
98 }
99
100 public boolean isempty(){
101 return elem==null && next==null;
102 }
103 public NodeStack push(BinTree ref){
104 return new NodeStack(ref,this);
105 }
106
107 public BinTree top(){
108 return elem;
109 }
110 public NodeStack pop(){
111 return next;
112 }
113 }
114 115
116 public class Main { // public class Main
117 118
119 public static void main(String [] args) {
120
121 DataSet e0 = new DataSet(0,"0");
122 DataSet e1 = new DataSet(1,"1");
123 DataSet e2 = new DataSet(2,"2");
124 DataSet e3 = new DataSet(3,"3");
125
126 DataSetList l0 = new DataSetList(e0);
127 128
129 System.out.println("test1: ");
130 System.out.println(e1);
131 System.out.println(l0);
132
133 System.out.println("test2:");
134 DataSetList l3 = (new DataSetList(e0)).Cons(e1).Cons(e2).Cons(e3).Cons(e2);
135 System.out.println("l3="+l3);
136 137
138 System.out.println("l3 ="+l3);
139 System.out.println("l8 ="+reverse1(l3));
140 System.out.println("l9 ="+reverse1(l3));
141 System.out.println("l10 ="+appendlast2(l3,e2));
142 System.out.println("l11 ="+reverse2(l3));
143
144 System.out.println("---test3:---");
145 BinTree t1 = new BinTree(3);
146 BinTree t2 = new BinTree(4);
147 BinTree t3 = new BinTree(5,t1,t2);
148 System.out.println("sum1(t3)="+sum1(t3));
149 System.out.println("sum2(t3)="+sum2(t3));
150
151 System.out.println("---test4:---");
152 BinTree t4 = new BinTree(1);
153 BinTree t5 = new BinTree(2,null,t4);
154 BinTree t6 = new BinTree(3);
155 BinTree t7 = new BinTree(4,t5,t6);
156 System.out.println("sum1(t7)="+sum1(t7));
157 System.out.println("sum2(t7)="+sum2(t7));
158 159
160 }
161
162 // Uebungsblatt 10, Teilaufgabe a
163 public static DataSetList appendlast1(DataSetList l, DataSet e){
164 if(l==null){
165 return new DataSetList(e);
166 } else {
167 return appendlast1(l.tl(),e).Cons(l.hd());
168 }
169 }
170 public static DataSetList reverse1(DataSetList l){
171 if(l==null) return l;
172 if(l.next==null){
173 return l;
174 } else {
175 return appendlast1(reverse1(l.tl()),l.hd());
176 }
177 }
178
179 // Uebungsblatt 10, Teilaufgabe b
180 public static DataSetList appendlast2(DataSetList l, DataSet e){
181 DataSetList tmp = l;
182 if(l==null) return new DataSetList(e);
183 while(tmp.next != null) tmp=tmp.next;
184 tmp.next=new DataSetList(e);
185 return l;
186 }
187 public static DataSetList reverse2(DataSetList l){
188 int i = 0;
189 DataSetList tmp = l;
190 while(tmp != null) {
191 tmp=tmp.next;
192 i++;
193 }
194 if(i==0){
195 return tmp;
196 } else {
197 tmp=new DataSetList(new DataSet(l.elem.key,l.elem.value));
198 i--;
199 while(i!=0){
200 tmp=tmp.Cons(new DataSet(l.elem.key,l.elem.value));
201 l=l.next;
202 i--;
203 }
204 }
205 return tmp;
206 }
207 208
209 // Uebungsblatt 10, Teilaufgabe c
210 public static int sum1(BinTree t){
211 if(t==null) return 0;
212 if(t.has_no_sons()) return t.elem;
213 if(t.has_only_left_son()) return t.elem + sum1(t.left);
214 if(t.has_only_right_son()) return t.elem + sum1(t.right);
215 return t.elem + sum1(t.left) + sum1(t.right);
216 }
217
218 // Uebungsblatt 10, Teilaufgabe d
219 public static int sum2(BinTree t){
220 if(t==null) return 0;
221
222 int res = 0;
223 NodeStack stack = new NodeStack();
224 BinTree top_elem;
225
226 stack = stack.push(t);
227 while(!stack.isempty()){
228 top_elem=stack.top();
229 if(top_elem.has_no_sons()){
230 System.out.println("top element has no sons");
231 res=+top_elem.elem;
232 stack=stack.pop();
233 }
234 if(top_elem.has_only_left_son()){
235 System.out.println("top element has left son");
236 res=+top_elem.elem;
237 stack=stack.pop().push(top_elem.left);
238 }
239 if(top_elem.has_only_right_son()){
240 System.out.println("top element has right son");
241 res=+top_elem.elem;
242 stack=stack.pop().push(top_elem.right);
243 }
244 if(top_elem.has_both_sons()){
245 System.out.println("top element has both sons");
246 res=+top_elem.elem;
247 stack=stack.pop().push(top_elem.right).push(top_elem.left);
248 // continue;
249 }
250 }
251 return res;
252 }
253 } // public class Main
254