Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Data Structures
CHAPTER 5
Trees
5.1 Introduction
5.2 Binary Trees
5.3 Binary Tree Traversal
5.4 Additional Binary Tree Operations
5.5 Threaded Binary Trees
5.6 Heaps
5.7 Binary Search Trees
5.8 Selection Trees
5.9 Forests
5.10 Set Representations
5.11 Counting Binary Trees
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-1
Contents
Chapter 1
Basic Concepts
Chapter 2
Arrays
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Stacks and Queues
Linked Lists
Trees
Graph
Chapter 7
Sorting
Chapter 8 Hashing
Chapter 9 Heap Structures
Chapter 10 Search Structures
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-2
5.1 Introduction
Def: Tree
A tree T is a finite set of one or more nodes such that
• (1) root, root(T)
• (2) the remaining nodes are partitioned into n ≧ 0
disjoint sets T1,…, Tn, and Ti is a tree
Note 1.
root
T1
T1,T2,T3 are called the
subtree of the root T
T2
T3
Note 2. Recursive definition
Note 3. Genealogical trees: Figure 5.1, p.187
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-3
Introduction: Genealogical trees
Trees
root
Dusty
Honey Bear
Brunhilde
leaf
Gill
Tansey
Brandy
Terry
Tweed
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Coyote
Zoe
Crocus
Primrose
Nugget
Nous
Belle
Chapter 5 Trees
5-4
Introduction: Terminologies
node (13)
leaf (terminal)
nonterminal
parent
children
sibling
ancestor
level of a node
height of a tree (4)
Level
1
A
2
B
3
4
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
C
E
K
D
F
L
H
M
I
J
G
Chapter 5 Trees
5-5
Introduction: Degree, Level, Depth
Degree of a node: # of subtrees of a node
degree 3
level
1
2
2
2
3
2
3
Level of a node: root level 1
(for some textbook,root level 0)
if a node at level L, then its children L+1.
Depth (height) of a tree: max level of any node in the tree.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-6
Introduction: Tree Representation
List Representation:
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
• The root comes first, followed by a list of sub-trees degree of a node
A
B
E
K
C
F
L
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
G
D
H
I
J
M
Chapter 5 Trees
5-7
Introduction: Tree Representation (cont.)
List Representation: linked node
data
Child 1
Child 2
...
Child n
Figure 5.3
How many link fields are needed in such a representation?
Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree k) with n nodes,
then n(k – 1) + 1 of the nk child fields are 0, n ≧ 1.
Proof: The total number of child fields is nk
Non-0 child fields is n –1.
Hence, the number of 0 fields is nk – (n – 1) = n(k – 1) + 1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-8
Introduction: Tree Representation (cont.)
Another way to represent tree structure
(a) Nested sets
A
A
H
J
B
D
G E
B
F
C
H
C
J
D
E
F
G
(b) Nested parentheses
(A(B(H)(J))(C(D)E(G))(F))
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-9
Introduction: Tree Representation (cont.)
(c) Indentation
Ch2 |---------------------------|
A|---------------------------|
B|---------------------|
H|-----------|
J|-----------|
C|---------------------|
D|-----------|
E|-----------|
G|-----|
F|-----------|
(d) Dewey decimal notation
2.1|---------------------|
2.2|---------------------|
2.2.1|-----------|
2.2.2|-----------|
2.3|---------------------|
2.3.1|-----------|
2.3.1.1|-----|
2.3.2|-----------|
2.4|---------------------|
2.5|---------------------|
A 1
1.1
H
1.1.1
B
C 1.2
J D
E
F 1.2.3
1.1.2 1.2.1 1.2.2
G
1.2.2.1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-10
Introduction: Tree Representation
as a Left Child-Right Sibling
A
B
E
K
C
F
L
data
left child right sibling
D
G
H
I
J
A
M
B
F
E
Fig. 5.5
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
K
L
D
C
G
H
I
J
M
Chapter 5 Trees
5-11
Introduction: Tree Representation
as a Left child-Right child (Binary Tree)
A
B
K
L
G
left child right child
D
C
F
E
data
H
I
J
A
M
Left child-Right child
i.e. Degree-Two Tree
i.e. Binary Tree
We can represent any tree as a
binary tree.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
???
B
C
E
Fig. 5.6, p.191
D
H
I
F
G
M
J
K
L
Example 2: Fig. 5.8, p.253
Chapter 5 Trees
5-12
5.2 Binary Trees
Def: Binary tree
is a finite set of nodes, which is
• (i) empty or
• (ii) consists of a root and two disjoint binary trees (called the
left subtree and right subtree)
Cf.
min. node #
degree
A
B
A
B
Trees
Binary Trees
1
0
0, 1, 2 , 3, …
0, 1, 2
(right, left subtree)
the same
different
∴ Binary tree isn’t a special case of a tree; it is another concept.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-13
5.2.1 The Abstract Data Type
ADT 5.1, p. 193
structure Binary_Tree (abbreviated BinTree) is
objects: a finite set of nodes empty or consisting of a root, left Binary_Tree,
and right Binary_Tree.
functions:
for all bt, bt1, bt2BinTree, itemelement
BinTree Create() ::=
create an empty binary tree
Boolean IsEmpty(bt) ::= if(bt == empty binary tree)
return TRUE else return FALSE
BinTree MakeBT(bt1, item, bt2) ::= return a binary tree with bt1 as left
subtree, bt2 as right subtree, and a root containing item
BinTree Rchild(bt) ::=
if(IsEmpty(bt)) return error else
return the left subtree of bt.
element Data(bt) ::=
if(IsEmpty(bt)) return error else
return the data in root of bt.
BinTree Rchild(bt) ::=
if(IsEmpty(bt)) return error else
return the right subtree of bt.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-14
Special Binary Trees
Complete Binary Tree
Skewed Binary Tree
Level
A
A
B
C
B
C
D
E
F
G
Max #
1
1
20
2
2
21
3
4
23-1
4
8
24-1
D
H
I
E
i
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
2i -1
Chapter 5 Trees
5-15
Special Binary Trees (cont.)
Strictly Binary Tree
A
C
B
D
Every nonleaf node has nonempty left
and right subtrees
E
H
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-16
5.2.2 Properties of Binary Trees
Lemma 5.1 (Maximum number of nodes, p.194)
(1) The maximum number of nodes on level i of a binary tree is 2i-1, i1.
(2) The maximum number of nodes in a binary tree of depth k is 2k-1 , k 1.
Pf: (1) The proof is by induction on i
10 Induction Base: The root is the only node on level i = 1.
Hence the maximum # of nodes on level i = 1 is 20 = 1.
20 Induction Hypothesis:
For all j, 1 j < i, the max # of nodes on level j is 2j-1
30 Induction step:
The max # of nodes on level i-1 is 2i-2, by the induction hypothesis.
Since each node in a binary tree has maximum degree 2, the max #
of nodes on level i is 2 times the max # of level i-1 or 2i-1.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-17
Lemma 5.1 (cont.)
Pf: (2) The max # of nodes in a binary tree of depth k is
k
( max # of nodes on level i )
i 1
k
2
i -1
2k - 1
Note:
20
21
k=2
22
k=3
i 1
k=1
Suppose depth d , nodes # n
Then log2 (n 1) d n.
2d -1 n d log (n 1)
2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-18
Lemma 5.2
Lemma 5.2 ( Relationship between the # of leaf nodes and the # of
nodes of degree 2 in a binary tree)
For any nonempty binary tree, T
n0 = the # of leaf (terminal) nodes,
n2 = the # of nodes of degree 2
then n0 = n2 + 1
e.g.
Skewed tree
Complete binary tree
n0=1
n2=0
n0=n2+1
n0=5
n2=4
n0=n2+1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-19
Lemma 5.2 (cont.)
Pf:
10 Let n1 = the #of nodes of degree 1
n = the total # of nodes
Since all nodes in T are of degree 2, we have:
n = n0 + n 1 + n2
---------- (1)
20 Let B = the # of branches , then n = B + 1
(∵except for tree root, each node has a branch leading into it)
30 ∵All branches emanate either from a node of degree one or
from a node of degree 2
∴B = n1 + 2 n2
=> n = B + 1 = n1 + 2n2 + 1 ---------- (2)
(2) - (1)
0 = n2 + 1 - n0
n0 = n2 + 1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-20
Full Binary Tree vs. Complete Binary Tree
A full binary tree of depth k is a binary tree of depth k having 2k -1
nodes, k 0.
A binary tree with n nodes and depth k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary
tree of depth k.
Full binary tree of depth 4
Complete binary tree
A
A
F
E
D
H
I
B
C
B
J
K
L M N O
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
D
G
H
C
E
F
G
I
Chapter 5 Trees
5-21
5.2.3 Binary Tree Representations
A
B
Method 1. Array Representation:
1
2
3
4
5
6
7
8
9 10 11 12 13 14 15
A B
-
C
-
-
-
D
-
-
-
-
-
-
-
C
D
Method 2. Linked Representation:
A 0
B 0
C 0
0 D 0
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
1
2
3
4
5
2 A 0 3 B 0 4 C 0 0 D 0
Chapter 5 Trees
5-22
Array Representation
(in computer)
A
[1]
A
1
[2]
B
[3]
C
[4]
D
[5]
E
G
[6]
F
7
[7]
G
[8]
H
[9]
I
B
C
2
3
E
D
4
5
H
I
8
9
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
F
6
Chapter 5 Trees
5-23
Array Representation: Example
A
B
C
D
E
Disadvantages:
(1) waste space
(2) insertion/deletion problem
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
A
B
-C
---D
-.
E
Chapter 5 Trees
5-24
Array Representation:Lemma 5.3
Lemma 5.3 if a complete binary tree with n nodes is represented
sequentially, then for any node with index i , 1 i n , we have:
(1) parent (i) is at i/2 if i≠1.
A
1
If i = 1, i is at the root and has no parent.
B
2
(2) left_child (i) is at 2i if 2i n.
If 2i > n, then i has no left child.
If 2i + 1 > n, then i has no right child.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
E
5
D
4
(3) right_child (i) is at 2i + 1 if 2i+1 n.
H
8
C
3
F
6
G
7
I
9
Chapter 5 Trees
5-25
Array Representation:Lemma 5.3 (cont.)
E.g.
1
2
3
4
5
6
7
Parent of node 7: parent (7) is at
8
9
10
11
12
13
14
15
7
3.5 3
2
6
3
2
Parent of node 6: parent (6) is at
left_child of 5 is at 2i =10 ,
right_child of 5 is at 2i +1 =11 (2 x 8 = 16 > n has no Child)
Pf:We prove (2)
(3) is an immediate consequence of (2).
(∵the numbering of nodes on same level from left to right)
(1) follows from (2) and (3).
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-26
Array Representation:Lemma 5.3 (cont.)
We prove (2) by induction on i.
(2) left_child (i) is at 2i if 2i n.
If 2i > n, then i has no left child.
For i = 1, clearly the left child is at 2 unless 2 > n
in which case 1 has no left child.
Now assume that for all j, 1 j i, left_child (j) is
at 2j.
Then , the two nodes immediately preceding
i
left_child (i+1) in the representation are the right
child of i and the left child of i.
The left child of i is at 2i.
Hence, the left child of i + 1 is at 2i + 2 = 2(i + 1) 2i
unless
2(i + 1) > n in which case i + 1 has no left child .
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
i+1
2i+2
Chapter 5 Trees
5-27
Linked Representation
Node structure:
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
left_child data right_child
A
A
B
D
C
E
F
G
B
H
D
C
E
F
I
G
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
H
I
Chapter 5 Trees
5-28
5.3 Binary Tree Traversals
Binary Tree Traversals: Combinations
Let L, V, and R stand for moving left, visiting the node, and
moving right.
There are six possible combinations of traversal:
LVR, LRV, VLR, VRL, RVL, RLV
Adopt convention that we traverse left before right, only 3
traversals remain
• LVR: inorder, a+b
V
• LRV: postorder, ab+
data
+
• VLR: preorder, +ab
R
L
a
b
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
left_child
right_child
Chapter 5 Trees
5-29
Binary Tree Traversal: Example
Traversing (walking through) a tree
Inorder traversal
BAC
a+b
infix
Preorder traversal
ABC
+ab
prefix
Postorder traversal BCA
ab+
postfix
E.g. 1
A
C
B
+
E.g. 2
A
a
C
B
F
G
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
b
Inorder traversal
BA____
Preorder traversal
ABC____
Postorder traversal _________
Chapter 5 Trees
5-30
Binary Tree Traversal: Exercise
E.g. 3
inorder traversal
DIBA____
preorder traversal
ABDI____
postorder traversal _________
A
C
B
F
D
G
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-31
Arithmetic Expression using Binary Tree
Arithmetic Expression: A / B * C * D + E
Inorder traversal (infix expression)
+
A/B*C*D+E
Preorder traversal (prefix expression)
E
*
+* * /AB CD E
Postorder traversal (postfix expression)
AB/C*D*E+
Level order traversal
D
*
C
/
+*E*D/CAB
A
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
B
Chapter 5 Trees
5-32
Binary Tree Traversal: Inorder
Inorder traversal
Traverse the left subtree in inorder
visit the root
+
Traverse the right subtree in inorder
Program 5.1: Inorder traversal
E
*
D
*
P.201
C
/
A
B
A/B*C*D+E
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-33
Binary Tree Traversal: Preorder
Preorder traversal
visit the root
+
Traverse the left subtree in preorder
Traverse the right subtree in preorder
E
*
Program 5.2: Preorder traversal
D
*
P.202
C
/
A
B
+**/ABCDE
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-34
Binary Tree Traversal: Postorder
Postorder traversal
Traverse the left subtree
+
Traverse the right subtree
Visit the root
E
*
Program 5.3: Postorder traversal
D
*
P.203
C
/
A
B
AB/C*D*E+
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-35
Binary Tree Traversal: More Topics (skip)
Iterative Inorder Traversal
Level-Order Traversal
Traversal without a Stack
+
Method 1: adding parent field
Method 2: §5.5 Threaded binary tree
D
*
C
/
A
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
E
*
B
Chapter 5 Trees
5-36
5.4 Additional Binary Tree Operations
With the inorder, postorder, or preorder mechanisms, we can
implement all needed binary tree functions
Copying Binary Trees
Program 5.6: Copying a binary tree
p.207
Testing Equality
Program 5.7: Binary tree equivalence
p.207
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-37
The Satisfiability Problem
The Satisfiability Problem
Prepositional Calculus Expression (Formula)
A variable is an expression.
If x and y are expressions, then ¬x, x y, x y are expressions.
Parentheses can be used
Example: x1 (x2 ¬x3)
The Satisfiability Problem: Is there an assignment to make an
expression true?
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-38
The Satisfiability Problem: Example
Example: A B C
The Satisfiability Problem: Is there an assignment to make the
expression A B C true?
T
V
V
C
A
T
C
F
T
B
B
A
T
T
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
F
F
T
Value assignment
Chapter 5 Trees
5-39
The Satisfiability Problem: Complexity
Formula: (x1 ¬x2) (¬ x1 x3) ¬x3
x1
x2
x3
x3
x1
2n possible combinations for n variables: (T,T,T) (T,T,F) (T,F,T) (T,F,F)
(F,T,T) (F,T,F) (F,F,T) (F,F,F)
Note:there are 2n possible assignments of truth values to (x1 , …… , xn)
∴ check O(2n.g), where g is evaluation time
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-40
The Satisfiability Problem: Data Structure
Node structures
T/F
left_child data value right_child
C declaration
typedef emun {not, and, or, true, false } logical;
typedef struct node *tree_pointer;
typedef struct node {
tree_pointer left_child;
logical
data; // ¬, ,
short int
value;
tree_pointer right_child;
};
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-41
The Satisfiability Problem:
10 node structure
Lchild
Data
Evaluating a Formula
T
T. False
Value
Rchild
V
T
ABC
V
^
A
^
C
T
T
20 Program 5.8
30 Program 5.9
T
B
A
B
C
F
F
F
T
Value combination
Postorder: A B C
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-42
The Satisfiability Problem: Algorithm
for (all 2n possible truth value combinations) {
generate the next combination;
replace the variables by their values;
evaluate root by traversing it in postorder;
if (root->value) {
printf(< combination>)’
return;
}
}
printf(“no satisfiable combination\n”);
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-43
The Satisfiability Problem:
Program
void post_order_eval(tree_pointer node)
{
if (node) {
post_order_eval(node->left_child);
post_order_eval(node->right_child);
switch (node->data) {
case not: node->value =!node->right_child->value; break;
case and: node->value =
node->left_child->value && node->right_child->value;
break;
case or: node->value =
node->left_child->value || node->right_child->value;
break;
case true: node->value = TRUE; break;
case false: node->value = FALSE;
}
}
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-44
5.5 Threaded Binary Trees
Two many null pointers in current representation of binary trees
n: number of nodes
number of non-null links: n – 1
total links: 2n
null links: 2n - (n - 1) = n + 1
*
9 nodes
8 non-null link
10 null-link or 10 0-links
Replace these null pointers with some useful “threads”.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-45
Threaded BT: Using Null Fields
Replace null pointers with “threads”.
If left_child of ptr is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal.
i.e. inorder predecessor of ptr
If right_child of ptr is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal.
i.e. inorder successor of ptr
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-46
Threaded BT: Example
A Threaded Binary Tree
Inorder traversal: H, D, I, B, E, A, F, C, G
A
dangling
C
B
dangling D
E
I
H
G
F
left_child: inorder predecessor
right_child: inorder successor
Problem: How to create a threaded BT without dangling links?
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-47
Threaded BT: Adding Head Node
Inorder traversal: H, D, I, B, E, A, F, C, G
Root = Head Node
A
dangling
C
B
dangling
D
H
E
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
F
G
left_child: inorder predecessor
right_child: inorder successor
Chapter 5 Trees
5-48
Threaded BT: Data Structures
Data Structures for Threaded Binary tree
left_thread left_child data right_child right_thread
f
t
true: thread
false: child
Node structure definition for threaded BT
typedef struct threaded_tree *threaded_pointer;
typedef struct threaded_tree {
short int left_thread;
threaded_pointer left_child;
char data;
threaded_pointer right_child;
short int right_thread;
};
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-49
Threaded BT: Memory Representation
Memory Representation of A Threaded Binary tree
root
B
f
D
f
t
H
t
t
--
f
f
A
f
C
f
f
E
t
f
f
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
t
t
F
t
f
t
G
t
t
Chapter 5 Trees
5-50
Threaded BT: Empty
An Empty Threaded Binary Tree
left_thread left_child data right_child right_thread
f
t
false: child
true: thread
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-51
Threaded BT: Inserting nodes
Inserting nodes into Threaded Binary tree
Insert child as the right child of node parent
(1) change parent->right_thread to FALSE
(2) set child->left_thread and child->right_thread to TRUE
(3) set child->left_child to point to parent
(4) set child->right_child to parent->right_child
(5) change parent->right_child to point to child
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-52
Threaded BT: Inserting Node
Examples:Insert a node D as a right child of B.
Case 1:
root
root
A
A
B
(1)
parent
B
parent
(3)
C
D
child
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
C
(2)
D
child
Chapter 5 Trees
5-53
Threaded BT: Inserting Node (cont.)
Case 2:
root
root
A
A
parent
parent
child
B
C
(3)
D
C
E
F
B
G
(2)
D
(1)
E
(4)
F
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
child
G
Chapter 5 Trees
5-54
Threaded BT: Inserting Node (cont.)
Program 5.12, p. 218
void insert_right(thread_pointer parent, thread_pointer child)
// Insert child as the right child of parent
{
child->right_child = parent ->right_child;
child->right_thread = parent -> right_thread;
child->left_child = parent;
child->left_thread = TRUE;
// left_child is a thread
parent->right_child = child;
// attach child to parent
parent->right_thread = FALSE;
if (! child->right_thread) {
temp = insucc(child); // returns the inorder successor of child
temp->left_child = child;
}
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-55
5.6 Heaps
Def. Min Tree: value of parent value of children
2
3
5
6
8
9
4
10
11
7
Def. Max Tree: value of parent value of children
max
‧‧‧
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-56
Min Heap vs. Max Heap
Def. Min Heap: (1) min tree and
(2) complete binary tree
2
7
10
8
4
6
Def. Max Heap: (1) max tree and
(2) complete binary tree
Operations on heaps
• create an empty heap
• insert a new node
• delete a node
•…
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
ADT for Max Heap
• p. 220
• insert a new node
• delete a node
Chapter 5 Trees
5-57
Heaps: Insert a Node
Insertion into a max heap
Max heap
20
15
14
2
10
E.g.1. Add “5”
∵complete binary tree
E.g.2. Add “21”
21
20
15
5
14 10 2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
15
20
14 10 2
Chapter 5 Trees
5-58
Heaps: Insert a Node
Insertion to a Max Heap
20
20
Program 5.13
2
15
14
10
initial location of new node
5
15
14
10
p.223
2
insert 5 into heap
Time complexity: O(log2n)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-59
Heaps: Delete a Node
Deletion from a Max Heap
Program 5.14
p.225
14
10
2
15
2
15
15
10
remove 20
14
10
10
2
14
Time complexity: O(log2n)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-60
Time complexity for Different DS
Time complexity for different data structures for
priority queue
Representation
Insertion Deletion
(Data Structures)
Unordered array
(1)
(n)
Sorted array
O(n)
(1)
Unordered linked list
(1)
(n)
Sorted linked list
O(n)
(1)
Max heap
O(log2n)
O(log2n)
More topics about Heap on Ch. 9: Heap Structures
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-61
5.7 Binary Search Tree
Def. Binary Search Tree, T
is a binary tree
T=ψ or
T≠ψ (1) all keys are distinct
(2) keys in left subtree are smaller than the key in the root
(3) keys in right subtree are larger than the key in the root
(4) the left and right subtree are also b.s.t.
E.g. Fig 5.30, p.227 (a)〤 , (b)V, (c)V
30
20
10
5
25
12
15
22
Not a binary search tree
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
2
60
40
70
65
80
binary search trees
Chapter 5 Trees
5-62
Binary Search Tree: Searching
Searching a binary search tree
Recursive Search of a Binary Search Tree
• p. 227 Program 5.15
‧‧‧‧
Ex.
Iterative Search of a Binary Search Tree
• p. 228 Program 5.16
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-63
Binary Search Tree: Insert Node
30
5
2
30
30
40
80
2
Insert 80
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
40
5
40
5
2
35
80
Insert 35
Chapter 5 Trees
5-64
Binary Search Tree: Deletion
Case 1: One child or Leaf Node
30
30
delete 5
80
2
80
2
1
1
X
2
T1
T1
T2
T2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-65
Binary Search Tree: Deletion (cont.)
Case 2: Two children
40
40
60
20
10
30 50
70
55
45
55
20
10
30
45
70
50
52
52
Before deleting 60
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
After deleting 60
Chapter 5 Trees
5-66
5.8 Selection Trees (暫略)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-67
5.9 Forests
General tree
5-ary tree
………
K-ary tree: if k is the max degree of any node
Node structure:
A
7-ary tree
or
Data
Waste space
1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
2 ....... K
Chapter 5 Trees
5-68
Forests: Definition
Definition: A forest is a set of n 0 disjoint trees
A
B
C
E
D
F
G
H
I
Figure 5.34: Three-tree forest
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-69
Transform a Forest into a Binary Tree
Definition: If T1, T2, … , Tn: a forest of trees
B(T1, T2, … , Tn): a binary tree corresponding to this forest
we define B(T1, T2, … , Tn) by
B
(1) empty, if n = 0
A
(2) has root equal to root(T1)
has left subtree equal to B(T11, T12, … , T1m)
has right subtree equal to B(T2, T3, … , Tn)
A
B
C
E
D
F
G
H
Forest
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
B
E
I
G
F
C
D
H
Binary tree
I
Chapter 5 Trees
5-70
Transform a Forest into a Binary Tree (cont.)
To represent any forest as a binary tree.
Consider the following forest
A
B
D
C
E
F
G
K
H
J
Step 1: Transform each tree into a binary tree
D
A
B
C
E
K
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
H
F
G
J
Chapter 5 Trees
5-71
Transform a Forest into a Binary Tree (cont.)
Step 2: Regard the other trees as the sibling of the first tree and link
their roots together
A
B
D
C
K
E
H
F
G
J
A
Step 3: Tilt tree diagram 45° clockwise
D
B
E
C
K
H
F
J
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
G
5-72
Forest Traversals: Preorder
Preorder
- If F is empty, then return
- Visit the root of the first tree of F
- Traverse the subtrees of the first tree in tree preorder
- Traverse the remaining trees of F in tree preorder
A
preorder: A B C D E F G H I
Equivalent
B
E
B
D
E
G
G
F
C
A
H
preorder: A B C D E F G H I
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
C
D
F
H
I
Preorder is a time-honored concept which might be
called dynastic order.
order of succession to the throne.
Chapter 5 Trees
5-73
Forest Traversals: Inorder
Inorder
- If F is empty, then return
-Traverse the subtrees of the first tree in tree inorder
- Visit the root of the first tree
- Traverse the remaining trees in tree inorder
A
inorder: B C D A F E H I G
Equivalent
B
E
B
D
E
G
G
F
C
A
C
D
F
H
I
H
inorder: B C D A F E H I G
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-74
Forest Traversals: Postorder
Postorder
- If F is empty, then return
- Traverse the subtrees of the first tree in tree postorder
- Traverse the remaining trees in tree postorder
- Visit the root of the first tree
Note: Postorder has no corresponding binary tree traversal
of a forest
Postorder: D C B F I H G E A Postorder: B C D F E H I G A
A
B
E
D
H
B
C
G
E
A
G
F
C
Equivalent
D
F
H
I
I
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-75
Applications of Trees (略)
Manipulation of Algebraic Formula
Set Representation
Decision Tree
A Huffman Decode Tree
…
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-76
Manipulation of Algebraic Formula
Evaluate a Expression: in §3.4, p.116
X = A/B-C+D*E-A*C ------ (Formula, p.116)
• Postorder => AB/C-DE*+AC*=> Program 3.9 void eval(expression e) (p.122)
Formula => Binary Tree
-
+
*
-
/
A
*
C
D
A
C
E
B
Postorder Traversal on tree AB/C-DE*+AC*=> To evaluate the Expression/Formula
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-77
A Huffman Decode Tree
Consider the coding schemes for {A,B,C,D,E}
•
Method 1:
A: 000
B: 001
C: 010
D: 011
E: 100
•
average length = 3 bits/char
encoding: 010 100 011
C E D
the most commonly occurring characters are
represented by the shortest strings.
100%
0
1
65%
Method 2:
A: 1
B: 01
C: 001
D: 0001
E: 0000
• Method 3:: Huffman coding
average length = (1+2+3+4+4)/5
= 2.8 bits/char
encoding: 01 0000 001
B E D
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Assume: A:
B:
C:
D:
E:
30% - 01
5% - 0000
10% - 0001
20% - 001
35% - 1
35%
1
0
15%
0
B
5%
1
E
1
35% 0
D
20%
A
30%
C
10%
average length = 2*30%+4*5%+4*10%+3*20%+1*35%
= 2.15 bits/char
encoding: 01 001 0000
A D B
Chapter 5 Trees
5-78
Decision Trees
Consider the “eight coins” problem:
Given coins a, b, c, d, e, f, g, h.
One is a counterfeit and has a different weight than the others.
We want to determine which coin it is, making use of an equal arm
balance.
a+b+c ? d+e+f
>
a+d ? b+e
=
g? h
<
a+d ? b+e
> =
<
<
> =
<
>
a?b
c?a
b?a
h?a
a?b
a?c
b?a
g?a
=
= >
= > = >
>
= >
=
>
= >
= >
aH
eL cH fL bH dL gH hL hH gL bL dH cL fH aL eH
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-79
Decision Trees (cont.)
Procedure EIGHTCOINS (*eight weights are input; the different one is discovered
using only 3 comparisons *)
read(a, b, c, d, e, f, g, h)
Case :a+b+c=d+e+f: If g>h then call COMP(g, h, a)
else call COMP(h, g, a)
:a+b+c>d+e+f: case
:a+d=b+e: call COMP(c, f, a)
:a+d>b+e: call COMP(a, e, b)
:a+d<b+e: call COMP(b, d, a)
end
:a+b+c<d+e+f: case
:a+d=b+e: call COMP(f, c, a)
:a+d>b+e: call COMP(d, b, a)
:a+d<b+e: call COMP(e, a, b)
end
Procedure COMP(x, y, z)
If x > y then print (x
‘heavy’)
else print (y ‘light’)
End COMP.
end
End EIGHTCOINS
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 5 Trees
5-80