Chapter5 Trees

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 381

CHAPTER 5

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 2
Trees
Root
Dusty

Honey Bear Brandy

Brunhilde Terry Coyote Nugget

Gill Tansey Tweed Zoe Crocus Primrose Nous Belle

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

 Representation as a Degree-Two Tree


(Binary Trees)

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

data child 1child 2 ... child k

How many link fields are


needed in such
階層a representation?

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.

Proof: Since each non-zero child field points to a


node and there is exactly one pointer to each node
other than the root, the number of non-zero fields
in an n-node tree is exactly n-1. The total number
of child fields in a k-ary tree with n nodes is nk.
Hence, the number of zere fields is nk-(n-1) = n(k-
1)+1. □

data child 1child 2 ... child k

Figure 5.4: Possible node structure for a tree of degree k

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 14
Binary Trees
 A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
 Any tree can be transformed into binary tree.
– by left child-right sibling representation
 The left subtree and the right subtree are
distinguished.
2021/12/6 Trees 15
Figure 5.7: Left child-right child tree representation of a tree
Level
階層

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

C Skewed Binary Tree


3 D E F G

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

• Level: depth h+1 ≤ n ≤ 2h+1 – 1


The root is at level 0
Level d has at most 2d nodes 1 ≤ e ≤ 2h
• Notation:
n number of nodes h ≤ i ≤ 2h – 1
e number of external (leaf)
nodes
i number of internal nodes log2(n + 1) − 1 ≤ h ≤ n − 1
h height

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

 Let n be the number of nodes in a binary


tree whose height is h.
 h+1 ≤ n ≤ 2h+1 – 1

 log2(n+1)-1 ≤ h ≤ n - 1

2021/12/6 Trees 34
Full Binary Tree

 A full binary tree of a given height h has


2h+1 – 1 nodes.

Height 3 full binary tree.


2021/12/6 Trees 35
Numbering Nodes In A Full Binary
Tree
 Number the nodes 1 through 2h+1 – 1.
 Number by levels from top to bottom.

 Within a level number from left to right.


1

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

 Parentof node i is node i / 2, unless i = 1.


 Node 1 is the root and has no parent.
2021/12/6 Trees 37
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Left child of node i is node 2i, unless 2i > n,


where n is the number of nodes.
 If 2i > n, node i has no left child.
2021/12/6 Trees 38
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Right child of node i is node 2i+1, unless 2i+1


> n, where n is the number of nodes.
 If 2i+1 > n, node i has no right child.
2021/12/6 Trees 39
Complete Binary Tree With n Nodes

 Startwith a full binary tree that has at least


n nodes.
 Number the nodes as described earlier.

 The binary tree defined by the nodes


numbered 1 through n is the unique n node
complete binary tree.

2021/12/6 Trees 40
Example

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Complete binary tree with 10 nodes.


2021/12/6 Trees 41
Space Analysis of Array Implementation

 n: number of nodes of binary tree T


 PM : index of the rightmost leaf of the corresponding
full binary tree (or size of the full tree)
 N: size of the array needed for storing T; N = pM + 1

Best-case scenario: balanced, full binary tree pM = n


1
1
Worst case scenario: unbalanced tree 3
2
 Height h = n - 1 7
 Size of the corresponding full tree: pM = 2h+1 – 1= 2n– 1
3
15
 N = 2n 4
31
5
Space usage: O(2n)

2n-1
n
2021/12/6 Trees 42
Right-Skewed Binary Tree
(worst case scenario)
1
a

b 3
7
c
15
d

tree[] a - b - - - c - - - - - - - d
0 5 10 15

 Ann node binary tree needs an array whose


length is between n+1 and 2n.
2021/12/6 Trees 43
Linked Representation
typedef struct node *treePointer;
typedef struct node {
int data;
treePointer leftChild, rightChild;
};

data

leftChild data rightChild


leftChild rightChild

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 46
Binary Tree Traversals
 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, LRV, VLR
– inorder, postorder, preorder

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)

(1) / (3)(1)C (3)


(2) (2)

(1) A(3) (1)B (3)


(2) (2)
2021/12/6 CHAPTER 5 55
Iterative Inorder Traversal
(Non-recursive Version, Using Stack)

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
/

5 Pop; print 1 → data (+)


6
A B Push 1→rightChild (9)
1

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
/

5 Pop; print 9→data (E)


6
A B
9

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)

void level_order(treePointer ptr)


/* level order tree traversal */
{
int front = rear = 0;
treePointer queue[MAX_QUEUE_SIZE];
if (!ptr) return; /* empty queue */
addq(ptr);
for (;;) {
ptr = deleteq();

2021/12/6 Trees 100


if (ptr) {
printf(“%d”, ptr->data);
if (ptr->leftChild)
addq(ptr->leftChild);
if (ptr->rightChild)
addq(ptr->rightChild);
}
else break;
}
}

2021/12/6 Trees 101


Level Order Traversal
(using queue)
void level_order(treePointer ptr) +*E*D/CAB
/* level order tree traversal */
{ int front = rear = 0;
treePointer queue[MAX_QUEUE_SIZE]; +
if (!ptr) return; /* empty queue */
addq(ptr);
for (;;) { * E
ptr = deleteq();
if (ptr) {
printf(“%d”, ptr->data); * D
if (ptr->leftChild)
addq(ptr->leftChild);
if (ptr->rightChild) / C
addq(ptr->rightChild);
}
else break;
} A B
}
Breadth First Search
2021/12/6 Trees (BFS) Approach 102
Level Order Traversal
(using queue)

1
+ +*E*D/ CAB

2 9
* E Output:

3 8 D
*

4 7 C
/

5 6
A B

2021/12/6 Trees 103


Level Order Traversal
(using queue)

1
+ +*E*D/ CAB

2 9
* E Output:

3 8 D front
*

4 7 C Enqueue (1)
/

5 6
A B

2021/12/6 Trees 104


Level Order Traversal
(using queue)

1
+ +*E*D/ CAB

2 9
* E Output:
rear
3 8 D front
*

4 7 C 1
/

5 6
A B

2021/12/6 Trees 105


Level Order Traversal
(using queue)

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

2021/12/6 Trees 106


Level Order Traversal
(using queue)

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

2021/12/6 Trees 107


Level Order Traversal
(using queue)

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

2021/12/6 Trees 108


Level Order Traversal
(using queue)

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

2021/12/6 Trees 109


Level Order Traversal
(using queue)

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

2021/12/6 Trees 110


Level Order Traversal
(using queue)

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

2021/12/6 Trees 111


Level Order Traversal
(using queue)

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

2021/12/6 Trees 112


Level Order Traversal
(using queue)

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

2021/12/6 Trees 113


Level Order Traversal
(using queue)

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

2021/12/6 Trees 114


Level Order Traversal
(using queue)

1
+ +*E*D/ CAB

2 9
* E Output: + * E * D / C A B
rear
3 8 D front
*

4 7 C
/

5 6 Queue Empty Stop


A B

2021/12/6 Trees 115


Outline
 Introduction
 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 116
Additional Binary Tree Operations

 Copying binary trees


 Testing equality

 The satisfiability problem

2021/12/6 Trees 117


Copying Binary Trees
treePointer copy(treePointer original)
{
treePointer temp;
if (original) {
MALLOC(temp, sizeof(node));
temp->leftChild=copy(original->leftChild);
temp->rightChild=copy(original->rightChild);
temp->data=original->data;
return temp;
}
return NULL;
}
postorder

2021/12/6 Trees 118


