DS Final Note
DS Final Note
Data Structure
CSE-2321
SyllabuS
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 2 of 31
4. Queue
Recursion
1
[Au22]
Ans Given, N = 8, B = 3, 7, -2, 5, 6, -4, 2, 7
Y (5) = Y (5-1) + B (5) [K = 5 and 5 < 8]
= Y (4-1) + B (4) + B (5)
= Y (3-1) + B (3) + B (4) + B (5)
= Y (2-1) + B (2) + B (3) + B (4) + B (5)
= Y (1-1) + B (1) + B (2) + B (3) + B (4) + B (5)
= 0 + B (1) + B (2) + B (3) + B (4) + B (5) [K = 0]
= 3 + 7 + (-2) + 5 + 6
= 19
Given, N = 3, B = 2, 7, -4
Y (5) = Y (5-1) [K = 5 and 5 > 3]
= Y (4-1) [K =4 and 4 > 3]
= Y (3-1) + B (3)
= Y (2-1) + B (2) + B (3)
= Y (1-1) + B (1) + B (2) + B (3)
= 0 + B (1) + B (2) + B (3) [K = 0]
= 2 + 7 + (-4)
=5
2
[SP22]
Ans G (2) = 3 * 2 = 6
G (8) = 2 * G (8-5) + 0
= 2 * G (3) + 0
= 2 * 3 * 3 + 0 = 18
G (24) = 2 * G (24-8) + 0
= 2 * G (16) + 0
= 2 * 2 * G (16-5) + 0
= 2 * 2 * G (11)
= 2 * 2 * 2 * G (11-5) + 0
= 2 * 2 * 2 * 2 * G (6)
= 2 * 2 * 2 * 2 * 2 * G (6-5) + 0
= 2 * 2 * 2 * 2 * 2 * G (1)
=2*2*2*2*2*3*1
= 96
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 3 of 31
[SP19, SP18]
Ans GCD (6, 15) = GCD (15, 6) [As, A < B] GCD (20, 28) = GCD (28, 20) [As, A < B]
= GCD (6, MOD (15, 6)) = GCD (28, MOD (28, 20))
= GCD (6, 3) = GCD (28, 8)
= GCD (3, MOD (6,3)) = GCD (8, MOD (28, 8))
= GCD (3, 0) = GCD (8, 4)
=3 = GCD (4, MOD (8, 4))
= GCD (4, 0)
=4
4
[SP18]
Ans 1. F (4, 2) = 1
2. F (2, 4) = F (2-1, 4) + F (2-1, 4-1)
= F (1, 4) + F (1, 3)
= F (1-1, 4) + F (1-1, 4-1) + F (1-1, 3) + F (1-1, 3-1)
= F (0, 4) + F (0, 3) + F (0, 3) + F (0, 2)
=1+1+1+1=4
[AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 4 of 31
Circular queue:
𝑖𝑓 𝒇𝒓𝒐𝒏𝒕 = 𝟎, 𝑡ℎ𝑒𝑛
𝑠𝑒𝑡 𝒓𝒆𝒂𝒓 = 𝒇𝒓𝒐𝒏𝒕 = 𝟏
[Au’22]
Ans 1. Number of elements in the queue = (Rear - Front + n) % n +1
In this case, Front is 5, Rear is 10, and n is 12. Plug these values into the formula:
Number of elements in the queue = (10 - 5 + 12) % 12 +1
Now, calculate the numerator first:
(10 - 5 + 12) = 17
Now, calculate the remainder when you divide 17 by 12:
17 % 12 = 5 + 1 = 6
So, there are 6 elements in the circular array queue with Front at 5 and Rear at 10.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 5 of 31
2
[SP18]
Ans 1. Number of elements in the queue = (Rear - Front + n) % n +1
In this case, Front is 4, Rear is 8, and n is 12. Plug these values into the formula:
Number of elements in the queue = (8 - 4 + 12) % 12 +1
Now, calculate the numerator first:
(8 - 4 + 12) = 16
Now, calculate the remainder when you divide 16 by 12:
16 % 12 = 4 + 1 = 5
3 Consider the following circular queue where Queue is allocated 6 memory cells.
Front = 3, Rear = 5, Queue: _, _, U, C, S, _
Illustrate the Queue, including front and rear, as the following sequences take place:
i) E is added iii) U, V, A are added
ii) 2 items deleted iv) 3 items are deleted [SP22]
Ans:
i) E is added:
front = 3, rear = 5 + 1 = 6
Queue:
U C S E
1 2 3 4 5 6
↑Front ↑Rear
i) 2 items are deleted
a. front = 4, rear = 6
Queue:
C S E
1 2 3 4 5 6
↑Front ↑Rear
b. front = 5, rear = 6
Queue:
S E
1 2 3 4 5 6
↑Front ↑Rear
ii) U,V,A are added
a. front = 5, rear = 1
Queue:
U S E
1 2 3 4 5 6
↑Rear ↑Front
b. front = 5, rear = 2
Queue:
U V S E
1 2 3 4 5 6
↑Rear ↑Front
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 6 of 31
c. front = 5, rear = 3
Queue:
U V A S E
1 2 3 4 5 6
↑Rear ↑Front
iii) 3 items are deleted
a. front = 6, rear = 3
Queue:
U V A E
1 2 3 4 5 6
↑Rear ↑Front
b. front = 1, rear = 3
Queue:
U V A
1 2 3 4 5 6
↑Front ↑Rear
c. front = 2, rear = 3
Queue:
V A
1 2 3 4 5 6
↑Front ↑Rear
4
[SP19]
Ans
i) W is added:
front = 3, rear = 4 + 1 = 5
Queue:
X Y W
1 2 3 4 5
↑Front ↑Rear
ii) 2 items are deleted
i) front = 4, rear = 5
Queue:
Y W
1 2 3 4 5
↑Front ↑Rear
ii) front = 5, rear = 5
Queue:
W
1 2 3 4 5
↑Front
↑Rear
iii) U,V,A are added
i) front = 5, rear = 1
Queue:
U W
1 2 3 4 5
↑Rear ↑Front
ii) front = 5, rear = 2
Queue:
U V W
1 2 3 4 5
↑Rear ↑Front
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 7 of 31
[AU17]
6
[ SP22, SP19]
Ans Insertion of an element
1. START
2. Store the element to insert in linear data structure
3. Check if (front == 0 && rear == MAX-1) || (front == rear+1) then queue
Overflow else goto step 4
4. Check if (front == -1) then front = 0; rear= 0; else goto step 5
5. Check if (rear == MAX -1) then rear=0; else rear= rear+1; and goto step 6
6. Insert element cqueue_arr[rear] = item;
7. STOP
Deletion of an element
1. START
2. Store the element to delete.
3. If front == -1 then Queue Underflow.
4. Element deleted from queue is cqueue_arr[front].
5. if (front==rear) then front= -1; rear= -1; else goto 6.
6. if (front == MAX - 1) then front= 0 else goto 7.
7. front = front + 1.
8. STOP
7
[AU18]
8
[AU18]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 8 of 31
1
[AU17]
Ans Ackerman function defined as
a. If m = 0, then A (m, n) = n + 1
b. If m ≠ 0 but n = 0, then A (m, n) = A (m-1, 1)
c. If m ≠ 0 and n ≠ 0, then A (m, n) = A (m-1, A (m, n-1))
A (1, 3)
= A (1-1, A (1, 3-1)) = A (0, A (0, A (0, 2)))
= A (0, A (1, 2)) = A (0, A (0, 2+1))
= A (0, A (1-1, A (1, 2-1))) = A (0, A (0, 3))
= A (0, A (0, A (1, 1))) = A (0, 3+1)
= A (0, A (0, A (1-1, A (1, 1-1)))) = A (0, 4)
= A (0, A (0, A (0, A (1, 0)))) = 4+1
= A (0, A (0, A (0, A (1-1, 1)))) =5
= A (0, A (0, A (0, A (0, 1))))
= A (0, A (0, A (0, 1+1)))
2 [Au’22]
Ans 7
♦ [SP19]
♦ [SP18]
♦ [AU17]
♦ [Au’22]
♦ [
SP22]
♦ [AU18,
AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 9 of 31
5. linked liSt
Linked list
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 10 of 31
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 11 of 31
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 12 of 31
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 13 of 31
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 14 of 31
[SP19, AU18]
[AU22, SP18]
4
[AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 15 of 31
[AU22, SP18]
6
[SP19]
9
10
11
12
(i) Avail = 10
so, after adding Jones following changes occure:
- Jones is added into the node taken from avail list which is BED[10]
- So, Avail list is updated by changing it’s link from Link[10] = 2
So, Avail = 2
- As this list is alphabetized, so, Jones will be added after Green.
So, Link[10] = 1, Link[8] =10
(ii) As Max is deleted, it’s node is added to the avail list.
We see that last node of the avail list is 6, Now we are adding current node to that ndoe.
- Link[6] = 4
- Link[7] = Link[4] = 12
- Link[4] = 0
Avail Start
↓ ↓
1 2 3 4 5 6 7 8 9 10 11 12
BED Kirk Dean Adam Lane Green Samu Jones Field Nelso
LINK 7 6 11 0 3 4 12 10 0 1 8 9
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 16 of 31
13
Start
↓
1 2 3 4 5 6 7 8 9 10 11 12
INFO Mary June Barbar Paula Diana Audrey Karen Nancy Ruth Eileen Sandra Halen
LINK 8 7 5 9 10 3 1 4 11 12 0 2
14
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 17 of 31
Group B
6. Sort
♧ SORTING
Selection Sort
Insertion Sort
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 18 of 31
♦ [AU22]
♦ 4 [SP22]
♦ 4 [SP22]
♦ [AU17]
♦ [SP19]
♦ [AU22]
♦ [AU17, SP18]
♦ [SP18]
♦ [AU17]
♧ HASHING
♦ [
AU22]
♦
[SP19]
♦ [AU18]
♧ COMPLEXITY
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 19 of 31
♦ [SP22]
♦ [AU18]
♦ [AU18,
SP18]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 20 of 31
7. tree
♧ BINARY TREE
A binary tree T is defined as a finite set of elements called nodes such that,
1. T is empty
2. T contains a distinguished node R, called the rot of T and the remaining nodes of T form an ordered
pair of disjoint binary trees T1 and T2
Sequential representation of binary tree:
Let T be a binary tree that is complete or nearly complete. Then T can be represented using a single linear
array TREE such that,
1. The Hoot R of T is stored in TREE [1]
2. If a node N occupies TREE [K], then its left child is stored in TREE [2K] and right child in TREE
[2K+1].
45
22 77
11 30 90
15 25 88
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
45 22 77 11 30 90 15 25 88
Sequential representation of binary tree
[AU18]
We know that,
For Pre order: Root → Left→ Right
For in order: Left → Root → Right
So, we can get root for the tree from the pre order.
In the pre order list, first element is G. So, the root of the tree is G
Now, from the in-order list, we can divide the list into two subtrees by G.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 21 of 31
G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Left subtree Root Right subtree
Now, to find left child of the root G we search first occurrence of any of the elements of left subtree in
the pre-order list.
We can see that, B (from left subtree list) in the pre-order list. So, left node is B.
G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Q B K, C, F, A P E, D, H, R
Left subtree Root Right subtree
In the right side of B we have K, C, F, A. Now we get A appeared first in the pre order list. So, A is
the right node of B subtree.
G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Q B K, C, F, A P E, D, H, R
Q B K, C, F A P E D H, R
Q B K C F A P E D H R
Left subtree Root Right subtree
This is how, we get the tree:
[AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 22 of 31
[SP18]
4
5 [SP22]
• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only nodes with keys
greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
• There must be no duplicate nodes.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 23 of 31
[SP’18]
[AU22]
3
4 [SP19]
4
[AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 24 of 31
[SP18]
6
4 [SP22]
7
[AU18]
In order successor is needed only when the right child is not empty. In this particular case, the in-order successor can
be obtained by finding the minimum value in the right child of the node.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 25 of 31
♧ HEAP
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 26 of 31
♦ [AU22]
♦ 4 [SP22]
♦ [AU17]
♦ [AU18]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 27 of 31
8. GraPH
Adjacency matrix:
Adjacency List:
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 28 of 31
3 [sp22]
Figure: Graph G
3 [SP19, SP18]
[AU17]
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 29 of 31
[AU17]
8 BFS and DFS for the following graph consider A as the source:
The general idea behind Breadth First Search (BFS) beginning at a starting node A is as follows. First we examine
the starting node A. Then we examine all theh neighbors of A. Then we examine all the neighbors of the
neighbors of A and so on. Adjacency List Nodes Marker
Let us assume we want to traverse the graph using BFS starting at A. A B, C, D A √
We will use QUEUE to keep track of neighbor nodes. B C, G B √
First push A to BUEVE and mark A as visited and pop A, Then Push C D C √
neighbors of A to QUEDE. D E D √
QUEUE: B, C, D E C E √
Pop B, Push neighbors of B and mark B as visited. F C, E, J F √
QUEUE: C, D, 4
G C, F G √
Pop C, Push neighbores of c and mark Cas visited.
H F, G H √
QUEUE: D, G
J E, H J √
Pop D, Push neighbors of D and mark Das visited.
QUEUE: G, E
Pop 6,Push neighbors of G and mark & as visited.
QUEUE: E, F
Pop E, Push neighbors of E and mark E as visited.
QUEUE: F
Pop F, Push neighbores of F and mark F as visited.
QUEUE: J
Pop J, Push neighbors of J and mark J as visited.
QUEUE: H
Pop H, Push neighbors of H and mark H as visited.
QUEUE: (empty)
As we can see the queue is empty, that means we’ve visited all nodes in the graph.
We start from vertex A, the DFS algorithm starts by putting it in the Visited list and putting all its adjacent vertices
in the stack. So, put A in the visited list and B, C, D in the stack.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 30 of 31
A Visited
B C D Stack
Now, in the top of the stack we have B
So, Set B as visited and put G in the stack. [As C is already in the stack, we won’t put it again]
A B Visited
G C D Stack
Now, in the top of the stack we have G
So, Set G as visited and put F in the stack. [As C is already in the stack, we won’t put it again]
A B G Visited
F C D Stack
Now, in the top of the stack we have F
So, Set F as visited and put E, J in the stack. [As C is already in the stack, we won’t put it again]
A B G F Visited
E J C D Stack
Now, in the top of the stack we have E
So, Set E as visited. [As C is already in the stack, we won’t put it again]
A B G F E Visited
J C D Stack
Now, in the top of the stack we have J
So, Set J as visited and put H in the stack.
A B G F E J Visited
H C D Stack
Now, in the top of the stack we have H
So, Set H as visited.
A B G F E J H Visited
C D Stack
Now, in the top of the stack we have C
So, Set C as visited.
A B G F E J H C Visited
D Stack
Now, in the top of the stack we have D
So, Set D as visited.
A B G F E J H C D
Visited
Stack
After we visit the last element, stack doesn't have any unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
9
4 [sp22]
10 Consider the following graph below, give the linked list representation of the
graph.
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 31 of 31
11
12
The maximum number of edges possible in a single graph with 'n' vertices is nC2 where, nC2 = n(n – 1)/2
BFS DFS
BFS starts traversal from root node and then DFS starts traversal from the root node and
explores the search in the level by level manner explore the search as far as possible from the
root node i.e. depth wise
BFS can be done using queue. DFS can be done using stack
It uses FIFO implementation It uses LIFO implementation
BFS is slower than DFS DFS is faster
13
14
15
CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC