CHAPTER 4 TREES §1 Preliminaries 1. Terminology Pedigree Tree ( binary tree ) Lineal Tree 1/17 §1 Preliminaries 【Definition】A tree is a collection of nodes.

Download Report

Transcript CHAPTER 4 TREES §1 Preliminaries 1. Terminology Pedigree Tree ( binary tree ) Lineal Tree 1/17 §1 Preliminaries 【Definition】A tree is a collection of nodes.

Slide 1

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 2

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 3

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 4

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 5

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 6

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 7

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 8

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 9

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 10

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 11

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 12

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 13

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 14

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 15

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 16

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T


Slide 17

CHAPTER 4

TREES

§1 Preliminaries
1. Terminology

Pedigree Tree
( binary tree )
Lineal Tree

1/17

§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:
 Subtrees must not connect together. Therefore every
node in the tree is the root of some subtree.
 There are N  1 edges in a tree with N nodes.
 Normally the root is drawn at the top.

2/17

§1 Preliminaries

 degree of a node ::= number of
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0.

max  de gree(node) 
 degree of a tree ::=node
tree
For example, degree of this tree = 3.

A

B
E
K

L

C
F

G

D
H
M

 parent ::= a node that has subtrees.

 children ::= the roots of the subtrees of a parent.
 siblings ::= children of the same parent.
 leaf ( terminal node ) ::= a node with degree 0 (no children).

3/17

I

J

§1 Preliminaries

 path from n1 to nk ::= a (unique) sequence
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1  i < k.
 length of path ::= number of edges on
the path.
 depth of ni ::= length of the unique path K
from the root to ni. Depth(root) = 0.

A

B
E

C
F

G

L

D
H

I

M

 height of ni ::= length of the longest path from ni to a leaf.
Height(leaf) = 0, and height(D) = 2.
 height (depth) of a tree ::= height(root) = depth(deepest leaf).
 ancestors of a node ::= all the nodes along the path from the
node up to the root.
 descendants of a node ::= all the nodes in its subtrees.

4/17

J

§1 Preliminaries

2. Implementation
 List Representation
(A)

A
B
E
K

C
F

( A ( B, C, D ) )

D

G

H

L

I

