






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note from cs61b course at university of california, berkeley. It covers the topic of trees, their recursive structure, fundamental operation of traversal, and different types of traversals such as preorder, inorder, and postorder. The document also discusses the conversion of tree expressions to prefix, infix, and postfix notations, and the visitor pattern for tree traversals. The lecture note also touches upon the time complexity of tree traversals and iterative deepening.
Typology: Slides
1 / 12
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