Homework Two is due tonight; PA2 comes out tonight
Examlet 4 runs Thursday to Sunday — book your seat on PrairieTest
Starting this week, labs include office hours
Where we’re going
Tree vocabulary
Recursive definitions
Implementation intro
Trees
“… most important nonlinear structure[s] in computer science.” — Donald Knuth, The Art of Computer Programming, Vol 1
A tree: ______________________________
Family trees · organization charts · classification trees (what kind of flower is this? is this mushroom poisonous?) · file directories · non-recursive call graphs
We’ll study more specific trees first:
Tree terminology
One of the vertices is called the root of the tree. Guess which one it is.
Make an English word containing the names of the vertices that have a parent but no sibling.
How many parents does each vertex have?
Which vertex has the fewest children?
Which vertex has the most ancestors? descendants?
What is d’s depth? What is d’s height? What is d’s level?
List all the vertices in b’s left subtree.
List all the leaves in the tree.
Tree terminology (for your reference)
root: the single node with no parent
leaf: a node with no children
child: a node pointed to by me
parent: the node that points to me
sibling: another child of my parent
ancestor: my parent or my parent’s ancestor
descendant: my child or my child’s descendant
subtree: a node and its descendants
depth of node x: number of edges on the path from the root to x
height of node x: number of edges on the longest path from x to a leaf
level k: all the nodes at depth k
Branching: \(d\)-ary trees (binary if \(d = 2\))
A d-ary tree T is either
OR
Full\(d\)-ary tree:
Perfect\(d\)-ary tree:
Complete\(d\)-ary tree:
Binary tree height
height(r) — length of the longest path from root r to a leaf
Given a binary tree T, write a recursive definition of the height of T, height(T):
Number of nodes in a perfect tree of height h, N(h):