( A ( B ( E, FSo
), Cthe
( Gsize
), D of
( H,each
I, J )node
))
depends on the number of
( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.

J

M

B

E
F

A

C

I
J

5/17

L

G

H
D

K

M

§1 Preliminaries

 FirstChild-NextSibling Representation
A

Element
FirstChild NextSibling

N

B

A
B

C

N

C

D

E
E
K

L

F

G

D

H
M

I

J

K
N

F

G

N N

N N

H

I
N

L

M

N N

N N

Note: The representation is not unique since the
children in a tree can be of any order.

6/17

J
N N

§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

45

N

B

C

D
N

E
K
N

F

G

NN

NN

H

N

L

M

NN

NN

Element
Left
Right

7/17

I

J
NN

Left
Right

 Expression Trees (syntax trees)

§2 Binary Trees

+

〖Example〗 Given an infix expression:
A+ BC D

A

 Constructing an Expression Tree
(from postfix expression)

B





D
C

〖Example〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *

+
a
T2

T2 a
a

8/17

b

+ b

*c

d * +
e
cT1d *

bT T c
2 2

+ e T1 T1

d

+ e

d

e

T1

§2 Binary Trees

 Tree Traversals —— visit each node exactly once
 Preorder Traversal
 Postorder Traversal
void preorder ( tree_ptr tree )
{ if ( tree ) {
visit ( tree );
for (each child C of tree )
preorder ( C );
}
}

void postorder ( tree_ptr tree )
{ if ( tree ) {
for (each child C of tree )
postorder ( C );
visit ( tree );
}
}

Home work:
p.138 4.35
Implement
 Levelorder Traversal
Level-Order Traversal
1
void levelorder ( tree_ptr tree )
{ enqueue ( tree );
while (queue is not empty) {
visit ( T = dequeue ( ) );
for (each child C of T )
enqueue ( C );
}
}
9/17

1 2 3 4

2
4

3
5

6

7

5 6 7

12 3 4 5 6 7

§2 Binary Trees

 Inorder Traversal
void inorder ( tree_ptr tree )
{ if ( tree ) {
inorder ( tree->Left );
visit ( tree->Element );
inorder ( tree->Right );
}
}

〖Example〗 Given an
infix expression:
A+B CD
+


A


B
10/17

D
C

Iterative Program
void iter_inorder ( tree_ptr tree )
{ Stack S = CreateStack( MAX_SIZE );
for ( ; ; ) {
for ( ; tree; tree = tree->Left )
Push ( tree, S ) ;
tree = Top ( S ); Pop( S );
if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
}

Then inorder traversal  A + B  C  D
postorder traversal  A B C  D  +
preorder traversal  + A   B C D

§2 Binary Trees

〖Example〗 Directory listing in a hierarchical file system.
/usr
mark
book

course

ch1.c ch2.c ch3.c

alex
hw.c

hw.c

work

cop3530

syl.r

syl.r

course
cop3212

fall96 spr97 sum97
syl.r

bill

fall96
grades

p1.r p2.r

fall97
p2.r p1.r

grades

Unix directory

Listing format: files that are of depth di will have their names
indented by di tabs.

11/17

§2 Binary Trees

/usr
mark
book
Ch1.c
Ch2.c
Ch3.c
course
cop3530
fall96

syl.r
spr97
syl.r
sum97
syl.r

static void ListDir ( DirOrFile D, int Depth )
{
if ( D is a legitimate entry ) {
PrintName (D, Depth );
if ( D is a directory )
for (each child C of D )
ListDir ( C, Depth + 1 );
}
}

T ( N ) = O( N )

hw.c
alex
hw.c
bill
work
course
cop3212
fall96
grades
p1.r
p2.r
fall97
p2.r
p1.r
grades

12/17

Note: Depth is an internal variable and
must not be seen by the user of this
routine. One solution is to define
another interface function as the
following:
void ListDirectory ( DirOrFile D )
{

ListDir( D, 0 );

}

〖Example〗 Calculating the size of a directory.

§2 Binary Trees

/usr 1

book 1

mark 1

alex 1

course 1 hw.c 6

hw.c 8 work 1

ch1.c 3 ch2.c 2 ch3.c 4

bill 1

cop3530 1

course 1
cop3212 1

fall96 1 spr97 1 sum97 1

fall96 1

fall97 1

syl.r 1 syl.r 5 syl.r 2 grades 3 p1.r 4 p2.r 1 p2.r 2 p1.r 7 grades 9

Unix directory with file sizes
static int SizeDir ( DirOrFile D )
{
int TotalSize;
TotalSize = 0;
if ( D is a legitimate entry ) {
TotalSize = FileSize( D );

13/17

if ( D is a directory )
for (each child C of D )
TotalSize += SizeDir(C);
} /* end if D is legal */
return TotalSize;
}

T ( N ) = O( N )

Bonus Problem 1
Directory Listing
(2 points)

Due: Friday, November 13th, 2009 at 10:00pm

The problem can be found and submitted at
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1635

Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/

14/17

§2 Binary Trees

 Threaded Binary Trees

Here comes
Because I enjoy giving
the typical question of mine:
you headaches ... Just kidding.
Any
clue
on
how
to
We
can think
replace
Why
do
we
need
You
are
such
a
Okay,
of
a
full
Then
who
should
They are
improve
the
situation?
the null
links
bywith
“threads”
n
+
1.
threaded
genius
binary
!
trees?
binary
tree
n
nodes.
take
the
credit?
Of
course
not!
A.How
J. Perlis
and
C.
Thornton.
Oh
many
of well,
them
which
will
make
traversals
How
many
links
are there?
You
got
it!
I
wish
I’d
have
really
done it
Can are
I stand
that?
NULL?
easier.

15/17

§2 Binary Trees

Rule 1: If Tree->Left is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to
the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a
threaded binary tree must have a head node of
which the left child points to the first node.
typedef struct ThreadedTreeNode *PtrTo ThreadedNode;
typedef struct PtrToThreadedNode ThreadedTree;
typedef struct ThreadedTreeNode {
int
LeftThread; /* if it is TRUE, then Left */
ThreadedTree
Left;
/* is a thread, not a child ptr. */
ElementType
Element;
int
RightThread; /* if it is TRUE, then Right */
ThreadedTree
Right; /* is a thread, not a child ptr. */
}
16/17

§2 Binary Trees

〖Example〗 Given the syntax tree of an expression (infix)
A+BC D
head node

+

F


A


B

F

+

F

D

C
T

A

T

T

B



F



F

17/17

F

T

F

F
T

T
C

D
T

T