Equality of Binary Trees
the same topology and data
int equal(treePointer first, treePointer second)
{
/* function returns FALSE if the binary trees first and
second are not equal, otherwise it returns TRUE */

return ((!first && !second) || (first && second &&


(first->data == second->data) &&
equal(first->leftChild, second->leftChild) &&
equal(first->rightChild, second->rightChild)))
}

2021/12/6 Trees 119


Propositional Calculus Expressions
 A variable is an expression.
 If x and y are expressions, then ¬x, x∧y,
x∨y are expressions.
 Parentheses can be used to alter the normal
order of evaluation (¬ > ∧ > ∨).
 Example: x1 ∨ (x2 ∧ ¬x3)

 satisfiability problem: Is there an


assignment to make an expression true?

2021/12/6 Trees 120


(x1 ∧ ¬x2) ∨ (¬ x1 ∧ x3) ∨ ¬x3

(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

leftChild data value rightChild

typedef enum {not, and, or, true, false } logical;


typedef struct node *treePointer;
typedef struct node {
treePointer listChild;
logical data;
short int value;
treePointer rightChild;
};

2021/12/6 Trees 122


First version of satisfiability algorithm
for (all 2n possible 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”);

2021/12/6 Trees 123


Postorder Evaluation Function
void PostorderEval(treePointer node)
{
/* modified post order traversal to evaluate a
propositional calculus tree */
if (node) {
post_order_eval(node->leftChild);
post_order_eval(node->rightChild);
v

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

case or: node->value =


node->rightChild->value | | x 1 ¬ ¬ x3

node->leftChild->value;
break; 2x x
1

case true: node->value = TRUE;


break;
case false: node->value = FALSE;
}
Time Complexity: O(n) where n is the number of
}
nodes in the propositional calculus tree.
} Trees 125
Section 5.4.3 Exercise 2
Write a C function swapTree that takes a binary tree and swaps the
left and right children of every node. An example is shown below.

B C

D E F

G H

t t.SwapTree()
A swap tree example
reverse
Inorder: DGBEAFHC Inorder: CHFAEBGD

2021/12/6 Trees 126


SwapTree Function

void SwapTree(TreePointer Ptr)


{ TreePointer tempPtr;
if (Ptr != NULL) {
tempPtr = Ptr ->leftChild;
Ptr->leftChild = Ptr->rightChild ;
Ptr->rightChild = tempPtr;
SwapTree(Ptr->leftChild);
SwapTree(Ptr->rightChild);
}
}
2021/12/6 Trees 127
Outline
 Introduction
 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 128
Threaded Binary Trees
 Too 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
 Replace these null pointers with some useful
“threads”.

2021/12/6 Trees 129


Threaded Binary Trees (Cont’d)
If ptr->leftChild is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal

If ptr->rightChild is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal

2021/12/6 Trees 130


A Threaded Binary Tree
root A

dangling
B C

D E F G
dangling
inorder traversal:
H I H, D, I, B, E, A, F, C, G

2021/12/6 Trees 131


Data Structures for Threaded BTs
typedef struct threadedTree *threadedPointer;
typedef struct threadedTree {
short int leftThread;
threadedPointer leftChild;
char data;
threadedPointer rightChild;
short int rightThread; };

leftThread leftChild data rightChild rightThread

TRUE   FALSE

TRUE: thread FALSE: child


2021/12/6 Trees 132
Data Structures for Threaded BTs
 In order that we leave no loose threads, we will
assume a header node.
 The original tree is the left subtree of the header
node.
 An empty threaded binary tree is represented by the
header node as the following figure.
leftThread leftChild data rightChild rightThread

TRUE   FALSE

TRUE: thread FALSE: child


2021/12/6 Trees 133
Memory Representation of A Threaded BT
root
(head node)

f=false; t=true

2021/12/6 Trees 134


Finding the Inorder Successor of a
Node in Threaded BTs
threadedPointer insucc(threadedPointer tree)
{ /*find the inorder successor of tree in a
threaded binary tree */

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

2021/12/6 Trees 137


Examples
Insert a node D as a right child of B.

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)

Right subtree of B is nonempty.

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 141
Heaps
 Priority Queues
– An element to be inserted is the one with highest (or
lowest) priority.
• Max (or Min) Priority Queues
– The simplest data structure representing priority
queues is unordered linked lists.
– Heap is one of the data structures to represent
priority queues.
 Max (or Min) Heaps

2021/12/6 Trees 142


ADT for MaxPriorityQueue
ADT MaxPriorityQueue is
objects: a collection of n > 0 elements, each element has a key
functions:
for all q ∈MaxPriorityQueue, item ∈ Element, n ∈ integer
MaxPriorityQueue create(max_size)::= create an empty priority queue
Boolean isEmpty(q, n) ::= if (n>0) return TRUE
else return FALSE
Element top(q, n) ::= if (!isEmpty(q,n)) return an
instance of the largest element in q
else return error
Element pop(q,n) ::= if (!isEmpty(q,n)) return an instance of the
largest element in q and remove it from the pq
else return error
MaxPriorityQueue push(q,item,n)::= insert item into pq and retur
the resulting priority queue.
2021/12/6 Trees 143
Application: priority queues

 machine services
– amount of time (min heap)
– amount of payment (max heap)
 factory
– time tag

2021/12/6 Trees 144


Data Structures

 unordered linked list


 unordered array

 sorted linked list

 sorted array

 heap

2021/12/6 Trees 145


Priority Queue Representations
Time Complexity
Representation Insertion Deletion

Unordered Θ(1) Θ(n)


array
Unordered Θ(1) Θ(n)
linked list
Sorted array O(n) Θ(1)
Sorted linked O(n) Θ(1)
list
Max heap O(log2n) O(log2n)

2021/12/6 Trees 146


Heaps
 A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
 A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
 Operations on heaps

– creation of an empty heap


– insertion of a new element into the heap;
– deletion of the largest element from the heap
2021/12/6 Trees 147
Max Heaps

[1] [1] 9 [1]


14 30

[2] [3] [2] 6 [3] 3 [2]


12 7 25
[4] [5] [6] [4]
10 8 6 5

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

[1] [1] 10 [1]


2 11

[2] [3] [2] 20 [3] 83 [2]


7 4 21
[4] [5] [6] [4]
10 8 6 50

2021/12/6 Trees 150


structure MaxHeap
ADT for Max Heap
objects: a complete binary tree of n > 0 elements organized so that
the value in each node is at least as large as those in its children
functions:
for all heap belong to MaxHeap, item belong to Element, n,
max_size belong to integer
MaxHeap Create(max_size)::= create an empty heap that can
hold a maximum of max_size elements
Boolean HeapFull(heap, n)::= if (n==max_size) return TRUE
else return FALSE
MaxHeap Insert(heap, item, n)::= if (!HeapFull(heap,n)) insert
item into heap and return the resulting heap
else return error
Boolean HeapEmpty(heap, n)::= if (n>0) return FALSE
else return TRUE
Element Delete(heap,n)::= if (!HeapEmpty(heap,n)) return one
instance of the largest element in the heap
and remove it from the heap
2021/12/6 else return error 151
Example of Insertion into a Max Heap

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)

2021/12/6 Trees 152


Example of Insertion into a Max Heap

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)

2021/12/6 Trees 153


Example of Inserting 25 into a Max Heap

2021/12/6 Trees 154


Some C declarations used for a max heap

#define MAX_ELEMENTS 200 /* maximum heap size+1


*/
#define HEAP_FULL(n) (n== MAX_ELEMENTS-1)
#define HEAP_EMPTY(n) (!n)
typedef struct {
int key;
/* other fields */
} element;
element heap[MAX_ELEMENTS];
int n=0;

