SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Spring 2004
Prof. Doug Baldwin
insertValue turns empty tree into non-empty
Tree t1 = new Tree()
t1.addItem(...)
....
Tree t2 = t1.left()
t1.insertValue( x )
One example of why stacks are so important
Recursion, e.g.,
printParens( int n )
if n > 0
print "("
printParens( n-1 )
print ")"
Trace some invocations in "printParens(3)"
LIFO is exactly how methods call and return
Read sections 13.1, 13.2, 13.4
("Unary tree" = list)
Examples of trees in real life
Parts of trees
Properties
Trees in CS
if answer1 = "yes"
ask question 2
if answer2 = "yes"
ask question 3
....
else
ask question 3a
if answer 3a = yes
....
askQuestions()
print this.question
read answer
if answer = yes
this.left().askQuestions()
else
this.right().askQuestions()
If-then-else as trees?
main() {
x = 1
for ( i= 1; i < 10 ; i++ ) {
z = 17
if i < 5
y = 3
x = x +1
else
print "hello"
q = 9
}
}
Trees as data structures
Trees are recursively defined
Ordered tree class
Mini-Assignment:
Count the nodes in a tree
// In a subclass of OrderedTree
int countNodes()
...