SlideShare a Scribd company logo
1 of 124
Instructor
Mr. S.Christalin Nelson
AP(SG)/SoCSE
ADVANCED DATA STRUCTURES
(Vol-2)
At a Glance
• Heap Trees
• Heap Sort
• Huffman Algorithm
• Multiway Search Trees
• B Trees
• Graphs
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
2 of 124
Overview (1/2)
• Heap Tree is a Complete Binary Tree (CBT) whose elements
have keys/values that satisfy the heap property.
• Heap Property
– The keys along any path from root to leaf is ordered either in a
increasing (Max Heap Tree) or decreasing manner (Min Heap
Tree)
• Types
– Min Heap Tree
• Value [root node] ≤ Value [both children]
– Max Heap Tree
• Value [root node] ≥ Value [both children]
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
4 of 124
Overview (2/2)
• Example
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
77
55
66
44 50
60 33
22 25
58
18
7
15
10
19 17 16
5 of 124
Max Heap Insertion (1/7)
• Procedure
– (1) Create a new child node and store the new value to it.
– (2) Connect the child to the parent satisfying the CBT property.
– (3) Compare the value of this child with its parent.
• If value[child] > value[parent], then swap the two nodes and go
to step 4.
• If value[child] < value[parent], go to step 5.
– (4) Repeat step 3 until value[child] < value[parent]
– (5) Insertion is complete
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
6 of 124
Max Heap Insertion (2/7)
• Example: Insert 35, 33, 42, 10, 14, 19, 27, 44, 26, 31, 2 into a
Binary Heap Tree. Delete 2, 44, 27 and show the final Max
Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
35
42
33
7 of 124
Max Heap Insertion (3/7)
– Swap 42 & 35.
– Insert 10, 14, 19, 27, 44
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
33
10 27
14 19
44
8 of 124
Max Heap Insertion (4/7)
– Swap 44 & 10.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
33
44 27
14 19
10
9 of 124
Max Heap Insertion (5/7)
– Swap 33 & 44.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
44
33 27
14 19
10
10 of 124
Max Heap Insertion (6/7)
– Swap 44 & 42.
– Insert 26, 31
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
44
35
42
33 27
14 19
10 26 31
11 of 124
Max Heap Insertion (7/7)
• Swap 31 & 14. Insert 2
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
44
35
42
33 27
31 19
10 26 14
• Insertion Complete
2
12 of 124
Max Heap Deletion (1/8)
• Delete 2 (Last leaf node in the CBT)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
44
35
42
33 27
31 19
10 26 14 2
13 of 124
Max Heap Deletion (2/8)
• Remove 2
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
44
35
42
33 27
31 19
10 26 14
• Delete 44 (Root node of CBT)
14 of 124
Max Heap Deletion (4/8)
• Swap 44 (root node) with 14 (last leaf node in the CBT)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
14
35
42
33 27
31 19
10 26 44
15 of 124
Max Heap Deletion (4/8)
• Remove 44.
• Compare 14 (present root) with 42 & 35.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
14
35
42
33 27
31 19
10 26
16 of 124
Max Heap Deletion (5/8)
• Swap 14 with 42 (biggest value).
• Compare 14 with 33 and 31.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
14
33 27
31 19
10 26
17 of 124
Max Heap Deletion (6/8)
• Swap 14 with 33 (biggest value).
• Compare 14 with 10 and 26.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
33
14 27
31 19
10 26
18 of 124
Max Heap Deletion (7/8)
• Swap 14 with 26 (biggest value).
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
33
26 27
31 19
10 14
• Delete 27
19 of 124
Max Heap Deletion (8/8)
• Swap 27 with 14 (last leaf node in CBT). Remove 27
• Compare 14 with 35. No change.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
42
35
33
26 14
31 19
10
20 of 124
Exercise
• Q1: Insert 13, 14, 15, 18, 11, 12, 17, 16 into a Binary Max
Heap Tree & Min Heap Tree.
• Q2: The Breadth-first traversal pattern of a Binary tree is 8,
3, 4, 1, 5, 7, 9, 2, 6, 0. Convert the tree into a Binary Max
Heap Tree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
• A1:
– Binary Max Heap Tree: 18, 16, 17, 15, 11, 12, 14, 13
– Min Heap Tree: 11, 13, 12, 16, 14, 15, 17, 18
• A2:
– Binary Max Heap Tree: 9, 6, 8, 3, 5, 7, 4, 2, 1, 0
21 of 124
Heap Sort (1/18)
• The root node of a Heap Tree is either the largest or smallest
element.
• Process
– (1) Construct Binary heap tree (max/min).
– (2) Remove the root node and store it in an array and replace
the rightmost leaf node as the new root.
– (3) Repeat steps 1 & 2 until tree is made empty. The final array
consists of sorted elements.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
23 of 124
Heap Sort (2/18)
• Example: The Breadth-First Traversal pattern of a CBT is 8, 3,
4, 1, 5, 7, 9, 2, 6, 0. Sort the elements in the CBT using Heap
Sort Algo.
– Given CBT is
– Lets convert given CBT into a Binary Max Heap Tree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
4
3
1 9
5 7
2 6 0
24 of 124
Heap Sort (3/18)
• Starting from the rightmost subtree in bottom-most level
check if Max Heap Property is satisfied.
• If Max Heap Property is not satisfied then rearrange the
elements by heapification else check next subtree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
4
3
1 9
5 7
2 6 0
8
4
3
6 9
5 7
2 1 0
25 of 124
Heap Sort (4/18)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
4
3
6 9
5 7
2 1 0
8
9
3
6 4
5 7
2 1 0
26 of 124
Heap Sort (5/18)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
9
3
6 4
5 7
2 1 0
8
9
6
3 4
5 7
2 1 0
27 of 124
Heap Sort (6/18)
• Binary Max Heap Tree is constructed
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
9
6
3 4
5 7
2 1 0
9
8
6
3 4
5 7
2 1 0
28 of 124
Heap Sort (7/18)
• Remove the root node. The last leaf node is shifted/made
the new root node of the CBT.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
9
8
6
3 4
5 7
2 1 0
0
8
6
3 4
5 7
2 1
• Sorted Array {9}
29 of 124
Heap Sort (8/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
0
8
6
3 4
5 7
2 1
8
7
6
3 4
5 0
2 1
• Sorted Array {9}
30 of 124
Heap Sort (9/18)
• Remove the root node. The last leaf node is shifted/made
the new root node of the CBT.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
8
7
6
3 4
5 0
2 1
1
7
6
3 4
5 0
2
• Sorted Array {9, 8}
31 of 124
Heap Sort (10/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
1
7
6
3 4
5 0
2
7
4
6
3 1
5 0
2
• Sorted Array {9, 8}
32 of 124
Heap Sort (11/18)
• Remove the root node. The last leaf node is shifted/made
the new root node of the CBT.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
7
4
6
3 1
5 0
2
2
4
6
3 1
5 0
• Sorted Array {9, 8, 7}
33 of 124
Heap Sort (12/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
2
4
6
3 1
5 0
6
4
5
3 1
2 0
• Sorted Array {9, 8, 7, 6}
1
4
5
3 2 0
• Remove the root node. The last leaf node is
shifted/made the new root node of the CBT.
34 of 124
Heap Sort (13/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
1
4
5
3 2 0
5
4
3
1 2 0
• Sorted Array {9, 8, 7, 6, 5}
0
4
3
1 2
• Remove the root node. The last leaf node is
shifted/made the new root node of the CBT.
35 of 124
Heap Sort (14/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
0
4
3
1 2
4
0
3
1 2
• Sorted Array {9, 8, 7, 6, 5, 4}
2
0
3
1
• Remove the root node. The last leaf node is
shifted/made the new root node of the CBT.
36 of 124
Heap Sort (15/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
2
0
3
1
3
0
2
1
• Sorted Array {9, 8, 7, 6, 5, 4, 3}
1
0
2
• Remove the root node. The last leaf node is
shifted/made the new root node of the CBT.
37 of 124
Heap Sort (16/18)
• Convert CBT to binary Max Heap Tree.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
1
0
2
2
0
1
• Sorted Array {9, 8, 7, 6, 5, 4, 3, 2}
0
1
• Remove the root node. The last leaf node is
shifted/made the new root node of the CBT.
38 of 124
Heap Sort (17/18)
• Convert CBT to binary Max Heap Tree. Remove the root
node. The last leaf node is shifted/made the new root node
of the CBT.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
0
1
1
0
• Sorted Array {9, 8, 7, 6, 5, 4, 3, 2, 1}
0
• The CBT is a Singleton Tree. The root node is copied to array
and the Final Sorted Array {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
39 of 124
Heap Sort (18/18)
• Note:
– Alternatively, instead of removing root node from heap tree, it
can be swapped with the rightmost leaf and retained in the
tree. This node should not be considered for further
operations until the sorting procedure is completed.
– Then, the breadth-first traversal pattern would be 0, 1, 2, 3, 4,
5, 6, 7, 8, 9
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
0
2
1
3 6
4 5
7 8 9
40 of 124
Overview (1/2)
• Lossless data compression algorithm that assigns a variable-
length unique code (also called bit string or binary string or
code or codeword) to each symbol or character in the input
data based on its frequency of occurrence.
– i.e. The most frequent symbol has the shortest code.
– E.g. Consider a string “ACCEBFFFFAAXXBLKE”
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Character Frequency Code
A 3 111
B 2 100
C 2 101
E 2 001
F 4 01
K 1 0000
L 1 0001
X 2 110
42 of 124
Overview (2/2)
• Huffman code is a prefix code (or) prefix-free code
– i.e. The code representing a particular symbol is never a prefix
of the code representing any other symbol
– Example
• {01, 10, 0010, 1111} is prefix free
• {01, 10, 0010, 1010} is not prefix free because 10 is a prefix of
1010.
– Every message encoded by a prefix free code is uniquely
decipherable
• Variable-Length (prefix) code uses the least space when
compared to the Fixed-length code
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
43 of 124
Activity (1/2)
• (Q1) Suppose we want to store messages made up of 4
characters a, b, c, d with frequencies 60, 5, 30, 5 (percents)
respectively. What are the fixed-length codes and prefix-free
codes that use the least space?
– To store 100 of these characters,
• Fixed-length code requires 100×2 = 200 bits
• Prefix code requires (60×1) + (5×3) + (30×2) + (5×3) = 150 bits
– Prefix-free codes use least space & offers a 25% saving.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Characters a b c d
Frequency 60 5 30 5
Fixed-Length Code 00 10 10 11
Variable-length code (prefix code) 0 110 10 111
44 of 124
Activity (2/2)
• (Q2) Code is {a=0, b=110, c=10, d=111}. Check if the message
“01101100” is uniquely decipherable.
– 01101100 => 01101100 => 01101100 => 01101100 => “abba”
• The given codes are of variable length & are prefix-free codes. So,
the given message is uniquely decipherable to “abba”. Hence,
{a=0, b=110, c=10, d=111} is a Huffman code.
• (Q3) Code is {a=1, b=110, c=10, d=111}. Check if the message
“1101111” is uniquely decipherable.
– 1101111 => 1101111 => “acad”
– 1101111 => 1101111 => “bad”
• Though the given codes are of variable length, still they are not
prefix-free codes. So, the given message is not uniquely
decipherable as we get two messages “acad” and “bad”. Hence,
{a=1, b=110, c=10, d=111} is not a Huffman code.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
45 of 124
Huffman Algorithm (1/15)
• Huffman Algorithm is used to find the Huffman code
through construction of a Huffman Tree.
• Procedure
– (1) Create a leaf node for each unique character and build a
min heap of all leaf nodes.
– (2) Extract 2 nodes with the minimum frequency from the min
heap
– (3) Create a new internal node with frequency equal to the
sum of the 2 node frequencies. Make the first extracted node
as its left child and the other extracted node as its right child.
Add this node to the min heap.
– (4) Repeat steps 2&3 until the heap contains only one node.
The remaining node is the root node and the tree is complete.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
46 of 124
Huffman Algorithm (2/15)
• (Q) Suppose we want to store messages made up of 6
characters a, b, c, d, e, f with frequencies 5, 9, 12, 13, 16, 45
respectively. Find the Huffman code.
– Arrange the characters in ascending order of its frequencies
– Construct a min-heap tree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Characters a b c d e f
Frequency 5 9 12 13 16 45
a/5
b/9 c/12
d/13 e/16 f/45
47 of 124
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
b/9
d/13 c/12
f/45 e/16
Remove 9
e/16
d/13 c/12
f/45
To min-heap
c/12
d/13 e/16
f/45
a/5
b/9 c/12
d/13 e/16 f/45
f/45
b/9 c/12
d/13 e/16
b/9
d/13 c/12
f/45 e/16
Remove 5 To min-heap
Extract 2 nodes with smallest frequency.
48 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Create new internal node (N1)
• Frequency (N1) = Sum of freq. of the 2 extracted nodes = 5+9 = 14
• Create a subtree with node (N1) as root, 1st extracted node as left child
& 2nd extracted node as right child.
• Insert N1 into the min heap tree
Characters a b c d e f
Frequency 5 9 12 13 16 45
N1/14
b/9
a/5
c/12
d/13 e/16
f/45 N1/14
c/12
d/13 e/16
f/45
Insert N1
It’s a Min
Heap Tree
49 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Remove 13 To min-heap
Remove 12 To min-heap
Extract 2 nodes with smallest frequency.
c/12
d/13 e/16
f/45 N1/14
N1/14
d/13 e/16
f/45
d/13
N1/14 e/16
f/45
d/13
N1/14 e/16
f/45
f/45
N1/14 e/16
N1/14
f/45 e/16
50 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Create new internal node (N2)
• Frequency (N2) = Sum of freq. of the 2 extracted nodes = 12+13 = 25
• Create a subtree with node (N2) as root, 1st extracted node as left child
& 2nd extracted node as right child.
• Insert N2 into the min heap tree
Characters a b c d e f
Frequency 5 9 12 13 16 45
N2/25
d/13
c/12
N1/14
f/45 e/16
N2/25
To min-heap
N1/14
N2/25 e/16
f/45
Insert N2
N1/14
f/45 e/16
51 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Extract 2 nodes with smallest frequency.
N1/14
N2/25 e/16
f/45
Remove 14 f/45
N2/25 e/16
To min-heap e/16
N2/25 f/45
Remove 16 To min-heap
e/16
N2/25 f/45
f/45
N2/25
N2/25
f/45
52 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Create new internal node (N3)
• Frequency (N3) = Sum of freq. of the 2 extracted nodes = 14+16 = 30
• Create a subtree with node (N3) as root, 1st extracted node as left child
& 2nd extracted node as right child.
• Insert N3 into the min heap tree
Characters a b c d e f
Frequency 5 9 12 13 16 45
N3/30
e/16
N1/14
Insert N3
N2/25
f/45
N2/25
f/45 N3/30
It’s a Min
Heap Tree
53 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Extract 2 nodes with smallest frequency.
N2/25
f/45 N3/30
Remove 25 Remove 30
N3/30
f/45
It’s a Min
Heap Tree
f/45
It’s a Min
Heap Tree
Create new internal node (N4)
• Frequency (N4) = Sum of freq. of the 2 extracted nodes = 25+30 = 55
• Create a subtree with node (N4) as root, 1st extracted node as left child
& 2nd extracted node as right child.
• Insert N4 into the min heap tree
N4/55
N3/30
N2/25
f/45
Insert N4
f/45
N4/55
It’s a Min
Heap Tree
54 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Extract 2 nodes with smallest frequency.
f/45
N4/55
Remove 45 Remove 55
It’s a Min
Heap Tree
N4/55
Empty
Create new internal node (N5)
• Frequency (N5) = Sum of freq. of the 2 extracted nodes = 45+55 = 100
• Create a subtree with node (N5) as root, 1st extracted node as left child
& 2nd extracted node as right child.
• Insert N5 into the Empty tree as root node
N5/100
N4/55
f/45
Empty N5/100
Insert N5
55 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
The existing internal nodes are:
N1/14
b/9
a/5
N2/25
d/13
c/12
N3/30
e/16
N1/14
N4/55
N3/30
N2/25
N5/100
N4/55
f/45
N5/100
Fit internal nodes into tree:
N5/100
f/45 N4/55
N2/25
c/12 d/13
N3/30
N1/14
a/5 b/9
e/16
56 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
The generated Huffman Tree is:
N5/100
f/45 N4/55
N2/25
c/12 d/13
N3/30
N1/14
a/5 b/9
e/16
It’s a Full Tree
57 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
For every parent, consider ‘0’ for Left Child traversal & ‘1’ for Right child traversal
N5/100
f/45 N4/55
N2/25
c/12 d/13
N3/30
N1/14
a/5 b/9
e/16
0
0
0
0
0
1
1
1
1
1
58 of 122
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Find code for each character by traversal from root
N5/100
f/45 N4/55
N2/25
c/12 d/13
N3/30
N1/14
a/5 b/9
e/16
0
0
0
0
0
1
1
1
1
1
Characters a b c d e f
Frequency 5 9 12 13 16 45
Variable-length code (prefix code) 1100 1101 100 101 111 0
59 of 122
Huffman Algorithm (15/15)
• (Q) Suppose we want to store messages made up of 6
characters a, b, c, d, e with frequencies 20, 15, 5, 15, 45
respectively. Find Huffman tree and Huffman code.
– Huffman Tree
– Huffman Code
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Characters a b c d e
Variable-length code (prefix code) 000 001 010 011 1
60 of 124
m-way search tree (1/3)
• A tree whose nodes can have certain no. of children (≥2) and
satisfy a search property (i.e. like BST) is called multi-way or
m-way search tree.
• Value of ‘m’ specifies
– Upper limit (max. no.) of subtrees/children per node.
– Upper limit (max. no.) of keys stored per node. i.e. (m-1) keys.
• Application of m-way search tree to speedup processing
– ‘m’ can be a very large number
• Each node corresponds to a physical block on disk.
• Moving one node to another (i.e. moving m-1 data items or a
disk block) involves reading a disk block. This process is very slow
when compared to moving around a data structure stored in
memory.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
62 of 124
m-way search tree (2/3)
• Properties
– All keys of a node are stored in ascending order.
• Children between keys k1 & k2 contains all keys in range k1 to k2.
– Each node has 1 to (m-1) keys.
• i.e. 2 to m children/subtrees.
– No. of non-empty subtrees = 0 (for a leaf) to 1+(no. of keys).
– Root has at least two subtrees (or store one key) unless it is a
singleton tree.
• Advantages
– m-way search trees give the same advantages to m-way trees
that BST gave to binary trees - they provide fast information
retrieval and update.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
63 of 124
m-way search tree (3/3)
• Disadvantage
– M-way search trees have the same problem that BST had - i.e.
they can become unbalanced.
– E.g.: 4-way Search Tree
• Note: B-Tree of order m (m-way search tree) is used instead.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
64 of 124
B-Trees (1/5)
• B-Tree is a self-balancing search tree.
• Vs. other self-balancing search trees (like AVL)
– B-Trees can hold huge data that cannot fit in main memory.
– B-Trees reduce the number of disk accesses.
• Disk read operation is done when a block (set of keys) is
unavailable in main memory. It is a very slow operation.
• Most of the tree operations require O(h) disk accesses.
– Height (h) of B-Trees is kept low by putting maximum possible keys
in a B-Tree node (approx. equal to the disk block size).
• Height of B-tree = 0.5log2n to log2n
– Where, n - no. of children
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
66 of 124
B-Trees (2/5)
• Defined by
– No. of keys per node (x) & no. of children (y). Hence also called
as a “x-y B-Tree”.
• E.g. In 2-3 B-Tree, every internal node is either a 2 node (has 1
key and 2 children) or 3 node (has 2 keys and 3 children).
• E.g. In 2-4 B-Tree, every internal node is either a 2/3/4 node. Also
called 2-3-4 tree.
– Order (determine the no. of subtrees/children)
• E.g. 2-3 B-Tree is of Order 3, 2-4 B-Tree is of Order 4
– Minimum degree or Branching Factor (t) or outdegree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
67 of 124
B-Trees (3/5)
• Properties
– All leaves are at same level (i.e. Balanced).
– All keys of a node are stored in ascending order
• Children between keys k1 & k2 contains all keys in range k1 to k2.
– No. of keys in a node
• At least: Root has min. 1 key, All node (except root): min.(t-1) keys.
• At most: All nodes (including root) contain max. (2t-1) keys.
– Max. no. of subtrees per node = No. of keys per node + 1.
• Vs. BST
– B-Tree grows and shrinks from root which is unlike BST. BSTs
grow downward and also shrink from downward.
– Like other balanced BSTs, time complexity to search, insert and
delete is O(log n).
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
68 of 124
B-Trees (4/5)
• Example-1
– B-Tree with minimum degree(t)=3
• All leaves are at same level
• No. of keys
– Root has 3 keys (>1)
– Nodes (except root) have at least 2 keys i.e. (t-1) keys
– All nodes (including root) may contain at most (2t-1) keys. i.e. 5
• No. of children/subtrees per node = No. of keys in node + 1.
– No. of children of the root node = 3 + 1 = 4
• All keys of a node are sorted in ascending order.
– Keys of root node 3, 30, 60 are sorted
• Subtree between keys k1 and k2 contains all keys in range from
k1 and k2.
– Between 3 and 30 of root node, the subtree contains the keys 4, 5, 6
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
69 of 124
B-Trees (5/5)
• Example-2
– 2-3 B-Tree (or) B-Tree of Order 3
(or) 3-way Tree
• Example-3
– 2-4 B-Tree (or) 2-3-4 Tree
(or) B-Tree of Order 4
(or) 4-way tree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
70 of 124
Insertion (1/12)
• Consider the following 2-4 B-Tree
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20
24
26 28 30
33
37 39 40
71 of 124
Insertion (2/12)
• Insert 21
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20
24
26 28 30
33
37 39 40
21
72 of 124
Insertion (3/12)
• Insert 23
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20 21
24
26 28 30
33
37 39 40
24
Shift 24
Save 23
73 of 124
Insertion (4/12)
• Insert 23
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20 21
23 24
26 28 30
33
37 39 40
74 of 124
Insertion (5/12)
• Insert 29
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20 21
23 24
26 28 30
33
37 39 40
26 28 29 30
Node in Level 2
already has 3 keys!!
Split Node &
Insert 29
75 of 124
Insertion (6/12)
• Insert 29
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 35
1 2
4 5 6
9
11 12
14 15
20 21
23 24 33
37 39 40
26 28 29 30
28
76 of 124
3 children in Level 2
would promote 28 to
Level 1
Insertion (7/12)
• Insert 29
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 28 35
1 2
4 5 6
9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
77 of 124
Insertion (8/12)
• Insert 7
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 28 35
1 2
4 5 6
9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
4 5 6 7 78 of 124
Node in Level 2 already
has 3 keys. Split the
node & Insert 7
Insertion (9/12)
• Insert 7 – Node in Level 2 has 5 children. Promote 5 to Level
1. Now node in Level 1 already has 3 keys. Hence Split parent
node.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 28 35
1 2 9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
4 5 6 7
3 8 10
4
3 5
79 of 124
Insertion (10/12)
• Insert 7 – Promote 5 to Level 0. Here, node already has 3
keys - split parent node.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
3 8 10 18 25 28 35
1 2 9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
6 7
3 5 8 10
4
80 of 124
Insertion (11/12)
• Insert 7 – Node in Level 1 has 2 keys and only 2 children.
Promote 5 to Level 0. At Level 0, the root node already has 3
keys - split parent node.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
18 25 28 35
1 2 9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
6 7
3 5 8 10
4
22 32
5 13
81 of 124
Insertion (12/12)
• Insert 7 – Node has 2 keys and only 2 children. Promote 13
by creating new level.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
13 22 32
18 25 28 35
1 2 9
11 12
14 15
20 21
23 24
33
37 39 40
26
29 30
6 7
3 5 8 10
4
22 32
5 13
13
82 of 124
Terminologies (1/7)
• Graph is a non-linear Data Structure
• Definition of Graph, G = (V, E)
– Set of Nodes or Vertices (V), Set of Edges (E)
• Number of Vertices = |V|, Number of Edges |E|
• Edges connect two vertices (source & destination)
• Types of Edges
– Based on Direction
• Directed edges
– Represented as ordered pair (V1, V2). Here (V1, V2) ≠ (V2, V1)
• Undirected (Bidirectional) edges
– Represented as unordered pair {V1, V2}. Here {V1, V2} = {V2, V1}
– Based on Weights
• Weighted & Unweighted edges
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
84 of 124
Terminologies (2/7)
• Directed Graph (Digraph) & Undirected Graphs
– Given graph is an undirected graph as it has undirected edges.
– Undirected graph is defined as G = (V, E)
• where
– V = {V1,V2,V3,V4,V5,V6,V7,V8}
– E = {{V1,V2},{V1,V3},{V1,V4},{V2,V5},{V2,V6},{V3,V7},{V4,V8}, …. }
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
85 of 124
Terminologies (3/7)
• Weighted & Unweighted Graphs
– All graphs with weighted (cost) edges are weighted graphs.
• Edges of a weighted graph is labelled with their corresponding
weight (cost incurred for taking the edge).
– Edges of an unweighted graph is not labelled and are
considered to have the same weight/cost (=1).
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
86 of 124
Terminologies (4/7)
• Null Graph
– A graph which contains only isolated vertices
• i.e. Set of edges in a null graph is empty. E = {}
• Multi-edge
– Many edges can be formed from a single vertex.
• Parallel Edges
– Edges have the same end nodes/vertices. i.e. Two vertices may
be connected by more than one edge.
• Multi Graph
– A graph having parallel edges.
• Trivial Graph
– A graph with only one vertex.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
87 of 124
Terminologies (5/7)
• Loop (or) Self-Loop
– An edge in a graph which is drawn from a vertex to itself.
• Simple graph
– A graph with no loops and no parallel edges.
– Max. number of edges |E|
• Simple undirected graph = n(n – 1)/2. 'n' vertices.
• Simple digraph = n(n-1), approx. equal to square of n
– Min. no. of edges = 0
– No. of simple graphs = 2(n(n-1)/2)
• Dense and Sparse Graph
• Cyclic Graph
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
88 of 124
Terminologies (6/7)
• Complete Graph
– A simple graph (G) is said to be complete if every vertex in G is
connected with every other vertex.
– A graph with ‘n’ vertex has n(n-1)/2 edges.
• Regular Graph
– A graph in which all the vertices are of equal degree.
• A regular graph of degree r is a graph with each vertex having the
degree r.
• Every null graph is a regular graph of degree zero & a complete
graph is a regular graph of degree n-1.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
89 of 124
Terminologies (7/7)
• Vs. Tree
– Tree is also an example of non-linear Data Structure.
– Tree is a special kind of graph with special rules.
• Tree with n nodes, would have (n-1) edges,
• All nodes are reachable through the root, etc.
– In Tree all nodes are connected to root in diff. breadth & depth.
• Applications
– Graph Theory
– Weighted Undirected Graph: Inter-city road map
– Unweighted Undirected Graph: Social Network
– Weighted Digraph: Intra-city road map
– Unweighted Digraph: www, web crawlers
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
90 of 124
Graph Representation
• Vertex List & Edge List
• Vertex List & Adjacency Matrix (for Dense Graphs)
• Vertex List & Adjacency List (for Sparse Graphs)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
91 of 124
Example
• (Q1) Find the DFS & BFS traversal of the following digraph
starting at node S.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
• (A1)
– DFS traversal: S, A, D, B, C
– BFS traversal: S, A, B, C, D
92 of 124
Example - Depth First Traversal (1/9)
• Additional Data Structure Used: Stack
• Starting at node with S
– DFS Sequence: S
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
S
93 of 124
Example - Depth First Traversal (2/9)
– DFS Sequence: S A
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
A
S
94 of 124
Example - Depth First Traversal (3/9)
– DFS Sequence: S A D
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
D
A
S
95 of 124
Example - Depth First Traversal (4/9)
– DFS Sequence: S A D B
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
B
D
A
S
96 of 124
Example - Depth First Traversal (5/9)
– DFS Sequence: S A D B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
C
D
A
S
97 of 124
Example - Depth First Traversal (6/9)
– DFS Sequence: S A D B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
D
A
S
98 of 124
Example - Depth First Traversal (7/9)
– DFS Sequence: S A D B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
A
S
99 of 124
Example - Depth First Traversal (8/9)
– DFS Sequence: S A D B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
S
100 of 124
Example - Depth First Traversal (9/9)
– Final DFS Sequence: S A D B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Stack
Empty
101 of 124
Example - Breadth First Traversal (1/9)
• Additional Data Structure Used: Queue
• Starting at node with S
– BFS Sequence: S
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
102 of 124
Example - Breadth First Traversal (2/9)
– BFS Sequence: S A
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
A
103 of 124
Example - Breadth First Traversal (3/9)
– BFS Sequence: S A B
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
A B
104 of 124
Example - Breadth First Traversal (4/9)
– BFS Sequence: S A B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
A B C
105 of 124
Example - Breadth First Traversal (5/9)
– BFS Sequence: S A B C
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
B C
Remove
A
106 of 124
Example - Breadth First Traversal (6/9)
– BFS Sequence: S A B C D
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
B C D
107 of 124
Example - Breadth First Traversal (7/9)
– BFS Sequence: S A B C D
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
C D
Remove
B
108 of 124
Example - Breadth First Traversal (8/9)
– BFS Sequence: S A B C D
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
D
Remove
C
109 of 124
Example - Breadth First Traversal (9/9)
– Final BFS Sequence: S A B C D
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Queue
Empty
Remove
D
110 of 124
Activity (1/2)
• Q2. Find the DFS & BFS traversal of the following digraph
starting at node S.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
• A2
– DFS traversal: S, A, D, G, E, B, F, C
– BFS traversal: S, A, B, C, D, E, F, G
111 of 124
Activity (2/2)
• Q3. Find the DFS & BFS traversal of the following digraph
starting at node A.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
• A3
– DFS traversal: A, B, D, C, F, G, E
– BFS traversal: A, B, C, D, E, F, G
Weights are
ignored !!
112 of 124
Spanning Tree (1/2)
• A subgraph or tree that spans all vertices of a connected &
undirected graph
• Maximum no. of Spanning Trees for a graph is V(V-2)
– Here V = no. of vertices in the graph
• Example:
– Find the maximum no. of spanning trees for the given graph.
Also draw them.
• V = 3. Hence the given graph has 3(3-2) = 3 spanning trees.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
113 of 124
Spanning Tree (2/2)
• Properties of Spanning Tree
– Maximum no. of Spanning Trees for a graph = V(V-2)
– Spanning tree for a graph G = (V, E) has of V vertices and (V-1)
edges after removal of only (E-V+1) edges.
• Example: Consider a graph with V=4, E=6.
– After removal of 3 edges (E-V+1 = 6-4+1) from graph, a spanning
tree would have 4 vertices & 3 edges (V-1 = 4-1)
– Should be maximally acyclic. i.e. Adding a single edge to a
spanning tree would make it a Acyclic graph.
– Should be minimally connected. i.e. Removal of a single edge
from a spanning tree would give a disconnected graph.
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
114 of 124
Minimum Cost Spanning Tree
• A Spanning Tree which is made of minimum cost edges & has
a minimum total cost.
• Algorithms
– Kruskal’s Algorithm
– Prim’s Algorithm
• Basic Initial steps
– Remove Self-loops
– Remove Parallel edges (Retain the edge with least weight)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
115 of 124
Kruskal’s Spanning Tree Algorithm (1/4)
• (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm
– Draw Edge Table (Edge in ascending order of their weight)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
10
24
12
22
28
25 16
14
18
Edge AB CD FG CF DG EG DE BE AC
Weight 10 12 14 16 18 22 24 25 28
A
B
E
D
C
F
G
2 Edges have same
weight
Take both edges &
consider any order
116 of 124
Kruskal’s Spanning Tree Algorithm (2/4)
• (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
10
24
12
22
28
25 16
14
18
10
12
22
25 16
14
18
24
Edge AB CD FG CF DG EG DE BE AC
Weight 10 12 14 16 18 22 24 25 28
Start from smallest weight edge & keep selecting edges that
does not form any circuit with previously selected edges
A
B
E
D
C
F
G
A
B
E
D
C
F
G
B
117 of 124
Kruskal’s Spanning Tree Algorithm (3/4)
• (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
MST
Cost = 99
10
24
12
22
28
25 16
14
18
10
12
22
25 16
14
A
B
E
D
C
F
G
A
E
D
C
F
G
B
118 of 124
Kruskal’s Spanning Tree Algorithm (4/4)
• (Q2) Find the MST using Kruskal’s Spanning Tree Algorithm
by starting at S
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Cost of MST = 17
119 of 124
Prim’s Spanning Tree Algorithm (1/4)
• (Q1) Find the MST using Prim’s Spanning Tree Algorithm
– Draw Table (V x V)
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
10
24
12
22
28
25 16
14
18
A
E
D
C
F
G
B
A B C D E F G
A 0 10 28 ∞ ∞ ∞ ∞
B 10 0 ∞ ∞ 25 ∞ ∞
C 28 ∞ 0 12 ∞ 16 ∞
D ∞ ∞ 12 0 24 ∞ 18
E ∞ 25 ∞ 24 0 ∞ 22
F ∞ ∞ 16 ∞ ∞ 0 14
G ∞ ∞ ∞ 18 22 14 0
120 of 124
Prim’s Spanning Tree Algorithm (2/4)
• (Q1) Find the MST using Prim’s Spanning Tree Algorithm
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
10
12
22
25 16
14
A
B
E
D
C
F
G
A B C D E F G
A 0 10 28 ∞ ∞ ∞ ∞
B 10 0 ∞ ∞ 25 ∞ ∞
C 28 ∞ 0 12 ∞ 16 ∞
D ∞ ∞ 12 0 24 ∞ 18
E ∞ 25 ∞ 24 0 ∞ 22
F ∞ ∞ 16 ∞ ∞ 0 14
G ∞ ∞ ∞ 18 22 14 0
18
24
28
121 of 124
Prim’s Spanning Tree Algorithm (3/4)
• (Q1) Find the MST using Prim’s Spanning Tree Algorithm
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
10
24
12
22
28
25 16
14
18
MST Cost = 99
A
E
D
C
F
G
B
10
12
22
25 16
14
A
B
E
D
C
F
G
18
24
28
122 of 124
Prim’s Spanning Tree Algorithm (4/4)
• (Q2) Find the MST using Prim’s Spanning Tree Algorithm by
starting at node S
23-Apr-22
Data Structures
Instructor: Mr.S.Christalin Nelson
Cost of MST = 17
123 of 124
Advanced Data Structures - Vol.2

More Related Content

What's hot

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Overview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfOverview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfChristalin Nelson
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 
Introduction to Cassandra Basics
Introduction to Cassandra BasicsIntroduction to Cassandra Basics
Introduction to Cassandra Basicsnickmbailey
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Aaron Shilo
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Database index
Database indexDatabase index
Database indexRiteshkiit
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013John Beresniewicz
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
Shell sorting
Shell sortingShell sorting
Shell sortingTUC
 
Yzm 2116 Bölüm 9 - Heap Binary Tree
Yzm 2116   Bölüm 9 - Heap Binary TreeYzm 2116   Bölüm 9 - Heap Binary Tree
Yzm 2116 Bölüm 9 - Heap Binary TreeDeniz KILINÇ
 

What's hot (20)

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Overview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfOverview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdf
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Computer Fundamentals-2
Computer Fundamentals-2Computer Fundamentals-2
Computer Fundamentals-2
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Introduction to Cassandra Basics
Introduction to Cassandra BasicsIntroduction to Cassandra Basics
Introduction to Cassandra Basics
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Database index
Database indexDatabase index
Database index
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Hash tables
Hash tablesHash tables
Hash tables
 
Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Queue
QueueQueue
Queue
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Yzm 2116 Bölüm 9 - Heap Binary Tree
Yzm 2116   Bölüm 9 - Heap Binary TreeYzm 2116   Bölüm 9 - Heap Binary Tree
Yzm 2116 Bölüm 9 - Heap Binary Tree
 

Similar to Advanced Data Structures - Vol.2

machine learning - Clustering in R
machine learning - Clustering in Rmachine learning - Clustering in R
machine learning - Clustering in RSudhakar Chavan
 
Density based methods
Density based methodsDensity based methods
Density based methodsSVijaylakshmi
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplicationAustin Benson
 
3b318431-df9f-4a2c-9909-61ecb6af8444.pptx
3b318431-df9f-4a2c-9909-61ecb6af8444.pptx3b318431-df9f-4a2c-9909-61ecb6af8444.pptx
3b318431-df9f-4a2c-9909-61ecb6af8444.pptxNANDHINIS900805
 
DATA MINING:Clustering Types
DATA MINING:Clustering TypesDATA MINING:Clustering Types
DATA MINING:Clustering TypesAshwin Shenoy M
 
Advanced database and data mining & clustering concepts
Advanced database and data mining & clustering conceptsAdvanced database and data mining & clustering concepts
Advanced database and data mining & clustering conceptsNithyananthSengottai
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMDATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMABIRAMIS87
 
CLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptxCLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptxShwetapadmaBabu1
 
Understanding DLmalloc
Understanding DLmallocUnderstanding DLmalloc
Understanding DLmallocHaifeng Li
 
ensembles_emptytemplate_v2
ensembles_emptytemplate_v2ensembles_emptytemplate_v2
ensembles_emptytemplate_v2Shrayes Ramesh
 
fast-matmul-ppopp2015
fast-matmul-ppopp2015fast-matmul-ppopp2015
fast-matmul-ppopp2015Austin Benson
 
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Parinda Rajapaksha
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 

Similar to Advanced Data Structures - Vol.2 (20)

machine learning - Clustering in R
machine learning - Clustering in Rmachine learning - Clustering in R
machine learning - Clustering in R
 
fast-matmul-cse15
fast-matmul-cse15fast-matmul-cse15
fast-matmul-cse15
 
Lec34
Lec34Lec34
Lec34
 
Density based methods
Density based methodsDensity based methods
Density based methods
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplication
 
3b318431-df9f-4a2c-9909-61ecb6af8444.pptx
3b318431-df9f-4a2c-9909-61ecb6af8444.pptx3b318431-df9f-4a2c-9909-61ecb6af8444.pptx
3b318431-df9f-4a2c-9909-61ecb6af8444.pptx
 
DATA MINING:Clustering Types
DATA MINING:Clustering TypesDATA MINING:Clustering Types
DATA MINING:Clustering Types
 
Advanced database and data mining & clustering concepts
Advanced database and data mining & clustering conceptsAdvanced database and data mining & clustering concepts
Advanced database and data mining & clustering concepts
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMDATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
 
CLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptxCLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptx
 
Understanding DLmalloc
Understanding DLmallocUnderstanding DLmalloc
Understanding DLmalloc
 
08 clustering
08 clustering08 clustering
08 clustering
 
ensembles_emptytemplate_v2
ensembles_emptytemplate_v2ensembles_emptytemplate_v2
ensembles_emptytemplate_v2
 
Sandia Fast Matmul
Sandia Fast MatmulSandia Fast Matmul
Sandia Fast Matmul
 
fast-matmul-ppopp2015
fast-matmul-ppopp2015fast-matmul-ppopp2015
fast-matmul-ppopp2015
 
kmean clustering
kmean clusteringkmean clustering
kmean clustering
 
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
 
Machine Learning - Clustering
Machine Learning - ClusteringMachine Learning - Clustering
Machine Learning - Clustering
 
Lec33
Lec33Lec33
Lec33
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 

More from Christalin Nelson (15)

Packages and Subpackages in Java
Packages and Subpackages in JavaPackages and Subpackages in Java
Packages and Subpackages in Java
 
Bitwise complement operator
Bitwise complement operatorBitwise complement operator
Bitwise complement operator
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Process Management
Process ManagementProcess Management
Process Management
 
Storage system architecture
Storage system architectureStorage system architecture
Storage system architecture
 
Data Storage and Information Management
Data Storage and Information ManagementData Storage and Information Management
Data Storage and Information Management
 
Application Middleware Overview
Application Middleware OverviewApplication Middleware Overview
Application Middleware Overview
 
Network security
Network securityNetwork security
Network security
 
Directory services
Directory servicesDirectory services
Directory services
 
System overview
System overviewSystem overview
System overview
 
Storage overview
Storage overviewStorage overview
Storage overview
 
Database overview
Database overviewDatabase overview
Database overview
 
Computer Fundamentals - 1
Computer Fundamentals - 1Computer Fundamentals - 1
Computer Fundamentals - 1
 

Recently uploaded

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Recently uploaded (20)

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 

Advanced Data Structures - Vol.2

  • 2. At a Glance • Heap Trees • Heap Sort • Huffman Algorithm • Multiway Search Trees • B Trees • Graphs 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 2 of 124
  • 3.
  • 4. Overview (1/2) • Heap Tree is a Complete Binary Tree (CBT) whose elements have keys/values that satisfy the heap property. • Heap Property – The keys along any path from root to leaf is ordered either in a increasing (Max Heap Tree) or decreasing manner (Min Heap Tree) • Types – Min Heap Tree • Value [root node] ≤ Value [both children] – Max Heap Tree • Value [root node] ≥ Value [both children] 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 4 of 124
  • 5. Overview (2/2) • Example 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 77 55 66 44 50 60 33 22 25 58 18 7 15 10 19 17 16 5 of 124
  • 6. Max Heap Insertion (1/7) • Procedure – (1) Create a new child node and store the new value to it. – (2) Connect the child to the parent satisfying the CBT property. – (3) Compare the value of this child with its parent. • If value[child] > value[parent], then swap the two nodes and go to step 4. • If value[child] < value[parent], go to step 5. – (4) Repeat step 3 until value[child] < value[parent] – (5) Insertion is complete 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 6 of 124
  • 7. Max Heap Insertion (2/7) • Example: Insert 35, 33, 42, 10, 14, 19, 27, 44, 26, 31, 2 into a Binary Heap Tree. Delete 2, 44, 27 and show the final Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 35 42 33 7 of 124
  • 8. Max Heap Insertion (3/7) – Swap 42 & 35. – Insert 10, 14, 19, 27, 44 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 33 10 27 14 19 44 8 of 124
  • 9. Max Heap Insertion (4/7) – Swap 44 & 10. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 33 44 27 14 19 10 9 of 124
  • 10. Max Heap Insertion (5/7) – Swap 33 & 44. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 44 33 27 14 19 10 10 of 124
  • 11. Max Heap Insertion (6/7) – Swap 44 & 42. – Insert 26, 31 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 44 35 42 33 27 14 19 10 26 31 11 of 124
  • 12. Max Heap Insertion (7/7) • Swap 31 & 14. Insert 2 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 44 35 42 33 27 31 19 10 26 14 • Insertion Complete 2 12 of 124
  • 13. Max Heap Deletion (1/8) • Delete 2 (Last leaf node in the CBT) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 44 35 42 33 27 31 19 10 26 14 2 13 of 124
  • 14. Max Heap Deletion (2/8) • Remove 2 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 44 35 42 33 27 31 19 10 26 14 • Delete 44 (Root node of CBT) 14 of 124
  • 15. Max Heap Deletion (4/8) • Swap 44 (root node) with 14 (last leaf node in the CBT) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 14 35 42 33 27 31 19 10 26 44 15 of 124
  • 16. Max Heap Deletion (4/8) • Remove 44. • Compare 14 (present root) with 42 & 35. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 14 35 42 33 27 31 19 10 26 16 of 124
  • 17. Max Heap Deletion (5/8) • Swap 14 with 42 (biggest value). • Compare 14 with 33 and 31. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 14 33 27 31 19 10 26 17 of 124
  • 18. Max Heap Deletion (6/8) • Swap 14 with 33 (biggest value). • Compare 14 with 10 and 26. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 33 14 27 31 19 10 26 18 of 124
  • 19. Max Heap Deletion (7/8) • Swap 14 with 26 (biggest value). 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 33 26 27 31 19 10 14 • Delete 27 19 of 124
  • 20. Max Heap Deletion (8/8) • Swap 27 with 14 (last leaf node in CBT). Remove 27 • Compare 14 with 35. No change. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 42 35 33 26 14 31 19 10 20 of 124
  • 21. Exercise • Q1: Insert 13, 14, 15, 18, 11, 12, 17, 16 into a Binary Max Heap Tree & Min Heap Tree. • Q2: The Breadth-first traversal pattern of a Binary tree is 8, 3, 4, 1, 5, 7, 9, 2, 6, 0. Convert the tree into a Binary Max Heap Tree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson • A1: – Binary Max Heap Tree: 18, 16, 17, 15, 11, 12, 14, 13 – Min Heap Tree: 11, 13, 12, 16, 14, 15, 17, 18 • A2: – Binary Max Heap Tree: 9, 6, 8, 3, 5, 7, 4, 2, 1, 0 21 of 124
  • 22.
  • 23. Heap Sort (1/18) • The root node of a Heap Tree is either the largest or smallest element. • Process – (1) Construct Binary heap tree (max/min). – (2) Remove the root node and store it in an array and replace the rightmost leaf node as the new root. – (3) Repeat steps 1 & 2 until tree is made empty. The final array consists of sorted elements. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 23 of 124
  • 24. Heap Sort (2/18) • Example: The Breadth-First Traversal pattern of a CBT is 8, 3, 4, 1, 5, 7, 9, 2, 6, 0. Sort the elements in the CBT using Heap Sort Algo. – Given CBT is – Lets convert given CBT into a Binary Max Heap Tree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 4 3 1 9 5 7 2 6 0 24 of 124
  • 25. Heap Sort (3/18) • Starting from the rightmost subtree in bottom-most level check if Max Heap Property is satisfied. • If Max Heap Property is not satisfied then rearrange the elements by heapification else check next subtree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 4 3 1 9 5 7 2 6 0 8 4 3 6 9 5 7 2 1 0 25 of 124
  • 26. Heap Sort (4/18) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 4 3 6 9 5 7 2 1 0 8 9 3 6 4 5 7 2 1 0 26 of 124
  • 27. Heap Sort (5/18) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 9 3 6 4 5 7 2 1 0 8 9 6 3 4 5 7 2 1 0 27 of 124
  • 28. Heap Sort (6/18) • Binary Max Heap Tree is constructed 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 9 6 3 4 5 7 2 1 0 9 8 6 3 4 5 7 2 1 0 28 of 124
  • 29. Heap Sort (7/18) • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 9 8 6 3 4 5 7 2 1 0 0 8 6 3 4 5 7 2 1 • Sorted Array {9} 29 of 124
  • 30. Heap Sort (8/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 0 8 6 3 4 5 7 2 1 8 7 6 3 4 5 0 2 1 • Sorted Array {9} 30 of 124
  • 31. Heap Sort (9/18) • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 8 7 6 3 4 5 0 2 1 1 7 6 3 4 5 0 2 • Sorted Array {9, 8} 31 of 124
  • 32. Heap Sort (10/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 1 7 6 3 4 5 0 2 7 4 6 3 1 5 0 2 • Sorted Array {9, 8} 32 of 124
  • 33. Heap Sort (11/18) • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 7 4 6 3 1 5 0 2 2 4 6 3 1 5 0 • Sorted Array {9, 8, 7} 33 of 124
  • 34. Heap Sort (12/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 2 4 6 3 1 5 0 6 4 5 3 1 2 0 • Sorted Array {9, 8, 7, 6} 1 4 5 3 2 0 • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 34 of 124
  • 35. Heap Sort (13/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 1 4 5 3 2 0 5 4 3 1 2 0 • Sorted Array {9, 8, 7, 6, 5} 0 4 3 1 2 • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 35 of 124
  • 36. Heap Sort (14/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 0 4 3 1 2 4 0 3 1 2 • Sorted Array {9, 8, 7, 6, 5, 4} 2 0 3 1 • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 36 of 124
  • 37. Heap Sort (15/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 2 0 3 1 3 0 2 1 • Sorted Array {9, 8, 7, 6, 5, 4, 3} 1 0 2 • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 37 of 124
  • 38. Heap Sort (16/18) • Convert CBT to binary Max Heap Tree. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 1 0 2 2 0 1 • Sorted Array {9, 8, 7, 6, 5, 4, 3, 2} 0 1 • Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 38 of 124
  • 39. Heap Sort (17/18) • Convert CBT to binary Max Heap Tree. Remove the root node. The last leaf node is shifted/made the new root node of the CBT. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 0 1 1 0 • Sorted Array {9, 8, 7, 6, 5, 4, 3, 2, 1} 0 • The CBT is a Singleton Tree. The root node is copied to array and the Final Sorted Array {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} 39 of 124
  • 40. Heap Sort (18/18) • Note: – Alternatively, instead of removing root node from heap tree, it can be swapped with the rightmost leaf and retained in the tree. This node should not be considered for further operations until the sorting procedure is completed. – Then, the breadth-first traversal pattern would be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 0 2 1 3 6 4 5 7 8 9 40 of 124
  • 41.
  • 42. Overview (1/2) • Lossless data compression algorithm that assigns a variable- length unique code (also called bit string or binary string or code or codeword) to each symbol or character in the input data based on its frequency of occurrence. – i.e. The most frequent symbol has the shortest code. – E.g. Consider a string “ACCEBFFFFAAXXBLKE” 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Character Frequency Code A 3 111 B 2 100 C 2 101 E 2 001 F 4 01 K 1 0000 L 1 0001 X 2 110 42 of 124
  • 43. Overview (2/2) • Huffman code is a prefix code (or) prefix-free code – i.e. The code representing a particular symbol is never a prefix of the code representing any other symbol – Example • {01, 10, 0010, 1111} is prefix free • {01, 10, 0010, 1010} is not prefix free because 10 is a prefix of 1010. – Every message encoded by a prefix free code is uniquely decipherable • Variable-Length (prefix) code uses the least space when compared to the Fixed-length code 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 43 of 124
  • 44. Activity (1/2) • (Q1) Suppose we want to store messages made up of 4 characters a, b, c, d with frequencies 60, 5, 30, 5 (percents) respectively. What are the fixed-length codes and prefix-free codes that use the least space? – To store 100 of these characters, • Fixed-length code requires 100×2 = 200 bits • Prefix code requires (60×1) + (5×3) + (30×2) + (5×3) = 150 bits – Prefix-free codes use least space & offers a 25% saving. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Characters a b c d Frequency 60 5 30 5 Fixed-Length Code 00 10 10 11 Variable-length code (prefix code) 0 110 10 111 44 of 124
  • 45. Activity (2/2) • (Q2) Code is {a=0, b=110, c=10, d=111}. Check if the message “01101100” is uniquely decipherable. – 01101100 => 01101100 => 01101100 => 01101100 => “abba” • The given codes are of variable length & are prefix-free codes. So, the given message is uniquely decipherable to “abba”. Hence, {a=0, b=110, c=10, d=111} is a Huffman code. • (Q3) Code is {a=1, b=110, c=10, d=111}. Check if the message “1101111” is uniquely decipherable. – 1101111 => 1101111 => “acad” – 1101111 => 1101111 => “bad” • Though the given codes are of variable length, still they are not prefix-free codes. So, the given message is not uniquely decipherable as we get two messages “acad” and “bad”. Hence, {a=1, b=110, c=10, d=111} is not a Huffman code. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 45 of 124
  • 46. Huffman Algorithm (1/15) • Huffman Algorithm is used to find the Huffman code through construction of a Huffman Tree. • Procedure – (1) Create a leaf node for each unique character and build a min heap of all leaf nodes. – (2) Extract 2 nodes with the minimum frequency from the min heap – (3) Create a new internal node with frequency equal to the sum of the 2 node frequencies. Make the first extracted node as its left child and the other extracted node as its right child. Add this node to the min heap. – (4) Repeat steps 2&3 until the heap contains only one node. The remaining node is the root node and the tree is complete. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 46 of 124
  • 47. Huffman Algorithm (2/15) • (Q) Suppose we want to store messages made up of 6 characters a, b, c, d, e, f with frequencies 5, 9, 12, 13, 16, 45 respectively. Find the Huffman code. – Arrange the characters in ascending order of its frequencies – Construct a min-heap tree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Characters a b c d e f Frequency 5 9 12 13 16 45 a/5 b/9 c/12 d/13 e/16 f/45 47 of 124
  • 48. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson b/9 d/13 c/12 f/45 e/16 Remove 9 e/16 d/13 c/12 f/45 To min-heap c/12 d/13 e/16 f/45 a/5 b/9 c/12 d/13 e/16 f/45 f/45 b/9 c/12 d/13 e/16 b/9 d/13 c/12 f/45 e/16 Remove 5 To min-heap Extract 2 nodes with smallest frequency. 48 of 122
  • 49. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Create new internal node (N1) • Frequency (N1) = Sum of freq. of the 2 extracted nodes = 5+9 = 14 • Create a subtree with node (N1) as root, 1st extracted node as left child & 2nd extracted node as right child. • Insert N1 into the min heap tree Characters a b c d e f Frequency 5 9 12 13 16 45 N1/14 b/9 a/5 c/12 d/13 e/16 f/45 N1/14 c/12 d/13 e/16 f/45 Insert N1 It’s a Min Heap Tree 49 of 122
  • 50. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Remove 13 To min-heap Remove 12 To min-heap Extract 2 nodes with smallest frequency. c/12 d/13 e/16 f/45 N1/14 N1/14 d/13 e/16 f/45 d/13 N1/14 e/16 f/45 d/13 N1/14 e/16 f/45 f/45 N1/14 e/16 N1/14 f/45 e/16 50 of 122
  • 51. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Create new internal node (N2) • Frequency (N2) = Sum of freq. of the 2 extracted nodes = 12+13 = 25 • Create a subtree with node (N2) as root, 1st extracted node as left child & 2nd extracted node as right child. • Insert N2 into the min heap tree Characters a b c d e f Frequency 5 9 12 13 16 45 N2/25 d/13 c/12 N1/14 f/45 e/16 N2/25 To min-heap N1/14 N2/25 e/16 f/45 Insert N2 N1/14 f/45 e/16 51 of 122
  • 52. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Extract 2 nodes with smallest frequency. N1/14 N2/25 e/16 f/45 Remove 14 f/45 N2/25 e/16 To min-heap e/16 N2/25 f/45 Remove 16 To min-heap e/16 N2/25 f/45 f/45 N2/25 N2/25 f/45 52 of 122
  • 53. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Create new internal node (N3) • Frequency (N3) = Sum of freq. of the 2 extracted nodes = 14+16 = 30 • Create a subtree with node (N3) as root, 1st extracted node as left child & 2nd extracted node as right child. • Insert N3 into the min heap tree Characters a b c d e f Frequency 5 9 12 13 16 45 N3/30 e/16 N1/14 Insert N3 N2/25 f/45 N2/25 f/45 N3/30 It’s a Min Heap Tree 53 of 122
  • 54. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Extract 2 nodes with smallest frequency. N2/25 f/45 N3/30 Remove 25 Remove 30 N3/30 f/45 It’s a Min Heap Tree f/45 It’s a Min Heap Tree Create new internal node (N4) • Frequency (N4) = Sum of freq. of the 2 extracted nodes = 25+30 = 55 • Create a subtree with node (N4) as root, 1st extracted node as left child & 2nd extracted node as right child. • Insert N4 into the min heap tree N4/55 N3/30 N2/25 f/45 Insert N4 f/45 N4/55 It’s a Min Heap Tree 54 of 122
  • 55. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Extract 2 nodes with smallest frequency. f/45 N4/55 Remove 45 Remove 55 It’s a Min Heap Tree N4/55 Empty Create new internal node (N5) • Frequency (N5) = Sum of freq. of the 2 extracted nodes = 45+55 = 100 • Create a subtree with node (N5) as root, 1st extracted node as left child & 2nd extracted node as right child. • Insert N5 into the Empty tree as root node N5/100 N4/55 f/45 Empty N5/100 Insert N5 55 of 122
  • 56. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson The existing internal nodes are: N1/14 b/9 a/5 N2/25 d/13 c/12 N3/30 e/16 N1/14 N4/55 N3/30 N2/25 N5/100 N4/55 f/45 N5/100 Fit internal nodes into tree: N5/100 f/45 N4/55 N2/25 c/12 d/13 N3/30 N1/14 a/5 b/9 e/16 56 of 122
  • 57. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson The generated Huffman Tree is: N5/100 f/45 N4/55 N2/25 c/12 d/13 N3/30 N1/14 a/5 b/9 e/16 It’s a Full Tree 57 of 122
  • 58. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson For every parent, consider ‘0’ for Left Child traversal & ‘1’ for Right child traversal N5/100 f/45 N4/55 N2/25 c/12 d/13 N3/30 N1/14 a/5 b/9 e/16 0 0 0 0 0 1 1 1 1 1 58 of 122
  • 59. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Find code for each character by traversal from root N5/100 f/45 N4/55 N2/25 c/12 d/13 N3/30 N1/14 a/5 b/9 e/16 0 0 0 0 0 1 1 1 1 1 Characters a b c d e f Frequency 5 9 12 13 16 45 Variable-length code (prefix code) 1100 1101 100 101 111 0 59 of 122
  • 60. Huffman Algorithm (15/15) • (Q) Suppose we want to store messages made up of 6 characters a, b, c, d, e with frequencies 20, 15, 5, 15, 45 respectively. Find Huffman tree and Huffman code. – Huffman Tree – Huffman Code 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Characters a b c d e Variable-length code (prefix code) 000 001 010 011 1 60 of 124
  • 61.
  • 62. m-way search tree (1/3) • A tree whose nodes can have certain no. of children (≥2) and satisfy a search property (i.e. like BST) is called multi-way or m-way search tree. • Value of ‘m’ specifies – Upper limit (max. no.) of subtrees/children per node. – Upper limit (max. no.) of keys stored per node. i.e. (m-1) keys. • Application of m-way search tree to speedup processing – ‘m’ can be a very large number • Each node corresponds to a physical block on disk. • Moving one node to another (i.e. moving m-1 data items or a disk block) involves reading a disk block. This process is very slow when compared to moving around a data structure stored in memory. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 62 of 124
  • 63. m-way search tree (2/3) • Properties – All keys of a node are stored in ascending order. • Children between keys k1 & k2 contains all keys in range k1 to k2. – Each node has 1 to (m-1) keys. • i.e. 2 to m children/subtrees. – No. of non-empty subtrees = 0 (for a leaf) to 1+(no. of keys). – Root has at least two subtrees (or store one key) unless it is a singleton tree. • Advantages – m-way search trees give the same advantages to m-way trees that BST gave to binary trees - they provide fast information retrieval and update. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 63 of 124
  • 64. m-way search tree (3/3) • Disadvantage – M-way search trees have the same problem that BST had - i.e. they can become unbalanced. – E.g.: 4-way Search Tree • Note: B-Tree of order m (m-way search tree) is used instead. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 64 of 124
  • 65.
  • 66. B-Trees (1/5) • B-Tree is a self-balancing search tree. • Vs. other self-balancing search trees (like AVL) – B-Trees can hold huge data that cannot fit in main memory. – B-Trees reduce the number of disk accesses. • Disk read operation is done when a block (set of keys) is unavailable in main memory. It is a very slow operation. • Most of the tree operations require O(h) disk accesses. – Height (h) of B-Trees is kept low by putting maximum possible keys in a B-Tree node (approx. equal to the disk block size). • Height of B-tree = 0.5log2n to log2n – Where, n - no. of children 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 66 of 124
  • 67. B-Trees (2/5) • Defined by – No. of keys per node (x) & no. of children (y). Hence also called as a “x-y B-Tree”. • E.g. In 2-3 B-Tree, every internal node is either a 2 node (has 1 key and 2 children) or 3 node (has 2 keys and 3 children). • E.g. In 2-4 B-Tree, every internal node is either a 2/3/4 node. Also called 2-3-4 tree. – Order (determine the no. of subtrees/children) • E.g. 2-3 B-Tree is of Order 3, 2-4 B-Tree is of Order 4 – Minimum degree or Branching Factor (t) or outdegree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 67 of 124
  • 68. B-Trees (3/5) • Properties – All leaves are at same level (i.e. Balanced). – All keys of a node are stored in ascending order • Children between keys k1 & k2 contains all keys in range k1 to k2. – No. of keys in a node • At least: Root has min. 1 key, All node (except root): min.(t-1) keys. • At most: All nodes (including root) contain max. (2t-1) keys. – Max. no. of subtrees per node = No. of keys per node + 1. • Vs. BST – B-Tree grows and shrinks from root which is unlike BST. BSTs grow downward and also shrink from downward. – Like other balanced BSTs, time complexity to search, insert and delete is O(log n). 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 68 of 124
  • 69. B-Trees (4/5) • Example-1 – B-Tree with minimum degree(t)=3 • All leaves are at same level • No. of keys – Root has 3 keys (>1) – Nodes (except root) have at least 2 keys i.e. (t-1) keys – All nodes (including root) may contain at most (2t-1) keys. i.e. 5 • No. of children/subtrees per node = No. of keys in node + 1. – No. of children of the root node = 3 + 1 = 4 • All keys of a node are sorted in ascending order. – Keys of root node 3, 30, 60 are sorted • Subtree between keys k1 and k2 contains all keys in range from k1 and k2. – Between 3 and 30 of root node, the subtree contains the keys 4, 5, 6 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 69 of 124
  • 70. B-Trees (5/5) • Example-2 – 2-3 B-Tree (or) B-Tree of Order 3 (or) 3-way Tree • Example-3 – 2-4 B-Tree (or) 2-3-4 Tree (or) B-Tree of Order 4 (or) 4-way tree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 70 of 124
  • 71. Insertion (1/12) • Consider the following 2-4 B-Tree 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 24 26 28 30 33 37 39 40 71 of 124
  • 72. Insertion (2/12) • Insert 21 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 24 26 28 30 33 37 39 40 21 72 of 124
  • 73. Insertion (3/12) • Insert 23 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 24 26 28 30 33 37 39 40 24 Shift 24 Save 23 73 of 124
  • 74. Insertion (4/12) • Insert 23 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 26 28 30 33 37 39 40 74 of 124
  • 75. Insertion (5/12) • Insert 29 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 26 28 30 33 37 39 40 26 28 29 30 Node in Level 2 already has 3 keys!! Split Node & Insert 29 75 of 124
  • 76. Insertion (6/12) • Insert 29 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40 26 28 29 30 28 76 of 124 3 children in Level 2 would promote 28 to Level 1
  • 77. Insertion (7/12) • Insert 29 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 28 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 77 of 124
  • 78. Insertion (8/12) • Insert 7 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 28 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 4 5 6 7 78 of 124 Node in Level 2 already has 3 keys. Split the node & Insert 7
  • 79. Insertion (9/12) • Insert 7 – Node in Level 2 has 5 children. Promote 5 to Level 1. Now node in Level 1 already has 3 keys. Hence Split parent node. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 4 5 6 7 3 8 10 4 3 5 79 of 124
  • 80. Insertion (10/12) • Insert 7 – Promote 5 to Level 0. Here, node already has 3 keys - split parent node. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 6 7 3 5 8 10 4 80 of 124
  • 81. Insertion (11/12) • Insert 7 – Node in Level 1 has 2 keys and only 2 children. Promote 5 to Level 0. At Level 0, the root node already has 3 keys - split parent node. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 6 7 3 5 8 10 4 22 32 5 13 81 of 124
  • 82. Insertion (12/12) • Insert 7 – Node has 2 keys and only 2 children. Promote 13 by creating new level. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 13 22 32 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 26 29 30 6 7 3 5 8 10 4 22 32 5 13 13 82 of 124
  • 83.
  • 84. Terminologies (1/7) • Graph is a non-linear Data Structure • Definition of Graph, G = (V, E) – Set of Nodes or Vertices (V), Set of Edges (E) • Number of Vertices = |V|, Number of Edges |E| • Edges connect two vertices (source & destination) • Types of Edges – Based on Direction • Directed edges – Represented as ordered pair (V1, V2). Here (V1, V2) ≠ (V2, V1) • Undirected (Bidirectional) edges – Represented as unordered pair {V1, V2}. Here {V1, V2} = {V2, V1} – Based on Weights • Weighted & Unweighted edges 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 84 of 124
  • 85. Terminologies (2/7) • Directed Graph (Digraph) & Undirected Graphs – Given graph is an undirected graph as it has undirected edges. – Undirected graph is defined as G = (V, E) • where – V = {V1,V2,V3,V4,V5,V6,V7,V8} – E = {{V1,V2},{V1,V3},{V1,V4},{V2,V5},{V2,V6},{V3,V7},{V4,V8}, …. } 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 85 of 124
  • 86. Terminologies (3/7) • Weighted & Unweighted Graphs – All graphs with weighted (cost) edges are weighted graphs. • Edges of a weighted graph is labelled with their corresponding weight (cost incurred for taking the edge). – Edges of an unweighted graph is not labelled and are considered to have the same weight/cost (=1). 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 86 of 124
  • 87. Terminologies (4/7) • Null Graph – A graph which contains only isolated vertices • i.e. Set of edges in a null graph is empty. E = {} • Multi-edge – Many edges can be formed from a single vertex. • Parallel Edges – Edges have the same end nodes/vertices. i.e. Two vertices may be connected by more than one edge. • Multi Graph – A graph having parallel edges. • Trivial Graph – A graph with only one vertex. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 87 of 124
  • 88. Terminologies (5/7) • Loop (or) Self-Loop – An edge in a graph which is drawn from a vertex to itself. • Simple graph – A graph with no loops and no parallel edges. – Max. number of edges |E| • Simple undirected graph = n(n – 1)/2. 'n' vertices. • Simple digraph = n(n-1), approx. equal to square of n – Min. no. of edges = 0 – No. of simple graphs = 2(n(n-1)/2) • Dense and Sparse Graph • Cyclic Graph 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 88 of 124
  • 89. Terminologies (6/7) • Complete Graph – A simple graph (G) is said to be complete if every vertex in G is connected with every other vertex. – A graph with ‘n’ vertex has n(n-1)/2 edges. • Regular Graph – A graph in which all the vertices are of equal degree. • A regular graph of degree r is a graph with each vertex having the degree r. • Every null graph is a regular graph of degree zero & a complete graph is a regular graph of degree n-1. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 89 of 124
  • 90. Terminologies (7/7) • Vs. Tree – Tree is also an example of non-linear Data Structure. – Tree is a special kind of graph with special rules. • Tree with n nodes, would have (n-1) edges, • All nodes are reachable through the root, etc. – In Tree all nodes are connected to root in diff. breadth & depth. • Applications – Graph Theory – Weighted Undirected Graph: Inter-city road map – Unweighted Undirected Graph: Social Network – Weighted Digraph: Intra-city road map – Unweighted Digraph: www, web crawlers 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 90 of 124
  • 91. Graph Representation • Vertex List & Edge List • Vertex List & Adjacency Matrix (for Dense Graphs) • Vertex List & Adjacency List (for Sparse Graphs) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 91 of 124
  • 92. Example • (Q1) Find the DFS & BFS traversal of the following digraph starting at node S. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson • (A1) – DFS traversal: S, A, D, B, C – BFS traversal: S, A, B, C, D 92 of 124
  • 93. Example - Depth First Traversal (1/9) • Additional Data Structure Used: Stack • Starting at node with S – DFS Sequence: S 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson S 93 of 124
  • 94. Example - Depth First Traversal (2/9) – DFS Sequence: S A 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson A S 94 of 124
  • 95. Example - Depth First Traversal (3/9) – DFS Sequence: S A D 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson D A S 95 of 124
  • 96. Example - Depth First Traversal (4/9) – DFS Sequence: S A D B 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson B D A S 96 of 124
  • 97. Example - Depth First Traversal (5/9) – DFS Sequence: S A D B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson C D A S 97 of 124
  • 98. Example - Depth First Traversal (6/9) – DFS Sequence: S A D B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson D A S 98 of 124
  • 99. Example - Depth First Traversal (7/9) – DFS Sequence: S A D B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson A S 99 of 124
  • 100. Example - Depth First Traversal (8/9) – DFS Sequence: S A D B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson S 100 of 124
  • 101. Example - Depth First Traversal (9/9) – Final DFS Sequence: S A D B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Stack Empty 101 of 124
  • 102. Example - Breadth First Traversal (1/9) • Additional Data Structure Used: Queue • Starting at node with S – BFS Sequence: S 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 102 of 124
  • 103. Example - Breadth First Traversal (2/9) – BFS Sequence: S A 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson A 103 of 124
  • 104. Example - Breadth First Traversal (3/9) – BFS Sequence: S A B 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson A B 104 of 124
  • 105. Example - Breadth First Traversal (4/9) – BFS Sequence: S A B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson A B C 105 of 124
  • 106. Example - Breadth First Traversal (5/9) – BFS Sequence: S A B C 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson B C Remove A 106 of 124
  • 107. Example - Breadth First Traversal (6/9) – BFS Sequence: S A B C D 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson B C D 107 of 124
  • 108. Example - Breadth First Traversal (7/9) – BFS Sequence: S A B C D 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson C D Remove B 108 of 124
  • 109. Example - Breadth First Traversal (8/9) – BFS Sequence: S A B C D 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson D Remove C 109 of 124
  • 110. Example - Breadth First Traversal (9/9) – Final BFS Sequence: S A B C D 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Queue Empty Remove D 110 of 124
  • 111. Activity (1/2) • Q2. Find the DFS & BFS traversal of the following digraph starting at node S. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson • A2 – DFS traversal: S, A, D, G, E, B, F, C – BFS traversal: S, A, B, C, D, E, F, G 111 of 124
  • 112. Activity (2/2) • Q3. Find the DFS & BFS traversal of the following digraph starting at node A. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson • A3 – DFS traversal: A, B, D, C, F, G, E – BFS traversal: A, B, C, D, E, F, G Weights are ignored !! 112 of 124
  • 113. Spanning Tree (1/2) • A subgraph or tree that spans all vertices of a connected & undirected graph • Maximum no. of Spanning Trees for a graph is V(V-2) – Here V = no. of vertices in the graph • Example: – Find the maximum no. of spanning trees for the given graph. Also draw them. • V = 3. Hence the given graph has 3(3-2) = 3 spanning trees. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 113 of 124
  • 114. Spanning Tree (2/2) • Properties of Spanning Tree – Maximum no. of Spanning Trees for a graph = V(V-2) – Spanning tree for a graph G = (V, E) has of V vertices and (V-1) edges after removal of only (E-V+1) edges. • Example: Consider a graph with V=4, E=6. – After removal of 3 edges (E-V+1 = 6-4+1) from graph, a spanning tree would have 4 vertices & 3 edges (V-1 = 4-1) – Should be maximally acyclic. i.e. Adding a single edge to a spanning tree would make it a Acyclic graph. – Should be minimally connected. i.e. Removal of a single edge from a spanning tree would give a disconnected graph. 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 114 of 124
  • 115. Minimum Cost Spanning Tree • A Spanning Tree which is made of minimum cost edges & has a minimum total cost. • Algorithms – Kruskal’s Algorithm – Prim’s Algorithm • Basic Initial steps – Remove Self-loops – Remove Parallel edges (Retain the edge with least weight) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 115 of 124
  • 116. Kruskal’s Spanning Tree Algorithm (1/4) • (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm – Draw Edge Table (Edge in ascending order of their weight) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 10 24 12 22 28 25 16 14 18 Edge AB CD FG CF DG EG DE BE AC Weight 10 12 14 16 18 22 24 25 28 A B E D C F G 2 Edges have same weight Take both edges & consider any order 116 of 124
  • 117. Kruskal’s Spanning Tree Algorithm (2/4) • (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 10 24 12 22 28 25 16 14 18 10 12 22 25 16 14 18 24 Edge AB CD FG CF DG EG DE BE AC Weight 10 12 14 16 18 22 24 25 28 Start from smallest weight edge & keep selecting edges that does not form any circuit with previously selected edges A B E D C F G A B E D C F G B 117 of 124
  • 118. Kruskal’s Spanning Tree Algorithm (3/4) • (Q1) Find the MST using Kruskal’s Spanning Tree Algorithm 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson MST Cost = 99 10 24 12 22 28 25 16 14 18 10 12 22 25 16 14 A B E D C F G A E D C F G B 118 of 124
  • 119. Kruskal’s Spanning Tree Algorithm (4/4) • (Q2) Find the MST using Kruskal’s Spanning Tree Algorithm by starting at S 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Cost of MST = 17 119 of 124
  • 120. Prim’s Spanning Tree Algorithm (1/4) • (Q1) Find the MST using Prim’s Spanning Tree Algorithm – Draw Table (V x V) 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 10 24 12 22 28 25 16 14 18 A E D C F G B A B C D E F G A 0 10 28 ∞ ∞ ∞ ∞ B 10 0 ∞ ∞ 25 ∞ ∞ C 28 ∞ 0 12 ∞ 16 ∞ D ∞ ∞ 12 0 24 ∞ 18 E ∞ 25 ∞ 24 0 ∞ 22 F ∞ ∞ 16 ∞ ∞ 0 14 G ∞ ∞ ∞ 18 22 14 0 120 of 124
  • 121. Prim’s Spanning Tree Algorithm (2/4) • (Q1) Find the MST using Prim’s Spanning Tree Algorithm 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 10 12 22 25 16 14 A B E D C F G A B C D E F G A 0 10 28 ∞ ∞ ∞ ∞ B 10 0 ∞ ∞ 25 ∞ ∞ C 28 ∞ 0 12 ∞ 16 ∞ D ∞ ∞ 12 0 24 ∞ 18 E ∞ 25 ∞ 24 0 ∞ 22 F ∞ ∞ 16 ∞ ∞ 0 14 G ∞ ∞ ∞ 18 22 14 0 18 24 28 121 of 124
  • 122. Prim’s Spanning Tree Algorithm (3/4) • (Q1) Find the MST using Prim’s Spanning Tree Algorithm 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson 10 24 12 22 28 25 16 14 18 MST Cost = 99 A E D C F G B 10 12 22 25 16 14 A B E D C F G 18 24 28 122 of 124
  • 123. Prim’s Spanning Tree Algorithm (4/4) • (Q2) Find the MST using Prim’s Spanning Tree Algorithm by starting at node S 23-Apr-22 Data Structures Instructor: Mr.S.Christalin Nelson Cost of MST = 17 123 of 124