2021/12/6 Trees 155


Insertion into a Max Heap
void push(element item, int *n)
{ /* insert item into a max heap of current
size *n*/
int i;
if (HEAP_FULL(*n)) {
fprintf(stderr, “The heap is full. \n”);
exit(EXIT_FAILURE);
}
i = ++(*n);
while ((i != 1) && (item.key > heap[i/2].key))
{ heap[i] = heap[i/2]; i /= 2; }
heap[i] = item; 2k-1=n ==> k=log2(n+1)
}
Time Complexity: O(log2n)

2021/12/6 Trees 156


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

• •
• •
• 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

2021/12/6 Trees 160


Example of Deletion from a Max Heap

remove
20 10 15

15 2 15 2 14 2

14 10 14 10

2021/12/6 Trees 161


Example of Deletion from a Max Heap

2021/12/6 Trees 162


Deletion from a Max Heap
element pop(int *n)
{ /* delete element the highest key from the
heap */
int parent, child;
element item, temp;
if (HEAP_EMPTY(*n)) {
fprintf(stderr, “The heap is empty\n”);
exit(EXIT_FAILURE);
}
/* save value of the element with the highest
key */
item = heap[1];
/* use last element in heap to adjust heap */
temp = heap[(*n)--];
parent = 1;
child = 2; Trees 163
while (child <= *n) {
/* find the larger child of the current parent */
if ((child < *n) &&
(heap[child].key < heap[child+1].key))
child++;
if (temp.key >= heap[child].key) break;
/* move to the next lower level */
heap[parent] = heap[child];
parent = child;
child *= 2; Since the height of a heap with n
} nodes is log2(n+1),the while
heap[parent] = temp; loop of pop is iterated O(log2n).
return item; Hence the time complexity of a
} deletion is O(log2n).

2021/12/6 Trees 164


Example of Deletion from a Max Heap
[1] parent=1

[2] [3] child=2 parent=2

[6]
child=4
[4] [5]

temp=heap[6]=6 parent=1
temp=6
child=2

parent=4
child=8

child=8 > no. of tree nodes (5)


2021/12/6 Trees heap[parent]=temp;
A Sequence of Deletions from a Max Heap
remove remove remove
23
28 19
17 19
23 19 17 18

17 14 10 18
14 10 18 14 10

remove remove remove remove

10 14 17 18

10 14 10 17 10

14

2021/12/6 Trees 166


Heap-Sort
 Consider a priority queue  Using a heap-based priority
with n items implemented queue, we can sort a
by means of a heap sequence of n elements in
– the space used is O(n)
O(n log n) time
– methods insert and  The resulting algorithm is
removeMin take O(log n) called heap-sort
time  Heap-sort is much faster
– methods size, isEmpty, and than quadratic sorting
min take time O(1) time algorithms, such as
insertion-sort and selection-
sort

© 2010 Goodrich, Tamassia Heaps 167


Sorting Using a Heap

 Input: array A with n elements to be sorted


 Temporary array T of size at least n+1, which will
work as a heap.
 Two steps:
1. Create a heap T using the elements of A
• Insert each element A[i] into T using T.insert(A[i])
2. Call T.deleteMin() n times to move the elements from T
to A, one by one.

2021/12/6 Heaps 168


Sorting Code
for (i = 0; i < n; i++)
T.insert(A[i]);
for (i = 0; i < n; i++)
A[i] = T.deleteMin();

2021/12/6 Heaps 169


Time Complexity of Heap Sort
 Stirling’s approximation: n! ≈ n e 2πn
n −n

 Insertions

log1 + log 2 + … + log n = log(n!) = O(nlogn)


 Deletions

log1 + log 2 + … + log n = log(n!) = O(nlogn)


 Total running time= O(nlogn)

2021/12/6 Heaps 170


In-place Heap Sort

2021/12/6 Heaps 171


Heap Sort Comparison
Using a temp heap T In place (no temp array)

(1) for (i = 0; i<n; i++) (1) buildHeap on A; // O(n) time


T.insert(A[i]); // bottom-up heap construction
// O(nlogn) time
(2) Repeat
(2) for (i = 0; i<n; i++) deleteMax; copyMax;
A[i] = T.deleteMin(); until the heap is empty;
// O(nlogn) time
// O(nlogn) time
Note: min heap
Note: max heap

2021/12/6 Heaps 172


In-place Heap Sort

 The heap sort algorithm we just discussed


requires a temporary array T (a min heap).
 In-place heap sort uses only one array, the
original array storing the inputs.
 Two steps:
1. Transform the original array to a max heap using
buildHeap procedure (“heapify”)
2. Call deleteMax() n times to get the array sorted.

2021/12/6 Heaps 173


In-place Heap Sort
Array interpreted as a binary tree
input file 1 2 3 4 5 6 7 8 9 10
26 5 77 1 61 11 59 15 48 19

[1] 26

[2] 5 [3] 77

[4] 1 [5] 61 [6] 11 [7] 59

[8] 15 [9] 48 [10] 19

2021/12/6 In-place Heap Sort 174


In-place Heap Sort
Step 1: Build a max heap for in-place heap sort.
(Bottom-up max heap construction)

initial heap [1] 77

[2] 61 [3] 59
exchange
[4] 48 [5] 19 [6] 11 [7] 26

[8] 15 [9] 1 [10] 5

2021/12/6 In-place Heap Sort 175


Step 2: Call deleteMax n times for in-place heap sort.
[1] 61

[2] 48 [3] 59

[4] 15 [5] 19 [6] 11 [7] 26

[8] 5 [9] 1 [10] 77


(a)

[1] 59

[2] 48 [3] 26

[4] 15 [5] 19 [6] 11 [7] 1

[8] 5 [9] 61 [10] 77

(b)

2021/12/6 In-place Heap Sort 176


Step 2: Call deleteMax n times for in-place heap sort. (cont’d)
[1] 48

[2] 19 [3] 26

[4] 15 [5] 5 [6] 11 [7] 1

[8] 59 [9] 61 [10] 77


(c)

[1] 26

[2] 19 [3] 11

[4] 15 [5] 5 [6] 1 [7] 48

[8] 59 [9] 61 [10] 77

(d)

2021/12/6 In-place Heap Sort 177


Step 2: Call deleteMax n times for in-place heap sort. (cont’d)
[1] 19

[2] 15 [3] 11

[4] 1 [5] 5 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77


(e)
[1] 15

[2] 5 [3] 11

[4] 1 [5] 19 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77

(f)

2021/12/6 In-place Heap Sort 178


Step 2: Call deleteMax n times for in-place heap sort. (cont’d)
[1] 11

[2] 5 [3] 1

[4] 15 [5] 19 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77

(g)

[1] 5

[2] 1 [3] 11

[4] 15 [5] 19 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77


(h)

2021/12/6 In-place Heap Sort 179


Step 2: Call deleteMax n times for in-place heap sort. (cont’d)
[1] 1

[2] 5 [3] 11

[4] 15 [5] 19 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77

