Professional Documents
Culture Documents
Chapter5 Trees
Chapter5 Trees
Chapter5 Trees
Trees
All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
2021/12/6 Trees 1
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
leaf
2021/12/6 Trees 3
Definition of Trees
A tree is a finite set of one or more nodes
such that:
There is a specially designated node called
the root.
The remaining nodes are partitioned into n>=0
disjoint sets T1, ..., Tn, where each of these sets
is a tree.
We call T1, ..., Tn the subtrees of the root.
2021/12/6 Trees 4
Level and Depth
Level
node (13)
degree of a node
leaf (terminal) A 1
nonterminal 3 1
parent B C D
2 2
children 2 2 1 2 2
sibling
degree of a tree (3) E F G H I J
ancestor 1 30 30 31 30 30 33
level of a node
K L M
height of a tree (4) 0 40 0 4
4 4
2021/12/6 Trees 5
Terminology
The degree of a node is the number of subtrees
of the node
– The degree of A is 3; the degree of C is 1.
The degree of a tree is the maximum of the degree of
the nodes in the tree.
The node with degree 0 is a leaf or terminal
node.
A node that has subtrees is the parent of the subtrees,
and these subtrees are the children of the node.
Children of the same parent are siblings.
The ancestors of a node are all the nodes
along the path from the root to the node.
2021/12/6 Trees 6
Representation of Trees
List Representation
Left Child-Right Sibling Representation
2021/12/6 Trees 7
Representation of Trees
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
A 1
B C D 2
E F G H I J 3
K L M Trees 4 8
Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree
k) with n nodes, each having a fixed size as in Figure
5.4, then n(k-1)+1 of the nk child fields are 0, n ≥ 1.
2021/12/6 Trees 9
List Representation of Trees
A 0
B F 0 C G 0 D I J 0
E K L 0 H M 0
( A ( B ( E ( K, L ),標籤(tag)欄位沒有顯示出來
F ), C ( G ), D ( H ( M ), I, J ) ) )
LEVEL
階層
A 1
B C D 2
E F G H I J 3
K L M 4
2021/12/6 Trees 10
Two Fixed-Node-Size Representation
for Trees
LeftChild-Right Sibling Representation
Representation as a Degree-Two Tree
(Binary Trees)
2021/12/6 Trees 11
Left Child - Right Sibling
階層
Level
A 1
B C D 2
E F G H I J 3
data
K L M 4
left child right sibling
A
B C D
E F G H I J
K L M
2021/12/6 Trees 12
Transforming General Trees into Binary Trees
A
A
B C D
B C D
A
E F G H I E F G H I
B
A C
E D
B C D F
G
H
E F G H I I
2021/12/6 Trees 13
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
A A 1
B C D 2
B E F G H I J 3
E C K L M 4
G D
K F
H
L
M I
J
2021/12/6 Trees 16
Abstract Data Type Binary_Tree
ADT Binary_Tree(abbreviated BinTree) is
objects: a finite set of nodes either empty or
consisting of a root node, left Binary_Tree,
and right Binary_Tree.
functions:
for all bt, bt1, bt2 ∈ BinTree, item ∈ element
Bintree Create()::= creates an empty binary tree
Boolean IsEmpty(bt)::= if (bt==empty binary
tree) return TRUE else return FALSE
2021/12/6 Trees 17
BinTree MakeBT(bt1, item, bt2)::= return a binary tree
whose left subtree is bt1, whose right subtree is bt2,
and whose root node contains the data item
Bintree Lchild(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 the root node of bt
Bintree Rchild(bt)::= if (IsEmpty(bt)) return error
else return the right subtree of bt
2021/12/6 Trees 18
Samples of Trees
LEVEL Complete Binary Tree
1 A
A A
B B 2 B C
D
4 H I
E 5
2021/12/6 Trees 19
Lemma 5.2:
Maximum Number of Nodes in BTs
The maximum number of nodes on level i of a
binary tree is 2i-1, i>=1.
The maximum number of nodes in a binary tree
of depth k is 2k-1, k>=1.
Prove by induction.
k
∑ 2 i −1
= 2 k
−1
i =1
2021/12/6 Trees 20
Lemma 5.3 Relations between Number of
Leaf Nodes and Nodes of Degree 2
For any nonempty binary tree, T, if n0 is the number
of leaf nodes and n2 the number of nodes of degree
2, then n0=n2+1.
proof:
Let n and B denote the total number of nodes &
branches in T.
Let n0, n1, n2 represent the nodes with no children,
single child, and two children respectively.
n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n,
n1+2n2+1= n0+n1+n2 ==> n0=n2+1 21
Lemma 5.3
LEVEL Complete Binary Tree
n0=1 1 A
A A
n2=0
n0=n2+1 n0=1
n=5 n2=0
B n0=n2+1 B 2
B=4 B C
n=B+1 n=2
n1=4 B=1
C n=B+1
B=n1+2n2 3
n1=1 D E F G
B=n1+2n2 n0=5
D n2=4
Skewed Binary Tree
n0=n2+1
4 H I n=9
E 5 B=8
n=B+1
2021/12/6 Trees
n1=0 22
B=n1+2n2
Full BTs VS Complete BTs
A fullbinary tree of depth k is a binary tree of
depth k having 2 -1knodes, 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
由上至下, A
由左至右編號
B C B C
D E F G D E F G
H I J K L M N O
H I
Complete binary tree Trees Full binary tree of depth 4 23
Full Binary Tree of Depth 4 with Complete Binary Tree
Sequential Numbers of Depth 4
1 1
2 3
2 3
4 5 6 7
4 5 6 7
8 9
8 9 10 11 12 13 14 15
2021/12/6 Trees 24
Binary Tree Representations
ArrayRepresentation
Linked Representation
2021/12/6 Trees 25
Lemma 5.4
Binary Tree Representations-
Array Representation
If a complete binary tree with n nodes (depth =
log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have:
– parent(i) is at i / 2 if i≠1. If i=1, i is at the root and
has no parent.
– left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no
left child.
– right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.
2021/12/6 Trees 26
[1] A
Array Representation [2] B
[3] C
(1) waste space
[1] [4] D
A (2) insertion/deletion
A [1] [5] E
[2] B problem
[2] [6] F
[3] -- [1]
B [7] G
[4] C
A [8] H
[4] [5] --
C [9] I
[6] -- [2]
[7] -- B
[3]
[8] C
D [8] D
[9] -- [4] [5] [6] [7]
[16] . . F
D E G
E
[16] E
[8] [9]
2021/12/6 H I 27
Properties of Binary Trees
Maximum number of nodes in the levels of a
binary tree For a binary tree
with height h,
maximum number of
nodes =
h
∑ 2
d =0
d
= 2 h +1
−1
maximum number of
internal nodes =
h −1
∑ 2
d =0
d
= 2 h
−1
28
d 2d
Properties of (General) Binary Trees
2021/12/6 Trees 29
29
Properties of Proper Binary Trees
• A binary trees is proper if each
n=e+i
node has either zero or two e=i+1
children. n = 2e − 1
• Level: depth
The root is at level 0 log2 e ≤ h ≤ e – 1
Level d has at most 2d nodes log2 (i + 1) ≤ h ≤ i
• Notation:
n number of nodes 2h+1 ≤ n ≤ 2h+1 - 1
e number of external (leaf) h +1 ≤ e ≤ 2h
nodes
h ≤ i ≤ 2h -1
i number of internal nodes
log2(n + 1) − 1 ≤ h ≤ (n − 1)/2
h height
2021/12/6 Trees 30
Properties of Proper Binary Trees
1 1
2 3
2 3
4 5 6 7 4 5 6 7
8 9 10 11 12 13 14 15
8 9
n=e+i
n = 15 e=i+1 n =9
e =8 n = 2e − 1 e =5
i =7 log2 e ≤ h ≤ e – 1 i =4
h =3 log2 (i + 1) ≤ h ≤ i h =3
2h+1 ≤ n ≤ 2h+1 - 1
h +1 ≤ e ≤ 2h
h ≤ i ≤ 2h -1
log2(n + 1) − 1 ≤ h ≤ (n − 1)/2 31
2021/12/6
Minimum Number Of Nodes
Minimum number of nodes in a binary tree
whose height is h.
At least one node at each of first h levels.
minimum number of
nodes is h +1
2021/12/6 Trees 32
Maximum Number Of Nodes
All possible nodes at first h+1 levels are present.
Maximum number of nodes
= 1 + 2 + 4 + 8 + … + 2h
= 2h+1 - 1
2021/12/6 Trees 33
Number Of Nodes & Height
log2(n+1)-1 ≤ h ≤ n - 1
2021/12/6 Trees 34
Full Binary Tree
Tree height h = 3
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2021/12/6 Trees 36
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2021/12/6 Trees 40
Example
2 3
4 5 6 7
8 9 10 11 12 13 14 15
b 3
7
c
15
d
tree[] a - b - - - c - - - - - - - d
0 5 10 15
data
2021/12/6 Trees 44
A
Linked Representation
B C
A
D E F G
B root
root
H I A
A 0
C
B 0 B C
D C 0
D 0 E 0 0 F 0 G 0
D 0
0 H 0 0 I 0
E 0
E
2021/12/6 (a) Trees 45
(b)
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
2021/12/6 Trees 47
An Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E preorder traversal
+* * /AB CD E
* D prefix expression
postorder traversal
AB/C*D*E+
/ C postfix expression
level order traversal
A B +*E*D/CAB
2021/12/6 Trees 48
Inorder Traversal (recursive version)
void inorder(treePointer ptr)
/* inorder tree traversal */
{
if (ptr) {
inorder(ptr->leftChild);
printf(“%d”, ptr->data);
indorder(ptr->rightChild);
}
}
Time Complexity: O(n)
2021/12/6 Trees 49
Inorder Traversal
A/B*C*D+E
void inorder(treePointer ptr) (1)
/* inorder tree traversal */ + (3)
{ (2)
if (ptr) {
inorder(ptr->leftChild); //(1)
(1)
* (3) (1) E (3)
(2) (2)
printf(“%d”, ptr->data); //(2)
(3)
indorder(ptr->rightChild); //(3) (1) * (3)(1) D
} (2) (2)
} (1) C (3)
(1) / (3)
(2) (2)
(1)
(1) A (3) B (3)
(2) (2)
2021/12/6 Trees 50
Trace Operations of Inorder Traversal
Call of inorder Value in root Action Call of inorder Value in root Action
1 + 11 C
2 * 12 NULL
3 * 11 C printf
4 / 13 NULL
+
5 A 2 * printf
6 NULL 14 D E
5 A printf 15 NULL *
7 NULL 14 D printf
4 / printf 16 NULL
* D
8 B 1 + printf
9 NULL 17 E
8 B printf 18 NULL / C
10 NULL 17 E printf
3 * printf 19 NULL
A B
A/B*C*D+E
2021/12/6 Trees 51
Preorder Traversal (recursive version)
void preorder(treePointer ptr)
/* preorder tree traversal */
{
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->leftChild);
predorder(ptr->rightChild);
}
}
Time Complexity: O(n)
2021/12/6 Trees 52
Preorder Traversal (recursive version)
+**/ABCDE
void preorder(treePointer ptr) (1)
/* preorder tree traversal */ (2) +
(3)
{
if (ptr) { (1) (1) E
printf(“%d”, ptr->data); //(1)
(2) * (2) (3)
(3)
preorder(ptr->leftChild); //(2)
(1) (1) D
predorder(ptr->rightChild);//(3) (2) * (2)
} (3) (3)
}
(1) / (1) C
(2) (2) (3)
(3)
(1) A (1) B
(2) (2)
(3) (3)
2021/12/6 CHAPTER 5 53
Postorder Traversal (recursive version)
void postorder(treePointer ptr)
/* postorder tree traversal */
{
if (ptr) {
postorder(ptr->leftChild);
postdorder(ptr->rightChild);
printf(“%d”, ptr->data);
}
}
Time Complexity: O(n)
2021/12/6 Trees 54
Postorder Traversal (recursive version)
AB/C*D*E+
void postorder(treePointer ptr)
/* postorder tree traversal */
(1) + (3)
{
if (ptr) {
(2)
postorder(ptr->leftChild); //(1) (1)
* (3) (1) E
(3)
postdorder(ptr->rightChild); //(2) (2)
(2)
printf(“%d”, ptr->data); //(3)
}
(1)
* (3) (1) D (3)
} (2) (2)
2021/12/6 CHAPTER 5 56
Iterative Inorder Traversal
(using stack)
void iter_inorder(treePointer node)
{
int top= -1; /* initialize stack */
treePointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->leftChild)
push(node);/* add to stack */
node= pop();
/* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node = node->rightChild;
} Depth First Search (DFS)
} Approach
Time Complexity: O(n)
2021/12/6 Trees 57
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/
5 6
A B
2021/12/6 Trees 58
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/
5 6
A B
push 1
2021/12/6 Trees 59
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/
5 6
A B push 2
1
2021/12/6 Trees 60
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/
push 3
5 6
A B 2
1
2021/12/6 Trees 61
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/
push 4
3
5 6
A B 2
1
2021/12/6 Trees 62
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
4 7 C
/ push 5
4
3
5 6
A B 2
1
2021/12/6 Trees 63
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output:
3 8 D
*
Pop; print 5→data (A)
4 7 C
/ 5
4
3
5 6
A B 2
1
2021/12/6 Trees 64
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A
3 8 D
*
Pop; print 4→data (/)
4 7 C
/ Push 4→rightChild (6)
4
3
5 6
A B 2
1
2021/12/6 Trees 65
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A /
3 8 D
*
4 7 C
/
push 6
3
5 6
A B 2
1
2021/12/6 Trees 66
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A /
3 8 D
*
Pop; print 6→data (B)
4 7 C
/
6
3
5 6
A B 2
1
2021/12/6 Trees 67
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B
3 8 D
*
4 7 C
/ Pop; print 3 → data (*)
Push 3→rightChild (7)
3
5 6
A B 2
1
2021/12/6 Trees 68
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B *
3 8 D
*
4 7 C
/
push 7
5 6
A B 2
1
2021/12/6 Trees 69
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B *
3 8 D
*
4 7 C
/ Pop; print 7→data (C)
7
5 6
A B 2
1
2021/12/6 Trees 70
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C
3 8 D
*
4 7 C
/
Pop; print 2 → data (*)
5 6 Push 2→rightChild (8)
A B 2
1
2021/12/6 Trees 71
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C *
3 8 D
*
4 7 C
/
5 6
A B push 8
1
2021/12/6 Trees 72
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C *
3 8 D
*
4 7 C
/
Pop; print 8→data (D)
5 6
A B 8
1
2021/12/6 Trees 73
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C * D
3 8 D
*
4 7 C
/
2021/12/6 Trees 74
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C * D +
3 8 D
*
4 7 C
/
5 6
A B
push 9
2021/12/6 Trees 75
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C * D +
3 8 D
*
4 7 C
/
2021/12/6 Trees 76
Iterative Inorder Traversal
(using stack)
1
Depth First Search A/B*C*D+E
+
(DFS) Approach
2 9
* E
Output: A / B * C * D + E
3 8 D
*
4 7 C
/
Stack Empty
5 6
A B Stop
2021/12/6 Trees 77
Iterative Preorder Traversal
(Non-recursive Version, Using Stack)
2021/12/6 CHAPTER 5 78
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output:
3 8 D
*
4 7 C
/
5 6
A B
2021/12/6 Trees 79
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output:
3 8 D
*
4 7 C
/ Print 1→data (+)
5 6
A B
push 1
2021/12/6 Trees 80
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: +
3 8 D
*
4 7 C
/ Print 2→data (*)
5 6
A B push 2
1
2021/12/6 Trees 81
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + *
3 8 D
*
4 7 C
/ Print 3→data (*)
push 3
5 6
A B 2
1
2021/12/6 Trees 82
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * *
3 8 D
*
4 7 C
/ Print 4→data (/)
push 4
3
5 6
A B 2
1
2021/12/6 Trees 83
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * /
3 8 D
*
4 7 C
/ push 5 Print 5→data (A)
4
3
5 6
A B 2
1
2021/12/6 Trees 84
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A
3 8 D
*
Pop;
4 7 C
/ 5
4
3
5 6
A B 2
1
2021/12/6 Trees 85
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A
3 8 D
*
Pop;
4 7 C
/ print
4 4→rightChild→data (B);
3 Push 4→rightChild (6);
5 6
A B 2
1
2021/12/6 Trees 86
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B
3 8 D
*
4 7 C
/
push 6
3
5 6
A B 2
1
2021/12/6 Trees 87
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B
3 8 D
*
Pop;
4 7 C
/
6
3
5 6
A B 2
1
2021/12/6 Trees 88
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B
3 8 D
*
4 7 C
/ Pop;
print
3 3→rightChild→data (C);
5 6
A B 2 Push 3→rightChild (7);
1
2021/12/6 Trees 89
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C
3 8 D
*
4 7 C
/
push 7
5 6
A B 2
1
2021/12/6 Trees 90
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C
3 8 D
*
4 7 C
/ Pop;
7
5 6
A B 2
1
2021/12/6 Trees 91
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C
3 8 D
*
4 7 C
/
Pop;
5 print
6
A B 2 2→rightChild→data (D);
1 Push 2→rightChild (8);
2021/12/6 Trees 92
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D
3 8 D
*
4 7 C
/
5 6
A B push 8
1
2021/12/6 Trees 93
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D
3 8 D
*
4 7 C
/
Pop;
5 6
A B 8
1
2021/12/6 Trees 94
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D
3 8 D
*
4 7 C
/
5 Pop;
6
A B Print
1 1→rightChild→data (E);
Push 1 → rightChild (9);
2021/12/6 Trees 95
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D E
3 8 D
*
4 7 C
/
5 6
A B
push 9
2021/12/6 Trees 96
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D E
3 8 D
*
4 7 C
/
5 Pop;
6
A B
9
2021/12/6 Trees 97
Iterative Preorder Traversal
(using stack)
1
Depth First Search
+
(DFS) Approach
+**/ ABCDE
2 9
* E
Output: + * * / A B C D E
3 8 D
*
4 7 C
/
Stack Empty
5 6
A B Stop
2021/12/6 Trees 98
Level Order Traversal
(Using Queue)
2021/12/6 CHAPTER 5 99
Level Order Traversal
(using queue)
1
+ +*E*D/ CAB
2 9
* E Output:
3 8 D
*
4 7 C
/
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output:
3 8 D front
*
4 7 C Enqueue (1)
/
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output:
rear
3 8 D front
*
4 7 C 1
/
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output:
rear
3 8 D front
*
2
4 7 C Dequeue 1 Enqueue (1 → leftChild )
/
Enqueue (1→ rightChild)
Print 1→Data (+)
9
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: +
3 8 D front rear
*
3
4 7 C Dequeue 2 9 Enqueue (2 → leftChild )
/
Enqueue (2→ rightChild)
Print 2→Data (*)
8
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + *
3 8 D front rear
*
4 7 C Dequeue 9 3 8
/
Print 9→Data (E)
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E
3 8 D front rear
*
4
4 7 C Dequeue 3 8 Enqueue (3 → leftChild )
/
Enqueue (3→ rightChild)
Print 3→Data (*)
7
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E *
3 8 D front rear
*
4 7 C Dequeue 8 4 7
/
Print 8→Data (D)
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E * D
3 8 D front rear
*
5
4 7 C Dequeue 4 7 Enqueue (4 → leftChild )
/
Enqueue (4→ rightChild)
Print 4→Data (/)
6
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E * D /
3 8 D front rear
*
4 7 C Dequeue 7 5 6
/
Print 7→Data (C)
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E * D / C
3 8 D front rear
*
4 7 C Dequeue 5 6
/
Print 5→Data (A)
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E * D / C A
rear
3 8 D front
*
4 7 C Dequeue 6
/
Print 6→Data (B)
5 6
A B
1
+ +*E*D/ CAB
2 9
* E Output: + * E * D / C A B
rear
3 8 D front
*
4 7 C
/
Heaps
Selection Trees
Forests
(t,t,t) ∨
(t,t,f)
(t,f,t) ∨ ¬
(t,f,f)
(f,t,t) ∧ ∧ X3
(f,t,f)
(f,f,t) X1 ¬ ¬ X3
(f,f,f)
2n possible combinations X2 X1
for n variables
postorder traversal (postfix evaluation)
2021/12/6 Trees 121
node structure
v ¬
^ ^ x3
x1 ¬ ¬ x3
x2 x1
2021/12/6 124
switch(node->data) {
case not: node->value =
!node->rightChild->value;
break;
case and: node->value = v
node->rightChild->value && v ¬
node->leftChild->value;
break; ^ ^ x3
node->leftChild->value;
break; 2x x
1
B C
D E F
G H
t t.SwapTree()
A swap tree example
reverse
Inorder: DGBEAFHC Inorder: CHFAEBGD
Heaps
Selection Trees
Forests
If ptr->rightChild is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
dangling
B C
D E F G
dangling
inorder traversal:
H I H, D, I, B, E, A, F, C, G
TRUE FALSE
TRUE FALSE
f=false; t=true
threadedPointer temp;
temp = tree->rightChild;
if (!tree->rightThread)
while (!temp->leftThread)
temp = temp->leftChild;
return temp;
}
2021/12/6 Trees 135
Inorder Traversal of Threaded BTs
void tinorder(threadedPointer tree)
{
/* traverse the threaded binary tree inorder */
threadedPointer temp = tree;
for (;;) {
temp = insucc(temp);
if (temp==tree) break;
printf(“%3c”, temp->data);
}
}
Time Complexity: O(n)
where n is the number of nodes in the threaded binary
tree
2021/12/6 Trees 136
Inserting Nodes into Threaded BTs
Insert child as the right child of node parent
– change parent->right_thread to FALSE
– set child->left_thread and child->right_thread
to TRUE
– set child->left_child to point to parent
– set child->right_child to parent->right_child
– change parent->right_child to point to child
root root
A A
parent (1)
B parent B
(3)
child child
C D C D
(2)
Right subtree of B is empty.
case (a)
2021/12/6 Trees 138
Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree
(3)
(2) (1)
(4)
case (b)
2021/12/6 Trees 139
Right Insertion in Threaded BTs
void insertRight(threadedPointer parent,
threadedPointer child)
{ /* insert child as the right child of
parent */
threadedPointer temp; case (a)
(1) child->rightChild = parent->rightChild;
child->rightThread = parent->rightThread;
(2) child->leftChild = parent;
child->leftThread = TRUE;
(3) parent->rightChild = child;
parent->rightThread = FALSE;
if (!child->rightThread) {
temp = insucc(child); case (b)
(4) temp->leftChild = child;
}
}
2021/12/6 Trees 140
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
machine services
– amount of time (min heap)
– amount of payment (max heap)
factory
– time tag
sorted array
heap
Property:
The root of max heap (min heap) contains
the largest (smallest).
2021/12/6 Trees 148
A Max Heap
20
[1] [2] [3] [4] [5] [6]
12 7
20 12 7 10 5 6
10 5 6
A Min Heap
5
[1] [2] [3] [4] [5] [6]
12 40
5 12 40 25 50 65
25 50 65
2021/12/6 Trees 149
Min Heaps
20 20 21
15 2 15 5 15 20
14 10 14 10 2 14 10 2
initial location of new node insert 5 into heap insert 21 into heap
(see the next page)
20
15 2
14 10
(a) (b)
Insert 5 Insert 21
20 21
15 5 15 20
14 10 2 14 10 2
(c) (d)
4 24-1 = 8
•
• •
• •
•
• • • i 2i-1
•
• •
• •
• 2k-1
• • • k
Depth k
Total number of nodes of a tree with depth k
k
=
k
0
2 −2 k n = 2 −1
∑2 i −1 0 1
= 2 + 2 + ⋅⋅⋅ + 2 k −1
=
1− 2
= 2k − 1
2 k
= n +1
i =1 ⇒
=n ⇒ k = log 2 (n157+ 1)
The relation between the number (n) of LEVEL Number of nodes
a full binary tree nodes and the
1 21-1 = 1
tree depth (k)
2 22-1 = 2
3 23-1 = 4
4 24-1 = 8
•
• •
• •
•
• • • i 2i-1
•
•
• n = 2k − 1
• •
• 2k-1 = 2L-1
• • • k
Number of
Total number of internal nodes of a tree with depth k
0 leaf nodes (L)
k −1
2 − 2k −1
= ∑ 2
i =1
i −1
= 2 0
+ 21
+ ⋅ ⋅ ⋅ + 2 k −2
=
1− 2
= 2 k −1 − 1
= 2k-1
= L -1 158
Inserting 15, 42, 29, 66, 73, 15*, 10, 19 into a max heap
one by one.
1. (15) 4.(66) 42 5.(73) 66
15
15 29 42 29
2. (42) 15
66 15 73
42
66
42 42
73 29
15 66 29
15 42
15
3.(29) 42
73
15 29
66
66 29
42 29
15 42
2021/12/6 159
15
Inserting 15, 42, 29, 66, 73, 15*, 10, 19 into a max heap
one by one.
6.(15) 8.(19) 73
73
66 29
66 29
15 42 15* 10
15 42 15*
19
73
7.(10)
73 66 29
66 29 19 42 15* 10
15
15 42 15* 10
remove
20 10 15
15 2 15 2 14 2
14 10 14 10
[6]
child=4
[4] [5]
temp=heap[6]=6 parent=1
temp=6
child=2
parent=4
child=8
17 14 10 18
14 10 18 14 10
10 14 17 18
10 14 10 17 10
14
Insertions
[1] 26
[2] 5 [3] 77
[2] 61 [3] 59
exchange
[4] 48 [5] 19 [6] 11 [7] 26
[2] 48 [3] 59
[1] 59
[2] 48 [3] 26
(b)
[2] 19 [3] 26
[1] 26
[2] 19 [3] 11
(d)
[2] 15 [3] 11
[2] 5 [3] 11
(f)
[2] 5 [3] 1
(g)
[1] 5
[2] 1 [3] 11
[2] 5 [3] 11
(i)
Array index: 1 2 3 4 5 6 7 8 9 10
1 5 11 15 19 26 48 59 61 77
[1] 1 Sorted sequence
[2] 5 [3] 11
(j)
2021/12/6 In-place Heap Sort 180
Example of Bottom-up Max Heap
Construction
2 3
4 5 6 7
8 9 7
10 811
2 3
4 5 6 7
8 9 7
10 811
2 3
4 11 6 7
8 9 7
10 85
2 3
4 11 6 7
8 9 7
10 85
2 3
9 11 6 7
8 4 7
10 85
2 3
9 11 6 7
8 4 7
10 85
2 7
9 11 6 3
8 4 7
10 85
2 7
9 11 6 3
8 4 7
10 85
11 7
9 6 3
8 4 7
10 85
11 7
9 10 6 3
8 4 8 75
11 7
9 10 6 3
8 4 72 58
11 7
9 10 6 3
8 4 72 58
9 10 6 3
8 4 72 85
10 7
9 6 3
8 4 72 85
10 7
9 5 6 3
8 4 72 8
10 7
9 5 6 3
8 4 72 8
1
Done.
2021/12/6 Heaps 197
Analysis
We visualize the worst-case time of a downheap with a proxy path that
goes first right and then repeatedly goes left until the bottom of the heap
(this path may differ from the actual downheap path)
Since each node is traversed by at most two proxy paths, the total
number of nodes of the proxy paths is O(n)
Thus, bottom-up heap construction runs in O(n) time
Bottom-up heap construction is faster than n successive insertions and
speeds up the first phase of heap-sort
h 1
height
0
T= ∑ ( h
d = h −1
− d ) ⋅ 2 d
= 1 ⋅ 2 h −1
+ 2 ⋅ 2 h−2
+ 3 ⋅ 2 h −3
+ ⋅ ⋅ ⋅ + h ⋅ 2 0
∴ T = n − 1 − h Heaps 200
Vector-based Heap Implementation
We can represent a heap with n keys
by means of a vector of length n + 1 2
For the node at rank i
5 6
– the left child is at rank 2i
– the right child is at rank 2i + 1 9 7
Links between nodes are not
explicitly stored
The cell of at rank 0 is not used
Operation insert corresponds to
inserting at rank n + 1 2 5 6 9 7
Operation removeMin corresponds to 0 1 2 3 4 5
removing at rank n
Yields in-place heap-sort
16 15 4 12 6 7 23 29
25 5 11 27
16 15 4 12 6 7 23 29
25 5 11 27
16 15 4 12 6 9 23 29
15 4 6 23
16 25 5 12 11 9 27 29
7 8
15 4 6 23
16 25 5 12 11 9 27 29
4 6
15 5 8 23
16 25 7 12 11 9 27 29
4 6
15 5 8 23
16 25 7 12 11 9 27 29
5 6
15 7 8 23
16 25 10 12 11 9 27 29
Heaps
Selection Trees
Forests
20 30 60
12 25 5 40 70
10 15 22 2 65 80
15
• Search for key 13:
6 18 – 15 → 6 → 7 → 13
3 7 17 20
2 4 13
9
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->leftChild;
else tree = tree->rightChild;
} Time Complexity: O(h)
return NULL; where h is the height of the binary tree.
} 2021/12/6 Trees 215
Binary Search Trees: Successor
Def: successor (x) = y, such that key [y] is the
smallest key > key [x]
E.g.: successor (15) = 17 15
successor (13) = 15 6 18
successor (9) = 13 3 7 17 y 20
x
2 4 13
• Case 1: right (x) is non empty 9
– successor (x) = the minimum in right (x)
• Case 2: right (x) is empty
– go up the tree until the current node is a left child:
successor (x) is the parent of the current node
– if you cannot go further (and you reached the root):
x is the largest element 216
Binary Search Trees: Predecessor
Def: predecessor (x) = y, such that key [y] is the
biggest key < key [x]
E.g.: predecessor (15) = 13 y
15
predecessor (9) = 7 6 x 18
predecessor (7) = 6
3 7 17 20
2 4 13
• Case 1: left (x) is non empty 9
– predecessor (x) = the maximum in left (x)
• Case 2: left (x) is empty
– go up the tree until the current node is a right child:
predecessor (x) is the parent of the current node
– if you cannot go further (and you reached the root):
x is the smallest element 217
Binary Search Trees: Insertion
• Goal:
– Insert value v into a binary search tree
• Idea:
– If key [x] < v move to the right child of x, Insert value 13
else move to the left child of x
12
– When x is NIL, we found the correct position
– If v < key [y] insert the new node as y’s left child 5 18
else insert it as y’s right child 2 9 15 19
1 3 13 17
– Beginning at the root, go down the tree and maintain:
• Pointer x : traces the downward path (current node)
• Pointer y : parent of x (“trailing pointer” )
12 12
x y
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 13 17
x = NIL
y = 15
2021/12/6 Trees 219
Inserting Nodes into a Binary Search
Tree
30 30 30
5 40 5 40 5 40
2 2 80 2 35 80
Insert 80 Insert 35
5 16 5 16
3 12 20 3 12 20
z
10 13 18 23 10 18 23
6 delete 6
15 delete 15
z
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
3 12 20 3 12 20
10 13 18 23 10 13 18 23
y 6 7
30
30
30
5 40
5 40
5 40
80 2
35 80 2
2
Delete 35 Delete 80
1
1
Delete 2
2
T1 T2
T1 X
T2
2021/12/6 Trees 229
Deletion from a Binary Search Tree
Example (Case 2)
10 30 50 70 10 30 50 70
45 55 45 52
52
After deleting 60
Before deleting 60
2021/12/6 Trees 231
Deletion from a Binary Search Tree
Case 3: deletion of a nonleaf node with two children
1 1
X Delete 2
2 2’
T1 T1
T2 T3 T2’ T3
2’
50 95
30 68 90 97
45 56 73 88 94
23
55 64 71 77 92
mid
small’ big
Time Complexity:
O(min{height(small),height(big)})
77
small big
50 95
30 68 90 97
23 45 56 73 88 94
55 64 71 77 80 92
80
small big
50 95
30 68 90 97
23 45 56 73 88 94
55 64 71 77 80 92
• Inverse of join.
• Obtain
S … dictionary of pairs with key < k.
B … dictionary of pairs with key > k.
m … pair with key = k (if present).
A S B
a B
C b
c D
E d
e m
f g
S B
B A
C b a
c D
E d
e m
f g
S B
A B
C a b
c D
E d
e m
f g
S B
A B
a C b
D
c
E d
e m
f g
S B
A B
a C D b
c d
E
e m
f g
S B
A B
a C D b
c E d
e
m
f g
S B
A B
a C D b
c E g d
e f
m
30 95 30 68 90 97
73 88 94
23 45 68 97 23 45 56
56 90 92
55 64 71 77
55 64 73 94
71 80 92
77 88
30 95 30 68 90 97
73 88 94
23 45 68 97 23 45 56
56 90 92
55 64 71 77
55 64 73 94
71 80 92
77 88
30 95 30 68 90 97
73 88 94
23 45 68 97 23 45 56
56 90 92
55 64 71 80
55 64 73 94
77
71 80 92
77 88
Key 85 is not in the split binary search tree.
2021/12/6 Trees 250
85
Split(85) small k big
85
50 50 95
30 95 30 68 90 97
73 88 94
23 45 68 97 23 45 56
56 90 92
55 64 71 80
55 64 73 94
77
71 80 92
77 88
Key 85 is not in the split binary search tree.
2021/12/6 Trees 251
85
Split(50) small mid big
50
50 30 95
30 95
23 45 68 97
23 45 68 97 56 90
56 90 55 64 73 94
55 64 73 94
71 80 92
71 80 92
77 88
77 88
30 95
23 45 68 97
23 45 68 97 56 90
56 90 55 64 73 94
55 64 73 94
71 80 92
71 80 92
77 88
77 88
Heaps
Selection Trees
Forests
6 2 8 3
4 5 6 7
9 6 8 17
8 9 10 11 12 13 14 15
Each leaf represents the first
10 9 20 6 8 9 90 17
record in the corresponding
15 20 20 15 15 11 95 18
run.
16 38 30 25 50 16 99 20
28
run run run run run run run run
1 2 3 4 5 6 7 8
4 5 6 7
9 6 8 17
8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
18
ordered sequence
15 20 20 15 15 11 100
16 38 30 25 50 16 110 20
6 8 9 8
2 3
output 6 2 3
4 5 6 7 4 5 6 7
9 6 8 17 9 15 8 17
8 9 10 15 8 9 10 15
11 12 13 14 11 12 13 14
10 9 20 6 8 9 90 17 10 9 20 15 8 9 90 17
15 20 20 15 15 11 95 18 15 20 20 25 15 11 95 18
16 38 30 25 50 16 99 20 16 38 30 28 50 16 99 20
28
run run run run run run run run run run run run run run run run
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
986 1
10
96 2 98 3
4 5 6 7
10
9 15
6 98 17
8 9 10 11 12 13 14 15
9 20 15
10 20 6 15
8 9 90 17
15
15 20
38
10 20 20 15
9 20 25 50
6 15 11
8 11
15 95
9 95 18
90 18
17
16
16 38
15 38 30
20 30 28
20 25 50
15 50 16
15 16 99
11 99 20
95 20
18
16 38 30 28
25 50 16 99 20
run run run run run run run run
run run run 28
run run run run run
11 22 33 44 55 66 77 88
run run run run run run run run
1 2 3 4 5 6 7 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorting.
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorted array.
Winner Trees 274
Sort 16 Numbers
1
1
2
3 1 2 2
3 6 5 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorted array.
Winner Trees 275
Sort 16 Numbers
1
1
2
3 3 2 2
3 6 5 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorted array.
Winner Trees 276
Sort 16 Numbers
1
3
2
3 3 2 2
3 6 5 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorted array.
Winner Trees 277
Sort 16 Numbers
2
3
2
3 3 2 2
3 6 5 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sorted array.
Winner Trees 278
Sort 16 Numbers
2
3
2
3 3 2 2
3 6 5 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2
Sorted array.
Winner Trees 279
Sort 16 Numbers
2
3
2
3 3 2 2
3 6 5 3 6 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2
Sorted array.
Winner Trees 280
Sort 16 Numbers
2
3
2
3 3 4 2
3 6 5 3 6 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2
Sorted array.
Winner Trees 281
Sort 16 Numbers
2
3
2
3 3 4 2
3 6 5 3 6 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2
Sorted array.
Winner Trees 282
Sort 16 Numbers
2
3
2
3 3 4 2
3 6 5 3 6 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2
Sorted array.
Winner Trees 283
Sort 16 Numbers
2
3
2
3 3 4 2
3 6 5 3 6 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2
Sorted array.
Winner Trees 284
Sort 16 Numbers
2
3
2
3 3 4 2
3 6 5 3 6 4 5 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2
Sorted array.
Winner Trees 285
Sort 16 Numbers
2
3
2
3 3 4 5
3 6 5 3 6 4 5 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2
Sorted array.
Winner Trees 286
Sort 16 Numbers
2
3
4
3 3 4 5
3 6 5 3 6 4 5 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2
Sorted array.
Winner Trees 287
Sort 16 Numbers
3
3
4
3 3 4 5
3 6 5 3 6 4 5 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2
Sorted array.
Winner Trees 288
Sort 16 Numbers
3
3
4
3 3 4 5
3 6 5 3 6 4 5 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1 2 2 3
Sorted array.
Winner Trees 289
Time To Sort
• Initialize
O(n) time
• Get winner
O(1) time
• Remove/replace winner and replay
O(log n) time
more precisely Theta(log n)
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8
1
2
3 1 2 2
3 6 1 3 2 4 2 5
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8
2
9 3
9 15 17
4 5 6 7
10 20 15 9 90
8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
run 1 run 2 run 3 run 4 run 5 run 6 run 7 run 8
2021/12/6 15Loser Trees15 297
6 0
6 1
8 1
6 8 9 2 17 3
2 3
4 5 6 7 4 5 6 7
9 6 8 17 10 20 9 90
8 9 10 15
8 9 10 11 12 13 14 15 11 12 13 14
10 9 20 6 8 9 90 17 10 9 20 6 8 9 90 17
8 0
15 2 17 3
4 5 6 7
output 6
10 20 9 90
8 9 10 15
11 12 13 14
10 9 20 15 8 9 90 17
15 20 20 25 15 11 95 18
16 38 30 28 50 16 99 20
2021/12/6 run run run run run run run run 298
1 2 3 4 5 6 7 8
6 8 9
986 0
10
98 1
20
9 2 17 3
4 5 6 7
10
20 20
25 15
9 90
8 9 10 15
11 12 13 14
9 20 25
10 20 6 15
8 9 90 17
10
15 9
20
38 20 6
25
28 8
15
50 9
11 90
95 17
18
16
15 38
20 30
20 28
25 50
15 16
11 99
95 20
18
16 38 30 28 50 16 99 20
4 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
6 1
4 8 5 7
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
6 3 2
4 8 5 7 6 9
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
3
2
6 3 4 2
4 8 5 7 6 9 5 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
3
2
6 3 4 5
4 8 5 7 6 9 5 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
3
2
6 3 4 5
4 8 5 7 6 9 5 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
3
2
6 3 4 5
4 8 5 7 6 9 5 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
3
2
6 3 4 5
4 8 5 7 6 9 5 8
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
32
3
2
6 53 4 5
4 8 95 7 6 9 5 8
4 3 6 8 91 5 7 3 2 6 9 4 5 2 5 8
Heaps
Selection Trees
Forests
A E G
D F H I
B C
Three-Tree Forest
F H I C F G
B C D
D H
(1)
I
A
E (1) A (2)
G (2) E (1)
B C D
F B C D G
H I F
H I
Forest Traversals
Preorder
– If F is empty, then return
– Visit the root of the first tree of F
– Taverse the subtrees of the first tree in forest preorder
– Traverse the remaining trees of F in forest preorder
Inorder
– If F is empty, then return
– Traverse the subtrees of the first tree in forest inorder
– Visit the root of the first tree
– Traverse the remaining trees in forest indorer
A A
B B C D
E C E F J
G H I
F G D
B C D
preorder
H
E G H
I F I
J
J
2021/12/6 Trees 319
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
6 7 8 1 9 3 5
Si ∩ Sj = φ
Two operations considered here
– Disjoint set union S1 ∪ S2={0,6,7,8,1,4,9}
– Find(i): Find the set containing the element i.
3 ∈ S3, 8 ∈ S1
2021/12/6 Trees 322
Disjoint Set Union
Make one of trees a subtree of the other
0 4
6 7 8 4 or 0 1 9
1 9 6 7 8
S1 ∪ S2
S1 ∪ S2
Possible representation for S1 ∪ S2
0
set pointer
name
6 7 8
S1
S2 4
S3 1 9
3 5
6 7 8 1 9 3 5
S1 S2 S3
int simpleFind(int i)
{ /* following the parent values starting at
i and continuing until reaching a negative
parent value */
for (; parent[i]>=0; i=parent[i]);
return i;
}
6 7 8 1 9 3 5
S1 S2 S3
Array Representation for Sets
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
6 7 8 1 9 3 5
S1 S2 S3
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
simpleUnion(0,4)
4 2
S1 ∪ S2
0 1 9 3 5
S3
6 7 8
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent 4 4 -1 2 -1 2 0 0 0 4
2021/12/6 Trees 329
0 4 2
6 7 8 1 9 3 5
S1 S2 S3
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
simpleUnion(4,0)
S1 ∪ S2 0 2
6 7 8 4 3 5
S3
1 9
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 0 2 0 0 0 4
2021/12/6 Trees 330
0 4 2
6 7 8 1 9 3 5
S1 S2 S3
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
simpleUnion(simpleFind(1),simpleFind(7)) simpleUnion(4,0)
S1 ∪ S2 0 2
6 7 8 4 3 5
S3
1 9
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 0 2 0 0 0 4
2021/12/6 Trees 331
SimpleUnion/Find
Worst Case
5 5 5 5 5 5
4 Union (0,1); 4 Union (1,2); 4 Union (2,3); 4 Union (3,4); 4 Union (4,5); 4
Find (0) Find (0) Find (0) Find (0) Find (0)
3 3 3 3 3 3
2 2 2 2 2 2
1 1 1 1 1 1
0 0 0 0 0 0
degenerate tree
2021/12/6 Trees 333
Disjoint Sets
Two Operations
– Union: weightedUnion Good
– Find(i): collapsingFind(i)
Example
-6 -3 -9
i j i
∪
j
-9
i
2021/12/6 336
Weighting rule for union(i, j)
Definition: Weighting rule for union(i, j). If the number
of nodes in tree i is less than the number in tree j then
make j the parent of i; otherwise make i the parent of j.
-1 -1 -1 -2 -1 -1 -3 -1 -1
0 1 ••• n-1 0 2 ••• n-1 0 3 ••• n-1
union(0,1)
union(0,2)
initial 1 1 2
起始狀況 .
Union (0,1) Union (0,2)
.
-4 -1 -1 -n .
0 4 ••• n-1 0 union(0,n-1)
• • •
1 2 3 1 2 3 ••• n-1
1 2 3 6
4 5 7 8 338
Union Operation Using Weighting Rule
void weightedUnion(int i, int j)
{ Keep a count (a negative value) in the root of tree
/* union the sets with roots i and j, i!=j,
using the weighting rule.
parent[i]=-count[i] and parent[j]=-count[j]
*/
int temp = parent[i]+parent[j];
if (parent[i]>parent[j]) { i has fewer nodes.
parent[i]=j;
parent[j]=temp;
}
else { j has fewer nodes If the number of nodes in tree i is
parent[j]=i; less than the number in tree j, then
parent[i]=temp; make j the parent of i; otherwise
} make i the parent of j.
}
2021/12/6 Trees 339
[-1]
0
[-1]
1
[-1]
2
[-1]
3
[-1]
4
[-1]
5
[-1]
6
[-1]
7 Weighted Union
(a) 一開始樹的高度都是1
(a) Initial height-1 trees Example 5.3: Consider the
[-2] [-2] [-2] [-2] behavior of weightedUnion on the
0 2 4 6
following sequence of unions
starting from the initial
1 3 5 7
(b)(b) Height-2
執行Union trees following
(0,1),(2,3),(4,5),與 (6,7) 後樹之高度為 2
Union (0,1), (2,3), (4,5), and (6,7)
[-4] [-4]
configuration of
0 4 parent[i]=-count[i] = -1,
1 2 5 6 0≤i<n=23:
3 7 union(0,1) union(2,3) union(4,5) union(6,7)
(c) Height-3 trees following
(c) 執行Union (0,2) 與 (4,6) 後樹之高度為 3
Union (0,2) and (4,6) union(0,2) union(4,6) union(0,4)
[-8]
0 Lemma 5.5
1 2 4 In general case, the maximum level
3 5 6 can be log2 m + 1 if the tree has m
7
nodes.
(d) Height-4 trees (0,4)
(d) 執行Union following Union
後樹之高度為 4 (0,4) Trees 340
Lemma 5.5: Let T be a tree with n nodes
created as a result of weightedUnion. No
node in tree T has level greater than
log 2 n + 1 .
Proof: please refer to our textbook.
l Collapsing Rule
l k j i
k
i Trees 343
collapsingFind(i)
root root
-5 m
-5 m
collapsingFind(i) = m
l
l k j i
k
7
-8
0 i [0] [1] [2] [3] [4] [5] [6] [7]
parent -8 0 0 2 0 4 0 0
1 2 4 6 7
3 5 Trees 346
Example 5.4 -8 -8
0 0
1 2 4 1 2 4 6 7
3 5 6 3 5
simpleFind(7)=0 collapsingFind(7)=0
8 find(7) operations 7 The right tree is the
on the left tree resulting tree after 8 find(7)
operations on the left tree.
find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7)
go up 3 1 1 1 1 1 1 1
reset 2
12 moves vs. 24 (= 3 x 8) moves
collapsingFind simpleFind
2021/12/6 Trees 347
α
classes:
4 7 10 8 1 5 11
{0, 2, 4, 7, 11},
9
{1, 3, 5},
(c) Trees following
(c) 處理完 7 ≡7 ≡ 8,4,
4, 6≡ 3 ≡ 5,6
2 ≡≡ 8, 3 ≡ 5, and 2 ≡ 11
11 後的樹
4 7 2 10 8 1 5
11 9
B, C D, E, F, G, H, I B D
A
C E F
B D, E, F, G, H, I G I
D E GFH
A
B C
D E F
inorder: 16 inorder:18
postorder: 16 postorder: 18
8 3
inorder: 8 inorder: 3
postorder: 8 postorder: 3
inorder: 16 inorder:18
postorder: 16 postorder: 18
8 3
inorder: 8 inorder: 3
postorder: 8 postorder: 3
A A A
A
D B D
BC EDGHFI B EDGHFI B
C C E GHFI C E F
GH I
A
B D
C E F
G I
Trees 360
2021/12/6
H
Outline
Introduction
Binary Trees
Heaps
Selection Trees
Forests
n 1 2 3 4 5 6 7
bn 1 1 2 5 14 42 132
bi bn-i-1
n 0 1 2 3 4 5 6
bn 1 1 2 5 14 42 132
1 2n
The number is .
n +1 n
2021/12/6 Trees 369
Number of distinct binary trees
Solving the recurrence of the equation
n −1
bn = ∑ bi bn −i −1 , n ≥ 1, and b0 = 1
i =0
B ( x) = ∑ bi x = b0 + b1 x + b2 x + b3 x + ⋅ ⋅ ⋅
i 2 3
i≥o
i≥o
where: b1 = b0b0
b2 = b0b1 + b1b0
b3 = b0b2 + b1b1 + b2b0
b4 = b0b3 + b1b2 + b2b1 + b3b0
⋅
⋅
⋅
n −1
bn = ∑ bi bn −i −1 , n ≥ 1, and b0 = 1
i =0
2021/12/6 Trees 371
Number of distinct binary trees
B 2 ( x ) = b0b0 + (b0b1 + b1b0 ) x + (b0b2 + b1b1 + b2b0 ) x 2
3
+ (b0b3 + b1b2 + b2b1 + b3b 0 ) x
4
+ (b0b4 + b1b3 + b2b2 + b3b1 +b4b0 ) x + ⋅ ⋅ ⋅
= b1 + b2 x1 + b3 x 2 + b4 x 3 + b5 x 4 + ⋅ ⋅ ⋅
2
Then, we have xB ( x) = B( x) − 1 (since B(0)= b0 = 1)
1 − 1 − 4x
Solving the quadratics, we get B ( x ) =
2x
i i
⇒ B ( x) = ∑ ∑ b j bi − j x = ∑ bi +1 x i
2
i ≥0 j =0 i ≥0
⇒ xB 2 ( x) = B ( x) − 1
1− 1− 4x
⇒ B( x) = because b0 = 1.
2x
1 1 / 2 1/ 2
⇒ B ( x) = 1 − ∑ (−4 x) = ∑
n
(−1) m 2 2 m +1 x m
2 x n≥0 n m≥0 m + 1
1/ 2 n 2 n +1 1 2n
⇒ bn = (−1) 2 =
n + 1 n +1 n
2021/12/6 Trees 373
Derivation of the number of distinct
binary trees (bn)
obtain the Taylor expansion of 1 − 4 x
First,
1 − 1 − 4x
B( x) =
2x
(= ∑b x
n≥o
n
n
)
Finally, simplify bn
2 2 2 2
⋅
⋅
Trees 375
⋅
Derivation of bn
f (0) = 1
1
f ' (0) = (−4)
2
1 1
f " (0) = − 1(−4) 2
2 2
1 1 1
f ' ' ' (0) = − 1 − 2 (−4)3
2 2 2
1 1 1 1
f ( x ) = − 1 − 2 − 3 (−4) 4
( 4)
2 2 2 2
⋅
⋅
⋅
2021/12/6 Trees 376
Derivation of bn
By Taylor series,
( 4)
f ' ( 0 ) f " ( 0 ) f ' ' ' ( 0 ) f (0) 4
f ( x) = (1 − 4 x)1/ 2 = f (0) + x+ x2 + x3 + x + ⋅⋅⋅
1! 2! 3! 4!
1 1 1 2 1 1 1 3
( −4) − 1( −4) − 1 − 2 ( −4)
2 2 2 2 2 2 2
= 1+ x+ x + x3
1! 2! 3!
1 1 1 1 4
− 1 − 2 − 3 (−4)
2 2 2 2
+ x4 + ⋅⋅⋅
4!
1 1 1 1 n
− 1 − 2 ⋅
⋅ ⋅ − ( n − 1) ( − 4 )
2 2 2 2
= 1 + ∑ xn
n ≥1 n!
1 / 2
= 1 + ∑ (−4) n x n
n ≥1 n
Trees 377
Derivation of bn
Then, we simplify the generating function B(x) in compact
form.
1 − 1 1 / 2
Generating function B ( x) = [1 − f ( x)] = ∑ (−4)n x n
2x 2 x n ≥1 n
1 / 2 n − 1 n −1
= ∑ (−4) x
n ≥1 n 2
Let m = n − 1.
1/ 2 m +1 − 1 m
⇒ B( x) = ∑ (−4) x
m ≥ 0 m + 1 2
Summary
1 2n
= bn =
n +1 n