Professional Documents
Culture Documents
trees
binary trees
traversals of trees
template method pattern
data structures for trees
Trees
Trees
a tree represents a hierarchy
- organization structure of a corporation
Electronics RUs
R&D
Sales
Domestic
Canada
Purchasing
Manufacturing
International
S. America
Africa
TV
CD
Tuner
Overseas
Europe
Asia
Australia
overview
exams
Trees
grading
environment
homeworks
programs
programming
support code
Another Example
Unix or DOS/Windows file system
/user/rt/courses/
cs016/
grades
homeworks/
hw1 hw2
hw3
cs252/
programs/
pr1
pr2
projects/
pr3
papers/
buylow
Trees
sellhigh
grades
demos/
market
Terminology
A is the root node.
B is the parent of D and E.
C is the sibling of B
D and E are the children of B.
D, E, F, G, I are external nodes, or leaves.
A, B, C, H are internal nodes.
The depth (level) of E is 2
The height of the tree is 3.
The degree of node B is 2.
A
B
H
I
Binary Trees
Ordered tree: the children of each node are ordered.
Binary tree: ordered tree with all internal nodes of
degree 2.
Recursive definition of binary tree:
A binary tree is either
- an external node (leaf), or
- an internal node (the root) and two binary trees
(left subtree and right subtree)
Trees
+
+
1
4
river
Trees
Trees
query methods
- isRoot(p), isInternal(p), isExternal(p)
accessor methods
- root(), parent(p), children(p), siblings(p)
update methods (application specific)
Container
PositionalContainer
PositionalSequence
InspectableTree
Sequence
Trees
update methods
- expandExternal(p), removeAboveExternal(p)
PositionalContainer
Trees
PositionalSequence
InspectableTree
Sequence
InspectableBinaryTree
Traversing Trees
preorder traversal
Algorithm preOrder(v)
visit node v
for each child w of v do
recursively perform preOrder(w)
Paper
Title
Abstract
1.1
Trees
1.2
2.1
2.2
2.3
3.1
References
3.2
10
Traversing Trees
postorder traversal
Algorithm postOrder(v)
for each child w of v do
recursively perform postOrder(w)
visit node v
4874K
cs252/
1K
10K
grades
8K
229K
homeworks/
1K
4870K
programs/
1K
projects/
1K
82K
hw1 hw2
3K
Trees
2K
hw3
pr1
pr2
pr3
4K
57K
97K
74K
grades
3K
4787K
papers/
1K
demos/
1K
buylow
sellhigh
market
26K
55K
4786K
11
Evaluating Arithmetic
Expressions
specialization of a postorder traversal
Algorithm evaluateExpression(v)
if v is an external node
return the variable stored at v
else
+
+
9
3
10
17
20
11
2
16
Trees
21
5
7
6
12
3
13
26
7
27
4
12
Traversing Trees
inorder traversal of a binary tree
Algorithm inOrder(v)
recursively perform inOrder(leftChild(v))
visit node v
recursively perform inOrder(rightChild(v))
+
+
1
4
Trees
13
+
+
+
3
Trees
14
Trees
15
16
root
5
size
Baltimore
Trees
Chicago
New York
Providence
Seattle
17
C
F
D
G
Trees
18