(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

[4] 15 [5] 19 [6] 26 [7] 48

[8] 59 [9] 61 [10] 77

(j)
2021/12/6 In-place Heap Sort 180
Example of Bottom-up Max Heap
Construction

2021/12/6 Heaps 181


Initializing A Max Heap
1

2 3

4 5 6 7

8 9 7
10 811

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

2021/12/6 Heaps 182


Initializing A Max Heap
1

2 3

4 5 6 7

8 9 7
10 811

Start at rightmost array position that has a child.


Index is n/2. Heaps 183
Initializing A Max Heap
1

2 3

4 11 6 7

8 9 7
10 85

Move to next lower array position.

2021/12/6 Heaps 184


Initializing A Max Heap
1

2 3

4 11 6 7

8 9 7
10 85

2021/12/6 Heaps 185


Initializing A Max Heap
1

2 3

9 11 6 7

8 4 7
10 85

2021/12/6 Heaps 186


Initializing A Max Heap
1

2 3

9 11 6 7

8 4 7
10 85

2021/12/6 Heaps 187


Initializing A Max Heap
1

2 7

9 11 6 3

8 4 7
10 85

2021/12/6 Heaps 188


Initializing A Max Heap
1

2 7

9 11 6 3

8 4 7
10 85

2021/12/6 Heaps 189


Initializing A Max Heap
1

11 7

9 6 3

8 4 7
10 85

Find a home for 2.


2021/12/6 Heaps 190
Initializing A Max Heap
1

11 7

9 10 6 3

8 4 8 75

Find a home for 2.


2021/12/6 Heaps 191
Initializing A Max Heap
1

11 7

9 10 6 3

8 4 72 58

Done, move to next lower array position.


2021/12/6 Heaps 192
Initializing A Max Heap
1

11 7

9 10 6 3

8 4 72 58

Find home for 1.


2021/12/6 Heaps 193
Initializing A Max Heap
11

9 10 6 3

8 4 72 85

Find home for 1.


2021/12/6 Heaps 194
Initializing A Max Heap
11

10 7

9 6 3

8 4 72 85

Find home for 1.


2021/12/6 Heaps 195
Initializing A Max Heap
11

10 7

9 5 6 3

8 4 72 8

Find home for 1.


2021/12/6 Heaps 196
Initializing A Max Heap
11

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

© 2010 Goodrich, Tamassia Heaps 198


Running Time of Bottom-up Heap Construction
 Let T denote the running time.
no. of nodes depth keys
0 1
2d d 1 2

depth h-d h−1 2h−1

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

2021/12/6 Heaps 199


Running Time of Bottom-up Heap Construction
0
T= ∑ ( h −
d = h −1
d ) ⋅ 2 d
= 1 ⋅ 2 h −1
+ 2 ⋅ 2 h−2
+ 3 ⋅ 2 h −3
+ ⋅ ⋅ ⋅ + h ⋅ 2 0
(1)

2−1T = 1 ⋅ 2h − 2 + 2 ⋅ 2h − 3 + 3 ⋅ 2h − 4 + ⋅ ⋅ ⋅ + (h − 1) ⋅ 20 + h ⋅ 2−1 (2)


−1)
(1)-(2) ⇒ (1 − 2 T = (2 h −1 + 2 h − 2 + 2 h −3 + ⋅ ⋅ ⋅ + 20 ) − h ⋅ 2 −1
1 h 1
⇒ T = (2 − 1) − h
2 2
⇒ T = 2 h +1 − 2 − h Running time: O(n)
 n = 2 h +1 − 1 (or h = log 2 (n + 1) − 1)

∴ 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

© 2010 Goodrich, Tamassia Heaps 201


Merging Two Heaps
3 2
 We are given two heaps
8 5 4 6
and a key k
 We create a new heap
with the root node 7
storing k and with the 3 2
two heaps as subtrees 8 5 4 6
 We perform downheap to
restore the heap-order
2
property
3 4
8 5 7 6

© 2010 Goodrich, Tamassia Heaps 202


Bottom-up Heap Construction
 We can construct a heap
storing n given keys in
using a bottom-up
2i −1 2i −1
construction with log n
phases
 In phase i, pairs of heaps
with 2i −1 keys are merged
into heaps with 2i+1−1 keys
2i+1−1

© 2010 Goodrich, Tamassia Heaps 203


Example

16 15 4 12 6 7 23 29

25 5 11 27

16 15 4 12 6 7 23 29

© 2010 Goodrich, Tamassia Heaps 204


Example (cont’d)

25 5 11 27

16 15 4 12 6 9 23 29

15 4 6 23

16 25 5 12 11 9 27 29

© 2010 Goodrich, Tamassia Heaps 205


Example (cont’d)

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

© 2010 Goodrich, Tamassia Heaps 206


Example (end)
10

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

© 2010 Goodrich, Tamassia Heaps 207


Outline
 Introduction
 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 208
Binary Search Trees
⬥ Searching
⬥ Insertion
⬥ Deletion
⬥ Joining
⬥ Splitting

2021/12/6 Trees 209


Binary Search Trees
 Heaps
– a min (max) element is deleted. O(log2n)
– deletion of an arbitrary element O(n)
– search for an arbitrary element O(n)
 Binary search tree
– Every element has a unique key.
– The keys in a nonempty left subtree (right
subtree) are smaller (larger) than the key in the
root of subtree.
– The left and right subtrees are also binary
search trees.
2021/12/6 Trees 210
Examples of Binary Search Trees

20 30 60

12 25 5 40 70

10 15 22 2 65 80

2021/12/6 Trees 211


Binary Search Trees:
Searching for a Key
• Given a pointer to the root of a tree and a key k:
5
– Return a pointer to a node with key k
if one exists 3 7
2 4 9
– Otherwise return NIL
• Idea
– Starting at the root: trace down a path by comparing k
with the key of the current node:
• If the keys are equal: we have found the key
• If k < key[x] search in the left subtree of x
• If k > key[x] search in the right subtree of x
2021/12/6 Trees 212
Binary Search Trees:
Searching for a Key

15
• Search for key 13:
6 18 – 15 → 6 → 7 → 13
3 7 17 20
2 4 13
9

2021/12/6 Trees 213


Searching a Binary Search Tree
(Recursive Version)
element* search(treePointer root,int k)
{
/* return a pointer to the element whose key
is k. If there is no such
element, return NULL. */

if (!root) return NULL;


if (k == root->data.key)
return &(root->data);
if (k < root->data.key)
return search(root->leftChild,k);
return search(root->rightChild,k);
}
2021/12/6 Trees 214
Another Searching Algorithm
(Iterative Version)
treePointer search2(treePointer tree, int k)
{ /* return a pointer to the element whose key
is k. If there is no such
element, return NULL. */

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” )

2021/12/6 Trees 218


Binary Search Trees: Insertion
x=root[T], y=NIL y
Insert 13: 12 12
x
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 17

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

2021/12/6 Trees 220


Inserting a Dictionary Pair into a Binary Search Tree
void insert(treePointer *node,int k,iType theItem)
{ /* if k is in the tree pointed at by node do
nothing;otherwise add a new node with data
=(k,theItem) */
treePointer ptr,temp = modifiedSearch(*node,k);
if (temp || !(*node)) {
/* k is not in the tree */
MALLOC(ptr, sizeof(*ptr));
ptr→data.key = k;
ptr→data.item = theItem;
ptr→leftChild = ptr→rightChild = NULL;
if (*node) /* insert as child of temp */
if(k < temp→data.key) temp→leftChild = ptr;
else temp→rightChild = ptr;
else *node = ptr;
} Time Complexity: O(h)
} where h is the height of the binary tree.

2021/12/6 Trees 221


Function modifiedSearch(*node, k) called
by the function insert(*node, k, theItem)

Function modifiedSearch(*node, k) returns NULL


if the tree is empty or
if k is present.

Otherwise,it returns a pointer to the last node


of the tree that was encountered during the
search.

2021/12/6 Trees 222


Incremental Construction of a Binary Search Tree for
Input Data (25, 30, 24, 58, 45, 26, 12, 14)
1. insert
insert25 2. insert 30 3. insert 24 4. insert 58

5. insert 45 6. insert 26 7. insert 12 8. insert 14

2021/12/6 Trees 223


Incremental Construction of a Binary Search Tree for
Input Data (12, 14, 24, 25, 26, 30, 45, 58)

The resulting binary search tree


is a skewed binary search tree.
12
14
24
25
26
30
45
58

2021/12/6 Trees 224


Binary Search Trees: Deletion
• Goal:
– Delete a given node z from a binary search tree
• Idea:
– Case 1: z has no children
• Delete z by making the parent of z point to NIL
15 15

5 16 5 16

3 12 20 3 12 20
z
10 13 18 23 10 18 23

6 delete 6

2021/12/6 7 Trees 7 225


Binary Search Trees: Deletion
• Case 2: z has one child
– Delete z by making the parent of z point to z’s child,
instead of to z

15 delete 15
z
5 16 5 20

3 12 20 3 12 18 23

10 13 18 23 10

6 6

7 7

2021/12/6 Trees 226


Binary Search Trees: Deletion
• Case 3: z has two children
– z’s successor (y) is the minimum node in z’s right subtree
– y has either no children or one right child (but no left child)
– Delete y from the tree (via Case 1 or 2)
– Replace z’s key and satellite data with y’s.
6 15 15
delete z
5 16 6 16

3 12 20 3 12 20

10 13 18 23 10 13 18 23

y 6 7

2021/12/6 7 Trees 227


Deletion from a Binary Search Tree
Case 1: deletion of a leaf node (easy)

30
30
30
5 40
5 40
5 40

80 2
35 80 2
2

Delete 35 Delete 80

2021/12/6 Trees 228


Deletion from a Binary Search Tree
Case 2: deletion of a nonleaf node with single one child
(easy)

1
1
Delete 2
2
T1 T2
T1 X

T2
2021/12/6 Trees 229
Deletion from a Binary Search Tree
Example (Case 2)

2021/12/6 Trees 230


Deletion from a Binary Search Tree
Case 3: deletion of a nonleaf node with two children
40 40
X Delete 60
20 60 20 55

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’

2021/12/6 Trees 232


Deletion from a Binary Search Tree
Example (Case 3)
X

2021/12/6 Trees 233


Joining and Splitting Binary Trees
 Three-Way Join Operation:
threewayjoin (small, mid, big)
– Time Complexity: O(1)
 Two-Way Join Operation: twowayjoin(small, big)
– Time Complexity: O(min{height(small),height(big)})
 Splitting Operation:
split(theTree, k, small, mid, big)
– Time Complexity: O(height(theTree))

2021/12/6 Trees 234


Three-Way Join Operation:
threewayjoin (small, mid, big)

small, big: binary search trees The resulting binary


search tree
mid: a key
mid
small big

Time Complexity: O(1)

2021/12/6 Trees 235


Three-Way Join Operation:
threewayjoin (small, mid, big)
mid
80
small big

50 95

30 68 90 97

45 56 73 88 94
23

55 64 71 77 92

2021/12/6 Trees 236


Two-Way Join Operation:
twowayjoin (small, big)
1. Delete from small the pair mid with the largest key.
(Let the resulting binary search tree be small’.)
2. Perform the three-way join operation
threewayjoin(small’,mid, big)
The resulting binary
search tree

mid
small’ big
Time Complexity:
O(min{height(small),height(big)})

2021/12/6 Trees 237


Two-Way Join Operation:
twowayjoin (small, big)

77
small big

50 95

30 68 90 97

23 45 56 73 88 94

55 64 71 77 80 92

2021/12/6 Trees 238


Two-Way Join Operation:
twowayjoin (small, big)

80
small big

50 95

30 68 90 97

23 45 56 73 88 94

55 64 71 77 80 92

2021/12/6 Trees 239


Split(k)

• 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).

