Professional Documents
Culture Documents
§1 Preliminaries
TREES
1. Terminology
Pedigree Tree
( binary tree )
Lineal Tree
1/18
§1 Preliminaries
【 Definition 】 A tree is a collection of nodes. The collection
can be empty; otherwise, a tree consists of
(1) a distinguished node r, called the root;
(2) and zero or more nonempty (sub)trees T1, , Tk, each of
whose roots are connected by a directed edge from r.
Note:
Note:
Subtrees
Subtreesmust
mustnot
notconnect
connecttogether.
together. Therefore
Thereforeevery
every
node
nodeininthe
thetree
treeisisthe
theroot
rootof
ofsome
somesubtree.
subtree.
There are N 1 edges
Thereare edgesin
inaatree
treewith
withNNnodes.
nodes.
Normally
Normallythe
theroot
rootisisdrawn
drawnat
atthe
thetop.
top.
2/18
§1 Preliminaries
max degree(node )
degree of a tree ::=node
E F G H I J
tree
For example, degree of this tree = 3. K L M
3/18
§1 Preliminaries
path from n1 to nk ::= a (unique) sequence
A
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1 i < k. B C D
length of path ::= number of edges on E F G H I J
the path.
ancestors of a node ::= all the nodes along the path from the
node up to the root.
4/18
§1 Preliminaries
2. Implementation
List Representation
A (A)
B C D ( A ( B, C, D ) )
E F G H I J ( A ( B ( E, FSo the
), C ( Gsize
), Dof each
( H, I, J )node
))
depends on the number of
K L M ( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.
K
E
B
L
F
A C G
H M
D I
J
5/18
§1 Preliminaries
FirstChild-NextSibling Representation
Element A
N
FirstChild NextSibling
B C D
A N
B C D
E F G H I J
E F G H I J N N N N N N N
K L M K L M
N N N N N
6/18
§2 Binary Trees
【 Definition 】 A binary tree is a tree in which no node
can have more than two children.
Rotate the FirstChild-NextSibling tree clockwise by 45.
A A
N
45
B
N
E
B C D
N K C
F
N
E F G H I J L G
N
D
N
NN NN N NN
N
N
N
H
N
K L M
N NN NN M I
N
N
J
N
Element Left
N
N
Left Right
Right
7/18
§2 Binary Trees
Expression Trees (syntax trees) +
〖 Example 〗 Given an infix A
expression: D
+BCD
A Expression
Constructing an Tree B C
(from postfix expression)
〖 Example 〗 (a+b)*(c*(d+e))ab+cde+**
=
+
a b *c d * +
e
T2 T2 a + b c T1 d * + e T1 T1
a bT2 T2 c d + e T1
d e
8/18
§2 Binary Trees
Tree Traversals —— visit each node exactly once
Preorder Traversal Postorder Traversal
void preorder ( tree_ptr tree ) void postorder ( tree_ptr tree )
{ if ( tree ) { { if ( tree ) {
visit ( tree ); for (each child C of tree )
for (each child C of tree ) postorder ( C );
preorder ( C ); visit ( tree );
} }
} }
Levelorder Traversal
void levelorder ( tree_ptr tree ) 1
1 2 3 4
{ enqueue ( tree );
2 3
while (queue is not empty) { 5 6 7
visit ( T = dequeue ( ) ); 4 5 6 7
for (each child C of T )
enqueue ( C );
} 12 3 4 5 6 7
}
9/18
§2 Binary Trees
Inorder Traversal Iterative Program
void inorder ( tree_ptr tree ) void iter_inorder ( tree_ptr tree )
{ if ( tree ) { { Stack S = CreateStack( MAX_SIZE );
inorder ( tree->Left ); for ( ; ; ) {
visit ( tree->Element ); for ( ; tree; tree = tree->Left )
inorder ( tree->Right ); Push ( tree, S ) ;
} tree = Top ( S ); Pop( S );
} if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
〖 Example 〗 Given an }
infix expression:
A+BCD
+
Then inorder traversal A + B C D
A
postorder traversal A B C D +
D
preorder traversal + A B C D
B C
10/18
§2 Binary Trees
〖 Example 〗 Directory listing in a hierarchical file
system.
/usr
Unix directory
Listing format: files that are of depth di will have their names
indented by di tabs.
11/18
/usr §2 Binary Trees
mark
book static void ListDir ( DirOrFile D, int Depth )
Ch1.c {
Ch2.c
Ch3.c
if ( D is a legitimate entry ) {
course PrintName (D, Depth );
cop3530 if ( D is a directory )
fall96 for (each child C of D )
syl.r
spr97 ListDir ( C, Depth + 1 );
syl.r }
sum97 }
hw.c
syl.r T ( N ) = O( N )
alex
hw.c
bill
work
Note: Depth is an internal variable and
course must not be seen by the user of this
cop3212 routine. One solution is to define
fall96
grades another interface function as the
p1.r following:
p2.r
fall97 void ListDirectory ( DirOrFile D )
p2.r
p1.r
{ ListDir( D, 0 ); }
grades
12/18
§2 Binary Trees
〖 Example 〗 Calculating the size of a directory.
/usr 1
13/18
§2 Binary Trees
14/18
§2 Binary Trees
+ F F
A
D F + F
B C
T A T F F
F F T D T
T B T T C T
16/18
Bonus Problem 1
Path of Equal Weight
(2 points)
Due: Tuesday, January 7th, 2020 at 23:59pm