LKCFES - Lee Kong Chian Faculty of Engineering & Science ...
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures...
-
Upload
candice-brown -
Category
Documents
-
view
216 -
download
0
Transcript of Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures...
5-1Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Introduction to Data StructuresCHAPTER 5
Trees5.1 Introduction 5.2 Binary Trees5.3 Binary Tree Traversal5.4 Additional Binary Tree Operations5.5 Threaded Binary Trees5.6 Heaps
5.7 Binary Search Trees
5.8 Selection Trees
5.9 Forests
5.10 Set Representations
5.11 Counting Binary Trees
5-2Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Chapter 1 Basic Concepts
Chapter 2 Arrays
Chapter 3 Stacks and Queues
Chapter 4 Linked Lists
Chapter 5 Trees
Chapter 6 Graph
Chapter 7 Sorting
Chapter 8 Hashing
Chapter 9 Heap Structures
Chapter 10 Search Structures
Contents
5-3Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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.
Note 2. Recursive definition Note 3. Genealogical trees: Figure 5.1, p.187
T1, T2, T3 are called the
subtree of the root T
root
T2
T3
T1
5-4Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Trees
DustyDusty
Honey BearHoney Bear BrandyBrandy
BrunhildeBrunhilde TerryTerry
GillGill TanseyTansey TweedTweed ZoeZoe
CoyoteCoyote NuggetNugget
CrocusCrocus PrimrosePrimrose NousNous BelleBelle
Introduction: Genealogical trees
leaf
root
5-5Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Level
1
2
3
4
node (13) leaf (terminal) nonterminal parent children sibling ancestor level of a node height of a tree
(4)
AA
BB CC DD
EE FF
KK LL GG
HH II JJ
MM
Introduction: Terminologies
5-6Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Introduction: Degree, Level, Depth
Degree of a node: # of subtrees of a node
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.
3
2
degree level 1
2 2 2
3 3
5-7Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
AA
BB CC DD
EE FF
KK LL
GG HH II JJ
MM
5-8Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Introduction: Tree Representation (cont.)
List Representation: linked node
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
Child n...Child 2Child 1data
5-9Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Another way to represent tree structure (a) Nested sets
(b) Nested parentheses
(A(B(H)(J))(C(D)E(G))(F))
H J D G FE
A
B C
H J D E F
G
B C
A
Introduction: Tree Representation (cont.)
5-10Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
(c) Indentation
(d) Dewey decimal notation
A|---------------------------| B|---------------------| H|-----------| J|-----------| C|---------------------| D|-----------| E|-----------| G|-----| F|-----------|
Ch2 |---------------------------| 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
B C
H J D E F
G
1
1.1
1.1.1 1.1.2 1.2.21.2.3
1.2.2.1
1.2.1
1.2
Introduction: Tree Representation (cont.)
5-11Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B C D
E F G H I J
K L M
data
left child right sibling
Introduction: Tree Representationas a Left Child-Right Sibling
AA
BB CC DD
EE FF
KK LL
GG HH II JJ
MM
Fig. 5.5
5-12Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B
C
D
E
F G
HI
JK
L
M
data
left child right child
Introduction: Tree Representationas a Left child-Right child (Binary Tree)
A
B C D
E F G H I J
K L M ???
Left child-Right child i.e. Degree-Two Tree i.e. Binary Tree We can represent any tree as a binary tree.
Fig. 5.6, p.191
Example 2: Fig. 5.8, p.253
5-13Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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.
∴ Binary tree isn’t a special case of a tree; it is another concept.
Trees Binary Trees
min. node # 1 0
degree 0, 1, 2 , 3, …0, 1, 2
(right, left subtree)
the same differentB
AA
B
5-14Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
5.2.1 The Abstract Data Type ADT 5.1, p. 193structure 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.
5-15Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B
C
D
E
Skewed Binary Tree
Special Binary Trees
A
B C
GE
I
D
H
F
Complete Binary Tree
Level Max #
1 1 20
2 2 21
3 4 23-1
4 8 24-1
i 2i -1
5-16Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Strictly Binary Tree
Special Binary Trees (cont.)
A
B C
E
I
D
H
Every nonleaf node has nonempty left and right subtrees
5-17Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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 2k1 , 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.
5-18Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Lemma 5.1 (cont.) Pf: (2) The max # of nodes in a binary tree of depth k is
Note:
)1(log 12
.)1(log Then # nodes ,depth Suppose
2
2
ndn
ndnnd
d
k=1
k=2
k=3
20
21
22122
) level on nodes of #max (
1
1
1
kk
i
i
k
i
i
5-19Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
n0=1n2=0 n0=n2+1
Complete binary tree
n0=5n2=4n0=n2+1
Lemma 5.2
5-20Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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 + n1 + 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
Lemma 5.2 (cont.)
5-21Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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.
A
B C
GE
I
D
H
F
A
B C
GE
K
D
J
F
IH ONML
Full binary tree of depth 4 Complete binary tree
Full Binary Tree vs. Complete Binary Tree
5-22Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B
C
D
-------D---C-BA1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0D00C40B30A2
1 2 3 4 5
Method 1. Array Representation:
Method 2. Linked Representation:
0A
0B
0C
0D0
5.2.3 Binary Tree Representations
5-23Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
A
B
C
D
E
F
G
H
I
A
B C
GE
I
D
H
F
1
2 3
4 5 6 7
8 9
Array Representation
(in computer)
5-24Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B
--
C
--
--
--
D
--
.
E
[1][2][3][4][5][6][7][8][9].[16]
A
B
E
C
D
Disadvantages:
(1) waste space
(2) insertion/deletion problem
Array Representation: Example
5-25Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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.
If i = 1, i is at the root and has no parent.
(2) left_child (i) is at 2i if 2i n.
If 2i > n, then i has no left child.
(3) right_child (i) is at 2i + 1 if 2i+1 n.
If 2i + 1 > n, then i has no right child.
Array Representation:Lemma 5.3
A
B C
GE
I
D
H
F
1
2 3
4 5 6 7
8 9
5-26Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
E.g.
Parent of node 7: parent (7) is at
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
35.32
7
32
6
Array Representation:Lemma 5.3 (cont.)
5-27Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
We prove (2) by induction on i.
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 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) u
nless 2(i + 1) > n in which case i + 1 has no left child .
i i+1
2i+22i
Array Representation:Lemma 5.3 (cont.)
(2) left_child (i) is at 2i if 2i n. If 2i > n, then i has no left child.
5-28Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Node structure:
typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; };
Linked Representation
left_child data right_child
A
B C
D
H
E F
G I
A
C
D
B
FE
G IH
5-29Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Let L, V, and R stand for moving left, visiting the node, and mo
ving right. There are six possible combinations of traversal:
LVR, LRV, VLR, VRL, RVL, RLV Adopt convention that we traverse left before right, only 3 traver
sals remain• LVR: inorder, a+b• LRV: postorder, ab+• VLR: preorder, +ab
Binary Tree Traversals: Combinations
5.3 Binary Tree Traversals
data
left_child right_child
V
L R+
a b
5-30Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
A
B C
A
B C
GF
Inorder traversal BA____
Preorder traversal ABC____
Postorder traversal _________
E.g. 2
E.g. 1
+
a b
5-31Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Binary Tree Traversal: Exercise inorder traversal DIBA____ preorder traversal ABDI____ postorder traversal _________
A
B C
G
I
D F
E.g. 3
5-32Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Inorder traversal (infix expression)
A / B * C * D + E
Preorder traversal (prefix expression)
+ * * / A B C D E
Postorder traversal (postfix
expression)
A B / C * D * E +
Level order traversal
+ * E * D / C A B
Arithmetic Expression: A / B * C * D + E
Arithmetic Expression using Binary Tree
+
*
A
*
/
E
D
C
B
5-33Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Inorder traversal Traverse the left subtree in inorder
visit the root
Traverse the right subtree in inorder Program 5.1: Inorder traversal
Binary Tree Traversal: Inorder
+
*
A
*
/
E
D
C
B
A / B * C * D + E
P.201
5-34Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Binary Tree Traversal: Preorder
Preorder traversal visit the root
Traverse the left subtree in preorder
Traverse the right subtree in preorder Program 5.2: Preorder traversal
+
*
A
*
/
E
D
C
B
+ * * / A B C D E
P.202
5-35Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Postorder traversal Traverse the left subtree
Traverse the right subtree
Visit the root Program 5.3: Postorder traversal
Binary Tree Traversal: Postorder
+
*
A
*
/
E
D
C
B
A B / C * D * E +
P.203
5-36Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Iterative Inorder Traversal Level-Order Traversal Traversal without a Stack
Binary Tree Traversal: More Topics (skip)
+
*
A
*
/
E
D
C
B
Method 1: adding parent field
Method 2: §5.5 Threaded binary tree
5-37Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
5.4 Additional Binary Tree Operations With the inorder, postorder, or preorder mechanisms, we can impleme
nt all needed binary tree functions
Copying Binary Trees Program 5.6: Copying a binary tree
Testing Equality Program 5.7: Binary tree equivalence
p.207
p.207
5-38Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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?
The Satisfiability Problem
5-39Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Example: A B C The Satisfiability Problem: Is there an assignment to make the
expression A B C true?
The Satisfiability Problem: Example
V
C
A B
T
B
C
V T
T F
T F
T F T
A
Value assignment
5-40Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
x3x1
x2x1
x3
Formula: (x1 ¬x2) (¬ x1 x3) ¬x3
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
The Satisfiability Problem: Complexity
5-41Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
left_child data value right_child
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; } ;
Node structures
The Satisfiability Problem: Data Structure
C declaration
T/F
5-42Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
10 node structure
A B C
20 Program 5.8
30 Program 5.9
Lchild Data Value Rchild
T. False
V
^ C
A B
T
B
C
V T
T^ F
T F
T F T
A
The Satisfiability Problem: Evaluating a Formula
Value combination
Postorder: A B C
5-43Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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”);
The Satisfiability Problem: Algorithm
5-44Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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;}
}}
The Satisfiability Problem: Program
5-45Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Two many null pointers in current representation of binary treesn: number of nodesnumber of non-null links: n – 1total links: 2nnull links: 2n (n 1) = n + 1
Replace these null pointers with some useful “threads”.
*
9 nodes8 non-null link10 null-link or 10 0-links
5.5 Threaded Binary Trees
5-46Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
Replace null pointers with “threads”.
Threaded BT: Using Null Fields
5-47Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A
B C
GE
I
D
H
Fdangling
dangling
Inorder traversal: H, D, I, B, E, A, F, C, G
A Threaded Binary Tree
Threaded BT: Example
left_child: inorder predecessor right_child: inorder successor
Problem: How to create a threaded BT without dangling links?
5-48Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Threaded BT: Adding Head Node
A
B C
GE
I
D
H
Fdangling
dangling
Inorder traversal: H, D, I, B, E, A, F, C, G
Root = Head Node
left_child: inorder predecessor right_child: inorder successor
5-49Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
left_thread left_child data right_child right_thread
t f
false: childtrue: thread
Data Structures for Threaded Binary tree
Threaded BT: Data Structures
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; };
Node structure definition for threaded BT
5-50Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Memory Representation of A Threaded Binary tree
f f--
f fA
f fCf fB
t tE t tF t tGf fD
t tIt tH
root
Threaded BT: Memory Representation
5-51Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
t f
false: childtrue: thread
An Empty Threaded Binary Tree
Threaded BT: Empty
left_thread left_child data right_child right_thread
5-52Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
Inserting nodes into Threaded Binary tree
Threaded BT: Inserting nodes
5-53Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Examples:Insert a node D as a right child of B.
root
parent
A
B
C D child
Case 1:
parent
A
B
C D child
(1)
(2)
(3)
root
Threaded BT: Inserting Node
5-54Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Case 2:root
parentA
B
C
D
child
E
F G
(1)
(3)
(4)
(2)
root
parentA
B
C D
child
E
F G
Threaded BT: Inserting Node (cont.)
5-55Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Threaded BT: Inserting Node (cont.)
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 threadparent->right_child = child; // attach child to parentparent->right_thread = FALSE;if (! child->right_thread) {
temp = insucc(child); // returns the inorder successor of childtemp->left_child = child;
}}
Program 5.12, p. 218
5-56Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Def. Min Tree: value of parent value of children
Def. Max Tree: value of parent value of children
5.6 Heaps
2
8 43
5 9 10 76 11
‧‧‧
max
5-57Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Def. Min Heap: (1) min tree and
(2) complete binary tree
Def. Max Heap: (1) max tree and
(2) complete binary tree
Min Heap vs. Max Heap
2
7 4
10 8 6
Operations on heaps
• create an empty heap• insert a new node• delete a node• …
ADT for Max Heap
• p. 220• insert a new node• delete a node
5-58Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Insertion into a max heap Max heap
E.g.1. Add “5” E.g.2. Add “21”
Heaps: Insert a Node
20
15 2
14 10complete binary tree
20
15 5
14 10 2
21
15 20
14 10 2
5-59Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Insertion to a Max Heap
2
20
215
14 10
initial location of new node
20
15 5
14 10 2
insert 5 into heap
Heaps: Insert a Node
Program 5.13 p.223
Time complexity: O(log2n)
5-60Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Deletion from a Max Heap
remove 20
15 2
14 10
10
215
14 10
15
10 2
14
Heaps: Delete a Node
Time complexity: O(log2n)
Program 5.14 p.225
5-61Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Time complexity for different data structures for priority queue
More topics about Heap on Ch. 9: Heap Structures
Time complexity for Different DS
Representation (Data Structures)
Insertion Deletion
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)
5-62Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
5.7 Binary Search Tree
30
5 40
2
60
70
65 80
20
2512
10 15 22
Not a binary search tree binary search trees
5-63Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Searching a binary search tree Recursive Search of a Binary Search Tree
• p. 227 Program 5.15
‧‧‧‧
Iterative Search of a Binary Search Tree
• p. 228 Program 5.16
Ex.
Binary Search Tree: Searching
5-64Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
30
5 40
2
Insert 80 Insert 35
80
30
5 40
2 35 80
30
5 40
2
Binary Search Tree: Insert Node
5-65Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
30
5
2
80
1
2X
T1
T2
1
T1 T2
delete
30
2 80
Binary Search Tree: Deletion Case 1: One child or Leaf Node
5-66Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
40
5520
10 30 50 70
45 52
Before deleting 60 After deleting 60
Case 2: Two children
40
6020
10 30 50 70
45 55
52
Binary Search Tree: Deletion (cont.)
5-68Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
General tree
K-ary tree: if k is the max degree of any node
Node structure:
or
Waste space
5.9 Forests
………
5-ary tree
A 7-ary tree
Data
1 2 ....... K
5-69Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Definition: A forest is a set of n 0 disjoint trees
A
B C D
E
F
G
H I
Forests: Definition
Figure 5.34: Three-tree forest
5-70Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
(1) empty, if n = 0
(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)
Transform a Forest into a Binary Tree
A
B C D
E
F
G
H I
G
H
I
A
B
C
D
F
E
Forest Binary tree
B
5-71Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
To represent any forest as a binary tree. Consider the following forest
Step 1: Transform each tree into a binary tree
B C
A
E F
D
G
H JK
Transform a Forest into a Binary Tree (cont.)
B C
A
K
E F
D
G
H J
5-72Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Step 2: Regard the other trees as the sibling of the first tree and link their roots together
Step 3: Tilt tree diagram 45° clockwise
B C
A
KE F
D
G
H J
B
C
A
K
E
F
D
G
H
J
Transform a Forest into a Binary Tree (cont.)
5-73Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
Forest Traversals: Preorder
preorder: A B C D E F G H I
G
H
I
A
B
C
D
F
E A
B C D
E
F
G
H I
preorder: A B C D E F G H I
Equivalent
Preorder is a time-honored concept which might be
called dynastic order.
order of succession to the throne.
5-74Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
Forest Traversals: Inorder
inorder: B C D A F E H I G
G
H
I
A
B
C
D
F
E A
B C D
E
F
G
H I
inorder: B C D A F E H I G
Equivalent
5-75Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
G
H
I
A
B
C
D
F
E
A
B C D
E
F
G
H I
Postorder: D C B F I H G E A Postorder: B C D F E H I G A
Forest Traversals: Postorder
Equivalent
5-76Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
Manipulation of Algebraic Formula Set Representation Decision Tree A Huffman Decode Tree …
Applications of Trees (略 )
5-77Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
Postorder Traversal on tree AB/C-DE*+AC*-
=> To evaluate the Expression/Formula
-
*+
- * A C
/ C
A B
D E
Manipulation of Algebraic Formula
5-78Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
A Huffman Decode Tree Consider the coding schemes for {A,B,C,D,E}
• Method 1:
• Method 2:
A: 000B: 001C: 010D: 011E: 100
average length = 3 bits/charencoding: 010 100 011 C E D
A: 1B: 01C: 001D: 0001E: 0000
average length = (1+2+3+4+4)/5 = 2.8 bits/charencoding: 01 0000 001 B E D
• Method 3:: Huffman coding
the most commonly occurring characters are represented by the shortest strings.
Assume: A: 30% - 01 B: 5% - 0000 C: 10% - 0001 D: 20% - 001 E: 35% - 1
5%
20%
100%
65%
35%
15%
B C
D
A
E
0
0
0
10%
0 1
1
1
1
35%
30%
average length = 2*30%+4*5%+4*10%+3*20%+1*35% = 2.15 bits/charencoding: 01 001 0000 A D B
5-79Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 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.
Decision Trees
a+b+c ? d+e+f
a+d ? b+e a+d ? b+e g ? h
>
>
> > > >
>
> > > >
>
=
= =
= = = = = = = =
<
< < <
a?b c?a b?a g?a h?a a?b a?c b?a
aH eL cH fL bH dL gH hL hH gL bL dH cL fH aL eH
5-80Been-Chian Chien,Wei-Pang Yang and Wen-Yang Li
nChapter 5 Trees
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
endEnd EIGHTCOINS
Decision Trees (cont.)
Procedure COMP(x, y, z)
If x > y then print (x ‘heavy’)
else print (y ‘light’)
End COMP.