2021/12/6 Trees 240


Split A Binary Search Tree

A S B

a B
C b
c D
E d
e m
f g

2021/12/6 Trees 241


Split A Binary Search Tree

S B

B A
C b a
c D
E d
e m
f g

2021/12/6 Trees 242


Split A Binary Search Tree

S B

A B
C a b
c D
E d
e m
f g

2021/12/6 Trees 243


Split A Binary Search Tree

S B

A B
a C b
D
c
E d
e m
f g

2021/12/6 Trees 244


Split A Binary Search Tree

S B

A B
a C D b

c d
E
e m
f g

2021/12/6 Trees 245


Split A Binary Search Tree

S B

A B
a C D b

c E d

e
m
f g

2021/12/6 Trees 246


Split A Binary Search Tree

S B

A B
a C D b

c E g d

e f
m

2021/12/6 Trees 247


Split(80) small mid big
80
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 77

55 64 73 94

71 80 92

77 88

2021/12/6 Trees 248


Split(80) small mid big
80
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 77

55 64 73 94

71 80 92

77 88

2021/12/6 Trees 249


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 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

2021/12/6 Trees 252


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

2021/12/6 Trees 253


Splitting Operation: split(theTree, k, small, mid, big)
void split(nodePointer *theTree, int k,
nodePointer *samll,element *mid,
nodePointer *big)
{ /* split the binary search tree with
respect to key k */
if (!theTree){*small = *big = 0;
(*mid).key = -1;return;}
/* empty tree */
nodePointer sHead, bHead, s, b, currentNode;
/* create header nodes for small and big */
MALLOC(sHead, sizeof(*sHead));
MALLOC(bHead, sizeof(*bHead));
s = sHead, b = bHead;
/* do the split */
currentNode = *theTree; 254
while (currentNode)
if (k < currentNode→data.key) {
/* add to big */
b→leftChild = currentNode;
b = currentNode;
currentNode = currentNode →leftChild;}
else if(k > currentNode→data.key){
/* add to small */
s→rightChild = currentNode;
s = currentNode;
currentNode=currentNode→rightChild;}
else { /* split at currentNode */
s→rightChild=currentNode→leftChild;
b→leftChild=currentNode→rightChild;
*small = sHead→rightChild;
free(sHead);
2021/12/6 *big = bHead→leftChild; 255
free(bHead);
(*mid).item =currentNode→data.item;
(*mid).key = currentNode→data.key;
free(currentNode);
return;}
/* no pair with key k */
s→rightChild = b→leftChild = 0;
*small = sHead→rightChild; free(sHead);
*big = bHead→leftChild; free(bHead);
(*mid).key = -1;
return;
}
split(theTree, k, small, mid, big)
Time Complexity: O(height(theTree))
2021/12/6 Trees 256
Height of a Binary Search Tree

 When insertion and deletion are made at


random, the height of the BST is O(log n)
on the average.
 Balanced search trees with a worst-case
height of O(log n)
– Each insertion or deletion operation takes time
O(h) where h is tree height.
– AVL, red/black, 2-3, 2-3-4, B, and B+ trees
2021/12/6 Trees 257
Outline
 Introduction
 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 258
Selection Trees
(1) winner tree
(2) loser tree

2021/12/6 Trees 259


Selection trees
Problem.
Merge k ordered sequences, called runs, into
a single ordered sequence.

A straightforward method need k-1 comparisons to find the


next smallest element.
For k>2, the data structure selection tree can used to reduce the
number of comparisons to find the next smallest element.
– winner trees
– loser trees

2021/12/6 Trees 260


Winner trees
 A winner tree is a complete binary tree in which each
node represents the smaller of its two children. Thus, the
root node represents the smallest node in the tree.
6 1

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

