






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Today:
Readings for Today: Data Structures, Chapter 5
Readings for Next Topic: Data Structures, Chapter 6
Problem: Convert
x (^) +
y 3
z
⇒ (- (- (* x (+ y 3))) z)
static String toLisp (Tree T) { if (T == null) return ""; else if (T.degree () == 0) return T.label (); else { String R; R = ""; for (int i = 0; i < T.numChildren (); i += 1) R += " " + toLisp (T.child (i)); return String.format ("(%s%s)", T.label (), R); } }
Problem: Convert
x (^) +
y 3
z
⇒ ((-(x*(y+3)))-z)
To think about: how to get rid of all those paren- theses.
static String toInfix (Tree T) { if (T == null) return ""; if (T.degree () == 0) return T.label (); else { return String.format ("(%s%s%s)", toInfix (T.left ()), T.label (), toInfix (T.right ()) } }
void preorderTraverse (Tree T, Action whatToDo) { if (T != null) { whatToDo.action (T); for (int i = 0; i < T.numChildren (); i += 1) preorderTraverse (T.child (i), whatToDo); } }
interface Action { void action (Tree T); }
class Print implements Action | preorderTraverse (myTree, void action (Tree T) { | new Print ()); System.out.print (T.label ()); | } | } |
h+1− 1 k− 1 , where^ h^ is height.
0
1
3
7 8
4
9 10
2
5
11 12
6
13 14
0
1
2
3