CHAPTER 4
§1 Preliminaries
TREES
1. Terminology
Pedigree Tree
( binary tree )
Lineal Tree
1/18
§1 Preliminaries
【 Definition 】 A tree is a collection of nodes. The collection
can be empty; otherwise, a tree consists of
(1) a distinguished node r, called the root;
(2) and zero or more nonempty (sub)trees T1, , Tk, each of
whose roots are connected by a directed edge from r.
Note:
Note:
Subtrees
Subtreesmust
mustnot
notconnect
connecttogether.
together. Therefore
Thereforeevery
every
node
nodeininthe
thetree
treeisisthe
theroot
rootof
ofsome
somesubtree.
subtree.
There are N 1 edges
Thereare edgesin
inaatree
treewith
withNNnodes.
nodes.
Normally
Normallythe
theroot
rootisisdrawn
drawnat
atthe
thetop.
top.
2/18
§1 Preliminaries
degree of a node ::= number of A
subtrees of the node. For example,
degree(A) = 3, degree(F) = 0. B C D
max degree(node )
degree of a tree ::=node
E F G H I J
tree
For example, degree of this tree = 3. K L M
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/18
§1 Preliminaries
path from n1 to nk ::= a (unique) sequence
A
of nodes n1, n2, …, nk such that ni is the
parent of ni+1 for 1 i < k. B C D
length of path ::= number of edges on E F G H I J
the path.
depth of ni ::= length of the unique path K L M
from the root to ni. Depth(root) = 0.
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/18
§1 Preliminaries
2. Implementation
List Representation
A (A)
B C D ( A ( B, C, D ) )
E F G H I J ( A ( B ( E, FSo the
), C ( Gsize
), Dof each
( H, I, J )node
))
depends on the number of
K L M ( A ( B ( E ( K, L ), Fbranches.
), C ( G ), D ( H ( M ), I, J ) ) )
Hmmm... That’s not good.
K
E
B
L
F
A C G
H M
D I
J
5/18
§1 Preliminaries
FirstChild-NextSibling Representation
Element A
N
FirstChild NextSibling
B C D
A N
B C D
E F G H I J
E F G H I J N N N N N N N
K L M K L M
N N N N N
Note: The representation is not unique since the
children in a tree can be of any order.
6/18
§2 Binary Trees
【 Definition 】 A binary tree is a tree in which no node
can have more than two children.
Rotate the FirstChild-NextSibling tree clockwise by 45.
A A
N
45
B
N
E
B C D
N K C
F
N
E F G H I J L G
N
D
N
NN NN N NN
N
N
N
H
N
K L M
N NN NN M I
N
N
J
N
Element Left
N
N
Left Right
Right
7/18
§2 Binary Trees
Expression Trees (syntax trees) +
〖 Example 〗 Given an infix A
expression: D
+BCD
A Expression
Constructing an Tree B C
(from postfix expression)
〖 Example 〗 (a+b)*(c*(d+e))ab+cde+**
=
+
a b *c d * +
e
T2 T2 a + b c T1 d * + e T1 T1
a bT2 T2 c d + e T1
d e
8/18
§2 Binary Trees
Tree Traversals —— visit each node exactly once
Preorder Traversal Postorder Traversal
void preorder ( tree_ptr tree ) void postorder ( tree_ptr tree )
{ if ( tree ) { { if ( tree ) {
visit ( tree ); for (each child C of tree )
for (each child C of tree ) postorder ( C );
preorder ( C ); visit ( tree );
} }
} }
Levelorder Traversal
void levelorder ( tree_ptr tree ) 1
1 2 3 4
{ enqueue ( tree );
2 3
while (queue is not empty) { 5 6 7
visit ( T = dequeue ( ) ); 4 5 6 7
for (each child C of T )
enqueue ( C );
} 12 3 4 5 6 7
}
9/18
§2 Binary Trees
Inorder Traversal Iterative Program
void inorder ( tree_ptr tree ) void iter_inorder ( tree_ptr tree )
{ if ( tree ) { { Stack S = CreateStack( MAX_SIZE );
inorder ( tree->Left ); for ( ; ; ) {
visit ( tree->Element ); for ( ; tree; tree = tree->Left )
inorder ( tree->Right ); Push ( tree, S ) ;
} tree = Top ( S ); Pop( S );
} if ( ! tree ) break;
visit ( tree->Element );
tree = tree->Right; }
〖 Example 〗 Given an }
infix expression:
A+BCD
+
Then inorder traversal A + B C D
A
postorder traversal A B C D +
D
preorder traversal + A B C D
B C
10/18
§2 Binary Trees
〖 Example 〗 Directory listing in a hierarchical file
system.
/usr
mark alex bill
book course hw.c hw.c work course
ch1.c ch2.c ch3.c cop3530 cop3212
fall96 spr97 sum97 fall96 fall97
syl.r syl.r syl.r grades p1.r p2.r p2.r p1.r grades
Unix directory
Listing format: files that are of depth di will have their names
indented by di tabs.
11/18
/usr §2 Binary Trees
mark
book static void ListDir ( DirOrFile D, int Depth )
Ch1.c {
Ch2.c
Ch3.c
if ( D is a legitimate entry ) {
course PrintName (D, Depth );
cop3530 if ( D is a directory )
fall96 for (each child C of D )
syl.r
spr97 ListDir ( C, Depth + 1 );
syl.r }
sum97 }
hw.c
syl.r T ( N ) = O( N )
alex
hw.c
bill
work
Note: Depth is an internal variable and
course must not be seen by the user of this
cop3212 routine. One solution is to define
fall96
grades another interface function as the
p1.r following:
p2.r
fall97 void ListDirectory ( DirOrFile D )
p2.r
p1.r
{ ListDir( D, 0 ); }
grades
12/18
§2 Binary Trees
〖 Example 〗 Calculating the size of a directory.
/usr 1
mark 1 alex 1 bill 1
book 1 course 1 hw.c 6 hw.c 8 work 1 course 1
ch1.c 3 ch2.c 2 ch3.c 4 cop3530 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 ) if ( D is a directory )
{ for (each child C of D )
int TotalSize; TotalSize += SizeDir(C);
TotalSize = 0; } /* end if D is legal */
if ( D is a legitimate entry ) { return TotalSize;
TotalSize = FileSize( D ); }
T ( N ) = O( N )
13/18
§2 Binary Trees
Threaded Binary Trees
Because I enjoy giving Here comes
you headaches ... Just kidding.the Any
typical question of mine:
clue on how to
We can
Okay, replace You
Why are
do such
we a
need
Then who should
Theythink
are of a full improve the situation?
the null links by “threads” n
threaded
take
Of + 1.
genius !
binary trees?
the credit?
course not!
A.How binaryOh
J. Perlis tree
and C.with n nodes.
Thornton.
whichmany
will ofwell,
makethemtraversals
How
You
I wish
Can many
I’dgot
I standhave links
that? are
it! really there?
done it
are NULL?
easier.
14/18
§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. */
}
15/18
§2 Binary Trees
〖 Example 〗 Given the syntax tree of an expression
(infix)
A + B C D head node
+ F F
A
D F + F
B C
T A T F F
F F T D T
T B T T C T
16/18
Bonus Problem 1
Path of Equal Weight
(2 points)
Due: Tuesday, January 7th, 2020 at 23:59pm
The problem can be found and submitted at
https://pintia.cn/