2021/12/6 Winner Trees 261


sequential
allocation
winner tree Each node represents
the smaller of its two
1
scheme children.
6
(complete
2 3
binary
tree) 6 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

run 1 run 2 run 3 run 4 run 5 run 6 run 7 run 8

2021/12/6 Winner Trees 262


Winner trees
6 1 8 1

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

2021/12/6 Winner Trees 263


Winner trees
6 8 9

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

2021/12/6 Winner Trees 264


Analysis of merging runs using
winner trees
 k: # of runs
 n: # of records
 setup time: O(k) (k-1)
 The time to restructure the winner tree : O(log2k)
log2(k+1)
 the time to merge all n records: O(nlog2k)
(merge n records into an output file)
 slight modification: tree of loser
– consider the parent node only (vs. sibling nodes)

2021/12/6 Winner Trees 265


Winner Trees
Complete binary tree with n external
nodes and n - 1 internal nodes.
External nodes represent tournament
players.
Each internal node represents a match
played between its two children;
the winner of the match is stored at
the internal node.
Root has overall winner.
2021/12/6 Winner Trees 266
Winner Tree For 16 Players

2021/12/6 player Winner Trees match node 267


Winner Tree For 16 Players
1

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

Smaller element wins => min winner tree.


2021/12/6 Winner Trees 268
Winner Tree For 16 Players
1

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

height is log2 n (excludes player level)


2021/12/6 Winner Trees 269
Complexity Of Initialize

• O(1) time to play match at each match node.


• n - 1 match nodes.
• O(n) time to initialize n player winner tree.

2021/12/6 Winner Trees 270


Applications

Sorting.

Insert elements to be sorted into a


winner tree.
Repeatedly extract the winner and
replace by a large value.

2021/12/6 Winner Trees 271


Sort 16 Numbers
1

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

2021/12/6 Winner Trees 272


Sort 16 Numbers
1

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

2021/12/6 Winner Trees 273


Sort 16 Numbers
1

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 winner tree.


 O(n) time
• Remove winner and replay.
 O(log n) time
• Remove winner and replay n times.
 O(n log n) time
• Total sort time is O(n log n).
• Actually Theta(n log n).
2021/12/6 Winner Trees 290
Winner Tree Operations

• Initialize
 O(n) time
• Get winner
 O(1) time
• Remove/replace winner and replay
 O(log n) time
 more precisely Theta(log n)

2021/12/6 Winner Trees 291


Replace Winner And Replay
1

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

Replace winner with 6.


Winner Trees 292
Replace Winner And Replay
1

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

Replay matches on path to root.


2021/12/6 Winner Trees 293
Replace Winner And Replay
1

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

Replay matches on path to root.


2021/12/6 Winner Trees 294
Replace Winner And Replay
1

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

Opponent is player who lost last match played at this node.


2021/12/6 Winner Trees 295
Loser trees

 A selection tree in which each nonleaf node


retains a pointer to the loser is called a loser
tree.
– The leaf nodes represent the first record in each
run.
– An additional node, node 0, has been added to
represent the overall winner of the tournament.

2021/12/6 Loser Trees 296


Figure 5.36: Tree of losers corresponding to winner tree of
Figure 5.32
0 overall
6 winner
8
9
1
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

winner tree loser tree


9 1

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

run run run run run run run run


1 2 3 4 5 6 7 8

2021/12/6 Loser Trees 299


Loser Tree

Each match node stores the match


loser rather than the match winner.

2021/12/6 Loser Trees 300


Min Loser Tree For 16 Players

4 8

4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

2021/12/6 Loser Trees 301


Min Loser Tree For 16 Players

6 1

4 8 5 7

4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

2021/12/6 Loser Trees 302


Min Loser Tree For 16 Players
1

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

2021/12/6 Loser Trees 303


Min Loser Tree For 16 Players
1

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

2021/12/6 Loser Trees 304


Min Loser Tree For 16 Players
1

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

2021/12/6 Loser Trees 305


Min Loser Tree For 16 Players
1

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

2021/12/6 Loser Trees 306


Min Loser Tree For 16 Players
2

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

2021/12/6 Loser Trees 307


1 Winner

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

2021/12/6 Loser Trees 308


Complexity Of Loser Tree
Initialize

• One match at each match node.


• One store of a left child winner.
• Total time is O(n).
• More precisely Theta(n).

2021/12/6 Loser Trees 309


21 Winner

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

Replace winner with 9 and replay matches.


2021/12/6 Loser Trees 310
Complexity Of Replay

• One match at each level that has a match


node.
• O(log n)
• More precisely Theta(log n).

2021/12/6 Loser Trees 311


Outline
 Introduction
 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 312
Forests
 A forest is a set of n >= 0 disjoint trees

A E G

D F H I
B C

Three-Tree Forest

2021/12/6 Trees 313


Transforming a forest into a binary tree
 T1, T2, …, Tn: a forest of trees
B(T1, T2, …, Tn): a binary tree corresponding to this forest
 Obtaining the binary tree representation of Ti for 1≤ i ≤ n
 algorithm
(1) empty, if n = 0
(2) has root equal to root(T1);
has left subtree equal to B(T11, T12, …, T1m),
where T11, T12, …, T1m are the subtrees of root(T1);
has right subtree equal to B(T2, T3, …, Tn).

2021/12/6 Trees 314


Transforming a forest into a binary tree
A
Three-Tree Forest
A E G B E

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

2021/12/6 Trees 316


Forest Traversals

 Preorder and inorder traversals of the


corresponding binary tree T of a forest F
have a natural correspondence to traversals
on F.
 No natural analog for postorder traversal of
the corresponding binary tree T of F

2021/12/6 Trees 317


Forest Traversals
 Postorder
– If F is empty, then return
– Taverse the subtrees of the first tree in forest
postorder
– Traverse the remaining trees of F in forest postorder
– Visit the root of the first tree of F
 Level-order
– Beginning with the root of each tree of F
– Within each level, nodes are visited from left to right.
– The level-order traversal of F and that of its associated
binary tree do not necessarily yield the same result.

2021/12/6 Trees 318


inorder: EFBGCHIJDA
preorder: ABEFCGDHIJ

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 320
Disjoint Sets
 Two Operations
– Union
– Find(i): Find the set containing the element i
 Example

S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}


Union: S1 ∪ S2 = {0, 1, 4, 6, 7, 8, 9} (Si ∩ Sj = φ)
Find(3): 3 ∈ S3 ; Find(8): 8 ∈ S1

2021/12/6 CHAPTER 5 321


Representation of Disjoint Sets
 S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
S1 S2 4 S3 2
0

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

2021/12/6 Trees 323


Figure 5.39:Data Representation of S1, S2 and S3

0
set pointer
name
6 7 8
S1
S2 4

S3 1 9

3 5

2021/12/6 Trees 324


0 4 2

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

2021/12/6 Trees 325


Disjoint Sets
 Two Operations
– Union: simpleUnion Not Good
– Find(i):simpleFind(i)
 Example

S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}


(Si ∩ Sj = φ)
simpleUnion(1,2): S1 ∪ S2 = {0, 1, 4, 6, 7, 8, 9}
simpleFind(3): 3 ∈ S3 ; simpleFind(8): 8 ∈ S1

2021/12/6 CHAPTER 5 326


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

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;
}

void simpleUnion(int i, int j)


{ /* let the first tree become a subtree
of the second */
parent[i]= j;
}
2021/12/6 Trees 327
0 4 2

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

simpleFind (0)=0 simpleFind (4)=4 simpleFind (2)=2


