DSA 03 Tree Structures Eng
DSA 03 Tree Structures Eng
Contents
o Terminologies o Tree traversals o Tree
representation o Binary tree o Binary search tree
o AVL tree
o 2-3 tree, 2-3-4 tree
1
fit@hcmus | DSA | 2024 3
Some Examples
2
Organizational chart Directory tree
4
Trees
o Used to represent relationships o hierarchical in
nature
• “Parent-child” relationship exists between nodes in tree.
• Generalized to ancestor and descendant
• Lines between the nodes are called edges
3
Trees
r root
r1 r2 rk
T1 T2 Tk
subtree
Terminologies
o node: an item/element in a tree. o parent (of node n): The node
directly above node n in the tree. o child (of node n): The node
directly below node n in the tree. o root: The only node in the
tree with no parent. o leaf: A node with no children.
o path: A sequence of nodes and edges connecting a node with the
nodes below it.
4
fit@hcmus | DSA | 2024 7
Terminologies
o siblings: Nodes with common parent.
o ancestor (of node n): a node on the path from the root to n.
o descendant (of node n): a node on the path from node n to a leaf.
o subtree (of node n): A tree that consists of a child (if any) of n and
the child’s descendants.
5
fit@hcmus | DSA | 2024 9
Terminologies
o degree/order
• Order of node n: number of children of node n.
• Order of a tree: the maximum order of nodes in that tree.
o depth/level (of node n)
• If n is the root of T , it is at level 1.
• If n is not the root of T , its level is 1 greater than the level of its parent. if
node n is root:
level(n) = 1
Otherwise:
level(n) = 1 + level(parent(n))
6
10
10
Terminologies
o Height of tree: number of nodes in the longest path from the root to
a leaf.
11
Terminologies
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height (T) = max{level(Ni)}, NiT
7
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height(T) = 1 + max{height(Ti)}, Ti is a subtree of T
12
12
13
8
Kinds of Trees
14
14
General Tree
o Set T of one or more nodes such that T is partitioned into disjoint
subsets
• A single node r , the root
• Sets that are general trees, called subtrees of r
15
9
n-ary Tree
o set T of nodes that is either empty or partitioned into disjoint
subsets:
• A single node r , the root
• n possibly empty sets that are n-ary subtrees of r
16
16
Binary Tree
o Set T of nodes that is either empty or partitioned into disjoint
subsets
• Single node r , the root
• Two possibly empty sets that are binary trees, called left and right subtrees
of r
10
17
Traversals
18
18
Traversal
o Visit each node in a tree exactly once.
11
• In-order
• Post-order
19
Pre-order Traversal
PreOrder(root)
{ if root is empty
Do_nothing;
Visit root; //Print, Add, ...
//Traverse every Childi.
PreOrder(Child0);
PreOrder(Child1);
…
PreOrder(Childk-1);
}
20
20
Post-order Traversal
PostOrder(root)
{ if root is empty
Do_nothing;
//Traverse every Childi
12
PostOrder(Child0);
PostOrder(Child1);
…
PostOrder(Childk-1);
Visit at root; //Print, Add, ...
}
21
In-order Traversal
InOrder(root)
{ if root is empty
Do_nothing;
//Traverse the child at the first position
InOrder(Child0);
Visit at root;
//Traverse other children
InOrder(Child1);
InOrder(Child2);
…
InOrder(Childk-1);
}
22
22
13
Traversals
23
14
24
24
Examples
In-order
Pre-order
• abdeijcfgkh
• dbiejafckgh
Post-order
•dijebfkghca
15
fit@hcmus | DSA | 2024 25
25
Examples
26
26
16
Tree Representation
27
28
28
17
fit@hcmus | DSA | 2024 29
29
Tree Representation
Info Eldest Child Next Sibling
1 a 2 0
2 b 4 3
3 c 6 0
4 d 0 5
5 e 9 0
6 f 0 7
7 g 11 8
8 h 0 0
9 i 0 10
10 j 0 0
18
11 k 0 0
30
30
Tree Representation
A
Root
B C
D E F G H
I J K
31
19
Tree Representation
Info Parent
1 a 0
2 b 1
3 c 1
4 d 2
5 e 2
6 f 3
7 g 3
8 h 3
9 i 5
10 j 5
11 k 7
32
32
Binary Tree
33
Binary Tree
o Set T of nodes that is either empty or
partitioned into disjoint subsets.
• Single node r, the root
fit@hcmus | DSA | 2024
20
• Two possibly empty sets that are binary trees, called left and right subtrees of
r.
o Other definition: A rooted binary tree has a root node and every
node has at most two children.
34
34
Types of Binary
tree
o Heap
35
21
Perfect Binary Tree
o A perfect binary tree is a binary tree in which
• all interior nodes have two children
• and all leaves have the same depth or same level.
o In a perfect binary tree of height h, all nodes that are at a level less
than h have two children each.
36
36
22
Perfect Binary Tree
o If T is empty, T is a perfect binary tree of height 0.
o If T is not empty and has height h > 0, T is a perfect binary tree if its
root’s subtrees are both perfect binary trees of height h – 1.
a
b c
d e f g
37
23
38
38
39
24
o A full binary tree is either:
• A single vertex.
• A tree whose root node has two subtrees, both of which are
full binary trees.
40
40
Heap
o A heap is a complete binary tree that
either is empty or
• Its root
• (Max-heap): Contains a value greater than or
equal to the value in each of its children, and
• (Min-heap): Contains a value less than or
equal to the value in each of its children, and
• Has heaps as its subtrees
41
25
Number of Nodes
Given a binary tree T height of h. o What is
42
42
Height of Tree
Given a binary tree T with n nodes. o What
26
43
45
45
27
fit@hcmus | DSA | 2024 46
46
47
47
28
Binary Search Tree
48
Operations
o Insert (a key) o Search (a key) o
Remove (a key) o Traverse o Sort
(based on key value) o Rotate (Left
rotation, Right rotation)
49
29
49
Insertion
Insert (root, Data)
{ if (root is NULL) {
Create a new_Node containing Data
This new_Node becomes root of the tree
}
//Compare root's key with Key if
root's Key is less than Data's Key
Insert Key to the root's RIGHT subtree
else if root's Key is greater than Data's Key
Insert Key to the root's LEFT subtree
else
Do nothing //Explain why?
}
50
Insertion
Beginning with an empty binary search tree, what binary search tree is
formed when you insert the following values in the order given?
15, 5, 12, 8, 23, 1, 17, 21
15, 20, 40, 25, 70, 90, 80, 55, 60, 65, 30, 75
30
9, 1, 4, 2, 3, 9, 5, 8, 6, 7, 4
51
51
Insertion
Beginning with an empty binary search tree, what binary search tree is
formed when you insert the following values in the order given?
• W, T, N, J, E, B, A
• W, T, N, A, B, E, J
• A, B, W, J, N, T, E
• B, T, E, A, N, W, J
52
Search
Search (root, Data)
{ if (root is NULL) { return
NOT_FOUND;
}
31
//Compare root's key with Key if
root's Key is less than Data's Key
Search Data in the root's RIGHT subtree
else if root's Key is greater than Data's Key
Search Data in the root's LEFT subtree
else return FOUND //Explain why? }
53
53
Deletion
o When we delete a node, three possibilities arise.
o Node to be deleted:
• is leaf:
• Simply remove from the tree.
• has only one child:
• Copy the child to the node and delete the child • has two
children:
• Find in-order successor (predecessor) S_Node of the node. •
Copy contents of S_Node to the node and delete the S_Node.
54
32
Traversals
o Pre-order:
Node - Left - Right
o In-order:
Left - Node - Right
o Post-order:
Left - Right - Node
55
55
Traversals
What are the pre-order, in-order and post-order traversals of this binary
search tree?
fit@hcmus | DSA | 202456
33
56
57
57
34
Left Rotation
P Left rotation at P P
A B
B A
T1 T3
T2 T3 T1 T2
58
Left Rotation
35
59
59
Right Rotation
Right rotation at P
P P
B A
A B
T3 T1
T1 T2 T2 T3
60
36
40
37 50
36 55
45
65
61
61
37
62
63
63
38
fit@hcmus | DSA | 2024 64
64
AVL Tree
65
65
AVL Tree
o Named for its inventors, (Georgii) Adelson-Velsky and (Evgenii) Landis
39
o AVL Tree is a self-balancing binary search tree where
• for ALL nodes, the difference between height of the left subtrees and the right
subtrees cannot be more than one. (height invariant, or balance invariant).
66
AVL Tree
40
Which is the AVL Tree?
67
67
AVL Tree
o A balanced binary search tree
• Maintains height close to the minimum
41
• After insertion or deletion, check the tree is still AVL tree – determine
whether any node in tree has left and right subtrees whose heights differ by
more than 1
68
o Left-Right case
o Right-Right case
o Right-Left case
69
69
42
Cases of Height Invariant
Violation
o Left-Left case
12
8 18
5 17
70
8 18
5 17
43
71
71
72
44
73
73
45
fit@hcmus | DSA | 2024 74
74
Violation Resolving
o Right-Right case:
• Left rotation at un-balanced node.
o Right-Left case:
• Right rotation at un-balanced node’s right child.
• Left rotation at un-balanced node.
75
75
46
Violation
Resolving o Right-
Right case:
P Left rotation at P P
a b
Q
b a
T3 h+1
T1 h
T2 h T3 h+1 T1 h T2 h
76
47
Violation
Resolving o Right-
77
Left case:
• Right rotation at Q P
• Left rotation at P
a
Q
b
T1 h
c h
T4
48
Violation
Resolving o Right-
78
79
Left case:
49
Violation
Resolving o Right-
80
50
Violation
Resolving o Right-
81
81
51
fit@hcmus | DSA | 2024 82
82
Violation Resolving
o Left-Left case:
Single rotation
• Right rotation at un-balanced node.
o Left-Right case:
Double rotation
• Left rotation at un-balanced node’s left child.
• Right rotation at un-balanced node.
52
83
83
Violation
Resolving
Source: Wikipedia
84
Insertion
Beginning with an empty AVL tree, perform step-by-step the insertion
of the following values in the order given
15, 5, 12, 8, 23, 1, 17, 21
9, 1, 4, 2, 3, 9, 5, 8, 6, 7, 4
53
85
85
Red-Black Tree
87
Red-Black Tree
o Invented in 1972 by Rudolf Bayer.
o Red-Black tree is a binary search tree with the following rules:
• Every node has a color either red or black.
• The root of the tree is always black.
• There are no two adjacent red nodes (A red node cannot have a red parent
or red child).
• Every path from a node (including root) to any of its descendants NULL
nodes has the same number of black nodes.
• All leaf nodes are black nodes.
88
88
54
Red
-Black Tree
89
55
AA Tree
90
90
56
AA Tree
o Invented by Arne Anderson in a work published in 1993 (Balanced
Search Tree Made Simple).
o Two concepts:
• Level:
• Number of LEFT links from that nodes to a NULL node.
• Horizontal link:
• The link between parent and its child node having the same level.
91
AA Tree
15
Level 2
Level 1 5 10 20
Horizontal link
57
92
92
AA Tree
o AA tree is a binary search tree with the following rules:
• The level of every leaf node is one.
• The level of every left child is exactly one less than that of its parent.
• The level of every right child is equal to or one less than that of its parent.
Horizontal link must be a RIGHT link.
• The level of every right grandchild is strictly less than that of its grandparent.
There is no two consecutive horizontal links.
• Every node of level greater than one has two children.
93
AA Tree
30 70
15 50 60 85
35 40 55 65 80 90
5 10 20
58
94
94
6 27
3 4 7 8 15 40
95
59
AA Tree and -Red
Black Tree
30 70
15 50 60 85
35 40 55 65 80 90
5 10 20
96
96
60
2-3 Tree and 2-3-4 Tree
98
2-3 Tree
99
99
61
2-3-4 Tree
100
o A 3-node (has three children) must contain two data items, S and L, such
that
• S is greater than left child’s item(s) and less than middle child’s item(s);
• L is greater than middle child’s item(s) and less than right child’s item(s).
o A 4-node (has our children) must contain three data items S, M, and L that
satisfy:
• S is greater than left child’s item(s) and less than middle-left child’s item(s)
• M is greater than middle-left child’s item(s) and less than middle-right child’s item(s); •
L is greater than middle-right child’s item(s) and less than right child’s item(s).
62
101
101
2-node,-node,-node
4
3
2-node 3-node
4-node
102
63
103
103
2-3 Tree
o Invented by John Hopcroft in 1970.
• Leaves have no children and may contain either one or two data items.
104
2-3-4 Tree
o 2-3-4 tree is a tree in which
• Every internal node is a 2-node, a 3-node or a 4-node.
• Leaves have no children and may contain either one, two or three data
items.
64
105
105
113
114
114
65
Inserting Data into
-3 Tree
a
2
115
116
fit@hcmus | DSA | 2024
66
116
67
Inserting Data into a 2-3 Tree
o Splitting an internal node in a 2-3 tree when the node is a (a) left
child; (b) right child
117
68
Examples
118
118
119
Examples
The steps for inserting 38 into the tree
(a) The located node has no room;
(b) the node splits;
(c) the resulting tree
69
Examples
120
Original tree
Resulting tree
Next: inserting 36 into the tree
fit@hcmus | DSA | 2024 121
121
70
Examples
Examples
The steps for inserting 36 into the tree
122
122
123
71
Examples
124
124
72
Inserting Data into a 2-3-4 Tree
o
o Splits 4-nodes as soon as it encounters them on the way down the tree
from the root to a leaf
125
73
Inserting Data into a 2-3-4 Tree
o
126
126
127
74
Inserting Data into a 2-3-4 Tree
o
128
128
129
75
Inserting Data into a 2-3-4 Tree
o
130
130
76
Examples
o
131
Examples
o After inserting 50 and 40 into the tree
77
Examples
o
132
133
Examples
o After inserting 80 and 15 into the tree
78
Examples
o
134
135
Examples
o The steps for inserting 100 into the tree
79
Examples
o
136
136
80
Deletion
138
139
139
81
Removing Data from
-3 Tree
a2
140
82
141
141
142
83
143
143
144
84
Examples
(a) A 2-3 tree;
(b), (c), (d), (e) the steps for removing 70;
145
145
Examples
o the resulting tree
85
146
Examples
(a), (b), (c) The steps for removing 100 from the tree; (d)
147
Examples
The steps for removing 80 from the tree
86
fit@hcmus | DSA | 2024 148
148
Examples
The steps for removing 80 from the tree
149
149
87
Removing Data from a 2-3-4
Tree
o Removal algorithm has the same beginning as removal algorithm for
a 2-3 tree
o Locate the node n that contains the item I you want to remove.
o Find I ’s in-order successor and swap it with I so that the removal
will always be at a leaf. o If leaf is either a 3-node or a 4-node,
remove I .
153
88
B-Tree
158
158
B-Tree
o Original B-Tree proposed by R. Bayer and E. McCreigh in 1972.
o A B-Tree is a specialized multi-way tree designed especially for use
on external disk.
o Improved versions of B-Trees were later proposed in 1982 by
Huddleston and Mehlhorn, and by Maier and Salveter.
o B-tree variants are used mostly today as index structures in database
applications.
159
Definition
o (Knuth’s definition) A B-tree of order m is a tree which satisfies the
following properties:
fit@hcmus | DSA | 2024
89
• Every node has at most m children.
• Every non-leaf node (except root) has at least ⌈m/2⌉ child nodes.
• The root has at least two children if it is not a leaf node.
• A non-leaf node with k children contains k − 1 keys.
• All leaves appear in the same level and carry no information.
162
162
163
90
Example
Is this a B-
tree?
164
164
Height of B-Tree
• The maximum number of keys in a B-tree of order m and height h:
root m–1
level 2 m(m – 1)
level 3 m2(m – 1)
. . .
level h mh-1(m – 1)
165
fit@hcmus | DSA | 2024
91
Operations
o Insert (a key) o
Remove (a key)
166
166
92
Insertion
167
Insertion
o B-tree insertion is a generalization of 2-3 tree insertion.
o Insert K into B-tree of order m.
• We find the insertion point (in a leaf) by doing a search.
• If there is room then insert K.
• Else, promote the middle key to the parent, split the node into nodes around the
middle key.
168
168
Example
o Perform step-by-step the insertion of the following values to the
initially empty B-tree order 5
fit@hcmus | DSA | 2024
93
1, 12, 8, 2, 25, 5, 14, 28, 17
169
1 12 8 2 25 5 14 28 17
Example
1 128 12 1
2 8 2
Insert 1 Insert 12 Insert 8 Insert 2
170
170
1 12 8 2 25 5 14 28 17
Example
1 25
8
1 2 12 25
94
Insert 25
171
1 12 8 2 25 5 14 28 17
Example
1 2 5 12 14 25 28
Insert 5, 14, 28
172
172
95
1 12 8 2 25 5 14 28 17
Example
1 2 5 12 14 17 25 28
8 17
1 2 5 12 14 25 28
Insert 17
173
96
Example
Given a B-tree order 5:
Deletion
174
174
Deletion
o If the key to be deleted is not in a leaf, swap it with its successor (or
predecessor). Then delete the key from the leaf.
o If leaf contains more than the minimum number of keys, then one
can be deleted with no further action.
175
Deletion
o If the node contains the minimum number of keys, consider the two
immediate siblings of the parent node:
fit@hcmus | DSA | 2024
97
• If one of these siblings has more than the minimum
number of keys, then redistribute one key from this
sibling to the parent node, and one key from the parent
to the deficient node.
• If both immediate siblings have exactly the minimum number of keys, then merge the
deficient node with one of the immediate sibling node and one key from the parent
node.
o If this leaves the parent node with too few keys, then the process is
propagated upward.
176
176
12 29 52
2 7 9 15 22 31 43 56 69 72
Remove 2
177
Example
Given a B-tree order 5:
fit@hcmus | DSA | 2024
98
Example
Given a B-tree order 5:
12 29 52
7 9 15 22 31 43 56 69 72
After removing 2
178
178
99
Example
Given a B-tree order 5:
12 29 52
7 9 15 22 31 43 56 69 72
Remove 52
179
Example
Given a B-tree order 5:
12 29 56
7 9 15 22 31 43 52 69 72
100
Example
Given a B-tree order 5:
12 29 56
7 9 15 22 31 43 69 72
180
180
181
Example
Given a B-tree order 5:
101
Example
Given a B-tree order 5:
12 29 56
7 9 15 22 31 43 69 72
Remove 72
182
182
183
102
Example
Given a B-tree order 5:
12 29 56
7 9 15 22 31 43 69
Example
Given a B-tree order 5:
12 29
7 9 15 22 31 43 56 69
184
184
103
Example
Given a B-tree order 5:
12 29
7 9 15 22 31 43 56 69
Remove 22
185
Example
Given a B-tree order 5:
1 29
2
7 9 1 3 4 56 6
5 1 3 9
(Redistribute
) Borrow key from neighboring nodes
104
Example
Given a B-tree order 5:
186
186
187
105
Example
Given a B-tree order 5:
12 31
7 9 15 29 43 56 69
Example
Given a B-tree order 5:
16
3 8 28 48
1 2 5 7 12 14 25 26 29 45 52 53
188
188
189
fit@hcmus | DSA | 2024
106
Example
Given a B-tree order 5:
16
3 28 48
1 2 5 7 12 14 25 26 29 45 52 53
Example
Given a B-tree order 5
3 16 28 48
1 2 5 7 12 14 25 26 29 45 52 53
190
190
107
Quiz
Given the following B-tree order 5
• Remove 28, then remove 48
17
3 8 28 48
1 2 5 7 12 14 16 25 26 29 45 52 53 55 68
191
108
193
193
194
109
Questions and Answers
195
195
110