simpleFind (6)=0 simpleFind (1)=4 simpleFind (3)=2
simpleFind (7)=0 simpleFind (9)=4 simpleFind (5)=2
simpleFind (8)=0

2021/12/6 Trees 328


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(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

2021/12/6 CHAPTER 5 332


Figure 5.41:Degenerate tree
We construct the array
representation of a set
S={0,1,2, …, n-1} by the
The time required for n-1 following sequence of
union operations: union-find operations:
O(n)
n-2
union(0,1), find(0)
The time required for find union(1,2), find(0)
operations:
 .

n  .
∑i = O(n2) .
i =2 union(n-2,n-1),find(0)
0

degenerate tree
2021/12/6 Trees 333
Disjoint Sets
 Two Operations
– Union: weightedUnion Good
– Find(i): collapsingFind(i)
 Example

S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}


(Si ∩ Sj = φ)
weightedUnion(1,2): S1 ∪ S2 = {0, 1, 4, 6, 7, 8, 9}
collapsingFind(3): 3 ∈ S3 ; collapsingFind(8): 8 ∈ S1

2021/12/6 CHAPTER 5 334


weightedUnion(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.

-6 -3 -9
i j i

j

2021/12/6 CHAPTER 5 335


weightedUnion(i, j) : root information
-6 i -3
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

Union (0,3) Union (0,n-1)


Trees 337
Example: weightedUnion(0, 6)
-6 -3
0 6

1 2 3 7 8

i [0] [1] [2] [3] [4] [5] [6] [7] [8]


4 5 parent -6 0 0 0 2 2 -3 6 6

-9 i [0] [1] [2] [3] [4] [5] [6] [7] [8]


0
parent -9 0 0 0 2 2 0 6 6

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.

2021/12/6 Trees 341


Weighted Union Properties
From Lemma 5.5, it follows that the time to process
a find is O(log m) if there are m elements in a tree.

If an intermixed sequence of u-1 union and f find


operations is to be processed, the time becomes
O(u + f log u), as no tree has more than u nodes in it.

We need O(n) additional time to initialize the n-tree


forest.

2021/12/6 Trees 342


The Collapsing Rule
Definition [Collapsing rule]: If j is a node on the
path from i to its root and parent[i] ≠ root(i), then
set parent[j] to root(i).
root -5 -5 root
m m

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

2021/12/6 Trees 344


collapsingFind(i) Operation
int collapseFind(int i)
{ /* find the root of the tree containing element
i. Use the collapsing rule to collapse all
nodes from i to root */
int root, trail, lead;

for (root=i; parent[root]>=0;root=parent[root]);


for (trail=i; trail!=root; trail=lead) {
lead = parent[trail];
parent[trail]= root;
}
If j is a node on the path from
return root:
i to its root then make j a child
}
of the root

2021/12/6 Trees 345


Example 5.4 collapsingFind (7)
-8
0 =0
1 2 4
i [0] [1] [2] [3] [4] [5] [6] [7]
parent -8 0 0 2 0 4 4 6
3 5 6

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
α

Lemma 5.6 [Tarjan and Van Leeuwen] Assume that we


start with a forest of trees, each having one node. Let T(f, u)
be the maximum time required to process any intermixed
sequence of f finds and u unions. Assume that u ≥ n/2.
Then
k1(n+f α(f + n, n)) ≤ T(f, u) ≤ k2(n+f α(f + n, n))
for some positive constants k1 and k2.

2021/12/6 Trees 348


The function α(p, q) is related to a functional
inverse of Ackermann’s function A(i, j). These
functions are defined as follows:
A(1, j) = 2j, for j ≥ 1
A(i, 1) = A(i-1, 2) for i ≥ 2
A(i, j) = A(i-1, A(i, j-1)) for i, j ≥ 2

α ( p, q ) = min{ z ≥ 1 | A( z ,  p / q ) > log 2 q}, p ≥ q ≥ 1

2021/12/6 Trees 349


Application to Equivalence Classes
 Find equivalence class i ≡ j
 Find Si and Sj such that i ∈ Si and j ∈ Sj
(two finds)
– Si = Sj do nothing
– Si ≠ Sj union(Si , Sj)
 Example
0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8,
3 ≡ 5, 2 ≡ 11, 11 ≡ 0
{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}
2021/12/6 Trees 350
[-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1]
0 1 2 3 4 5 6 7 8 9 10 11

(a) Initial trees


(a) 起始樹 Example 5.6
[-2] [-2] [-2] [-2] [-1] [-1] [-1] [-1]
0 3 6 8 2 5 7 11 0 ≡ 4, 3 ≡ 1, 6 ≡ 10,
4 1 10 9
8 ≡ 9, 7 ≡ 4, 6 ≡ 8,
(b) 處理完 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9 後高度為 2 的樹
3 ≡ 5, 2 ≡ 11, 11 ≡ 0
(b) Height-2 trees following 0≡4, 3 ≡1, 6≡10 and 8 ≡ 9
Three equivalence
[-3] [-4] [-3] [-2]
0 6 3 2

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 後的樹

[-5] [-4] [-3] {6, 8, 9, 10}


0 6 3

4 7 2 10 8 1 5

11 9

(d) Trees following


(d) 處理完 11 ≡ 11 ≡0
0 後的樹

2021/12/6 Trees 351


 Every binary tree is uniquely defined by
its preorder and inorder sequences.

 Every binary tree is uniquely defined by


its postorder and inorder sequences.

 The preorder and postorder sequences of a


binary tree can not uniquely define the
binary tree.

2021/12/6 Trees 352


(Inorder, Preorder) ⇨ Binary Tree
preorder: ABCDEFGHI
inorder: BCAE D G H F I
A A

B, C D, E, F, G, H, I B D

A
C E F

B D, E, F, G, H, I G I

Every binary tree is uniquely


C defined by its preorder and inorder H
sequences.
2021/12/6 Trees 353
Constructing a Binary Search Tree from
its Inorder and Preorder Sequences A
DBE CGFH

Inorder: DBEACGFH B CGFH


D E
Preorder: ABDECFGH
A
B C

D E GFH

A
B C

D E F

2021/12/6 Trees G H 354


How to build a binary tree
from its inorder and postorder sequences

2021/12/6 Trees 355


(Inorder, Postorder) ⇨ Binary Tree
inorder: 7 8 11 3 5 16 12 18
Example postorder: 8 3 11 7 16 18 12 5
5
inorder: 7 8 11 3 inorder: 16 12 18
postorder: 8 3 11 7 postorder: 16 18 12
7 12

2021/12/6 Trees 356


(Inorder, Postorder) ⇨ Binary Tree
inorder: 7 8 11 3 5 16 12 18
Example postorder: 8 3 11 7 16 18 12 5
5
inorder: 7 8 11 3 inorder: 16 12 18
postorder: 8 3 11 7 postorder: 16 18 12
7 12
inorder: 8 11 3
postorder: 8 3 11
11

2021/12/6 Trees 357


(Inorder, Postorder) ⇨ Binary Tree
inorder: 7 8 11 3 5 16 12 18
Example postorder: 8 3 11 7 16 18 12 5
5
inorder: 7 8 11 3 inorder: 16 12 18
postorder: 8 3 11 7 postorder: 16 18 12
7 12
inorder: 8 11 3
postorder: 8 3 11
11 16 18

inorder: 16 inorder:18
postorder: 16 postorder: 18
8 3

inorder: 8 inorder: 3
postorder: 8 postorder: 3

2021/12/6 Trees 358


(Inorder, Postorder) ⇨ Binary Tree
inorder: 7 8 11 3 5 16 12 18
Example postorder: 8 3 11 7 16 18 12 5
5
inorder: 7 8 11 3 inorder: 16 12 18
postorder: 8 3 11 7 postorder: 16 18 12
7 12
inorder: 8 11 3
postorder: 8 3 11
11 16 18

inorder: 16 inorder:18
postorder: 16 postorder: 18
8 3

inorder: 8 inorder: 3
postorder: 8 postorder: 3

2021/12/6 Trees 359


Constructing a Binary Search Tree from
its Inorder and Postorder Sequences

Inorder: BCAEDGHFI Postorder: CBEHGIFDA

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

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees

 Selection Trees

 Forests

 Representation of Disjoint Sets

 Counting Binary Trees


2021/12/6 Trees 361
Counting Binary Trees
 Three disparate problems :
– Having the same solution
 Problem 1 (Distinct binary trees)
– Determine the number of distinct binary trees
having n nodes
 Problem 2 (Stack permutations)
– Determine the number of distinct permutations of
the numbers from 1 to n obtainable by a stack
 Problem 3 (Matrix multiplication)
– Determine the number of distinct ways of
multiplying n + 1 matrices
2021/12/6 Trees 362
Distinct Binary Trees
n =1
– only one binary tree
n =2
– 2 distinct binary trees
n =3
– 5 distinct binary trees

2021/12/6 Trees 363


Stack Permutations
 For a given preorder permutation 1, 2, 3, what
are the possible inorder permutations?
 Possible inorder permutations obtainable by a
stack are
(1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1)
– (3,1,2) is impossible.
– Each inorder permutation represents a distinct binary
tree.
1 1 1 1
1
Preorder: 2 2 3 2
2 2
(1,2,3)
Inorder 3 3 3
3 364
permutations (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1)
Stack Permutations
 Every binary tree is uniquely defined by its
preorder and inorder sequences.
 Example:
– Preorder: ABCDEFGHI A
Inorder: BCAEDGHFI
B D
– Constructing this binary tree
C E F
A
A G I
B D, E, F, G, H, I
H
B, C D, E, F, G, H, I
C
2021/12/6 Trees 365
Matrix Multiplication
 The product of n matrices
– M1 * M2 * … * Mn
 Matrix multiplication is associative
– Can be performed in any order
 If n = 3, there are 2 possibilities
(M1 * M2) * M3 ((M1* M2) * M3) * M4
M1 * (M2 * M3) (M1* (M2 * M3)) * M4
 If n = 4, there are 5 possibilities M1* ((M2 * M3) * M4)
M1* (M2 * (M3 * M4))
(M1* M2) * (M3 * M4)
2021/12/6 Trees 366
Matrix Multiplication
 Let bn be the number of different ways to compute the
product of n matrices.
– b2 = 1, b3 = 2, b4 = 5
 Let Mij, i ≤ j, be the product of Mi * Mi+1 * … * Mj..
 We may compute M1n by computing any one of the products
M1i * Mi+1,n .
 The numbers of distinct ways to obtain M1i and Mi+1,n are bi
and bn-i , respectively. n −1
 Letting b1 = 1, we have
bn = ∑ bi bn −i , n > 1
i =1

n 1 2 3 4 5 6 7
bn 1 1 2 5 14 42 132

2021/12/6 Trees 367


Number of distinct binary trees
 Let bn be the sum of all the possible binary trees formed
by the following way: a root and two subtrees with bi
and bn-i-1 nodes, for 0 ≤ i ≤ n.
n −1
bn bn = ∑ bi bn −i −1 , n ≥ 1, and b0 = 1
i =0

bi bn-i-1
n 0 1 2 3 4 5 6

bn 1 1 2 5 14 42 132

2021/12/6 Trees 368


All the following three numbers are equal.

 The number of binary trees with n nodes


(distinct binary trees)

 The number of permutations of 1 to n obtainable


with a stack (stack permutations)
 The number of ways to multiply n + 1 matrices
(matrix multiplication)

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

We let which is the generating function


for the number of binary trees.

B ( x) = ∑ bi x = b0 + b1 x + b2 x + b3 x + ⋅ ⋅ ⋅
i 2 3

i≥o

2021/12/6 Trees 370


Number of distinct binary trees
Observe the recurrence relation and the generating function.
B( x) = ∑ bi x = b0 + b1 x + b2 x + b3 x + ⋅ ⋅ ⋅
i 2 3

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

2021/12/6 Trees 372


Number of distinct binary trees
B ( x) = ∑ bi x i
i ≥0

 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,

 Then, simplify the generating function

1 − 1 − 4x
B( x) =
2x
(= ∑b x
n≥o
n
n
)

 Finally, simplify bn

2021/12/6 Trees 374


Derivation of bn
First, we get the Taylor expansion of 1 − 4x
f ( x) = (1 − 4 x)1 / 2
1
f ' ( x) =  (1 − 4 x) −1 / 2 (−4)
2
 1  1 
f " ( x) =   − 1(1 − 4 x) − 3 / 2 (−4) 2
 2  2 
 1  1  1 
f ' ' ' ( x) =   − 1 − 2 (1 − 4 x) − 5 / 2 (−4)3
 2  2  2 
 1  1  1  1 
f ( x) =   − 1 − 2  − 3 (1 − 4 x) − 7 / 2 (−4) 4
( 4)

 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 

2021/12/6 Trees 378


Derivation of bn
 1/ 2  m +1  − 1  m
B( x) = ∑  (−4)   x
m ≥ 0  m + 1  2 
m +1  −1 m +1 m +1 −1
 (−4)   = (−1) ⋅ 4 ⋅ (−1) ⋅ 2
 2 
m+2 2 ( m +1) −1
= (−1) ⋅2 ⋅2
= (−1) m ⋅ 2 2 m +1
 1/ 2 
∴ B( x) = ∑  m
(−1) ⋅ 2 2 m +1 m
x
m ≥ 0  m + 1
 1/ 2 
That is, B( x) = ∑ bn x , where bn = 
n
 

n 2 n +1
(−1) 2
n≥0  n + 1
2021/12/6 Trees 379
 1/ 2  Finally, we simplify bn .
bn =  (−1) n 2 2 n +1
 n + 1
 1  1  1  1  n 2 n +1
  − 1 − 2 ⋅
 ⋅ ⋅ − n  ⋅ (−1) ⋅ 2
2 2
=    2  2 
(n + 1)!
 1  − 1  − 3   2n − 1  n 2 n +1
    ⋅⋅⋅ −  ⋅ (−1) ⋅ 2
2 2  2   2 
=  
(n + 1) × n!
 1  1  3   2n − 1  2 n +1
    ⋅ ⋅ ⋅  ⋅2 ⋅ n!
2 2 2 2 
=     
(n + 1) × n! × n!
 1  1× 3 × 5 × ⋅ ⋅ ⋅ × ( 2n − 1) n n
 × n
× 2 × 2 × 2 × n!
2 2
= 
(n + 1) × n! × n!
1× 3 × 5 × ⋅ ⋅ ⋅ × ( 2n − 1) × ( 2 × 4 × 6 × ⋅ ⋅ ⋅ × 2n)
=
(n + 1) × n! × n!
1× 2 × 3 × ⋅ ⋅ ⋅ × 2n ( 2n)! 1  2n 
= = =   380
(n + 1) × n! × n! ( n + 1) × n! × n! n + 1  n 
(p.264)

Summary

The number of distinct binary trees with n nodes

1  2n 
= bn =  
n +1  n 

2021/12/6 Trees 381

You might also like