Data Structure 1 Model
Data Structure 1 Model
Q1. What is a Data Structure? Wht study data structure, Five areas of computer science
where data structure are used ?
Q2. What is Garbej Collection? Who will Run garbej collection program? When it will
be run ?
Ans:-
1. Dimensions:
o The first dimension ranges from 0 to 5. Its length is:
Length of first dimension=5−0+1=6\text{Length of first dimension} = 5 -
0 + 1 = 6Length of first dimension=5−0+1=6
o The second dimension ranges from -2 to 7. Its length is:
Length of second dimension=7−(−2)+1=7−(−2)+1=10\text{Length of
second dimension} = 7 - (-2) + 1 = 7 - (-2) + 1 =
10Length of second dimension=7−(−2)+1=7−(−2)+1=10
2. Total Number of Elements in A:
1. Dimensions:
o The first dimension ranges from 0 to 5. Its length is:
Length of first dimension=5−0+1=6\text{Length of first dimension} = 5 - 0 + 1 =
6Length of first dimension=5−0+1=6
o The second dimension ranges from -1 to 4. Its length is:
Length of second dimension=4−(−1)+1=4−(−1)+1=6\text{Length of second
dimension} = 4 - (-1) + 1 = 4 - (-1) + 1 =
6Length of second dimension=4−(−1)+1=4−(−1)+1=6
2. Total Number of Elements in B:
Final Answer:
Array A:
o Length of first dimension: 6
o Length of second dimension: 10
o Total elements: 606060
Array B:
o Length of first dimension: 6
o Length of second dimension: 6
o Total elements: 36
Ans:- A primitive data structure is a fundamental type of data that is built into the
programming language. It is the simplest form of data structure that represents a single value
and cannot be broken down further. Examples include integers, floats, characters, and
booleans.
Basic data structures that directly store More complex data structures derived
Definition
values. from primitive types.
Single value or atomic unit (e.g., int, Can hold multiple values or collections of
Components
char). values (e.g., arrays, lists, trees).
Rigid in size and type (e.g., an integer Flexible in terms of size and structure (e.g.,
Flexibility
stores a fixed range of values). dynamic lists can grow or shrink).
Show the queue content with front and rear values after the following operations:
i) Insert A
ii) Delete
iii) Insert B
iv) Delete
Ans:-
A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but
connects the last position back to the first position to form a circle. This allows efficient utilization of
memory by overwriting unused positions after deletion.
Given Data:
Maximum size: 6
Initial queue content: L, M, N, -, -, -
Front = 2, Rear = 4
Index 0 1 2 3 4 5
Value - - L M N -
Operations:
1. Insert A
Rear moves to the next position: Rear=(4+1)%6=5\text{Rear} = (4 + 1) \% 6 =
5Rear=(4+1)%6=5
Add A at index 5.
Updated queue:
Index 0 1 2 3 4 5
Value - - L M N A
Front = 2, Rear = 5
2. Delete
Remove the element at the front (index 2), which is L.
Set that position to -.
Front moves to the next position: Front=(2+1)%6=3\text{Front} = (2 + 1) \% 6 =
3Front=(2+1)%6=3
Updated queue:
Index 0 1 2 3 4 5
Value - - - M N A
Front = 3, Rear = 5
3. Insert B
Rear moves to the next position: Rear=(5+1)%6=0\text{Rear} = (5 + 1) \% 6 =
0Rear=(5+1)%6=0
Add B at index 0.
Updated queue:
Index 0 1 2 3 4 5
Index 0 1 2 3 4 5
Value B - - M N A
Front = 3, Rear = 0
4. Delete
Remove the element at the front (index 3), which is M.
Set that position to -.
Front moves to the next position: Front=(3+1)%6=4\text{Front} = (3 + 1) \% 6 =
4Front=(3+1)%6=4
Updated queue:
Index 0 1 2 3 4 5
Value B - - - N A
Front = 4, Rear = 0
Final Result:
B) What is a singly linked list? Write an algorithm to find the number of times a given ITEM
occurs in the singly linked list.
The last node in the list points to NULL, indicating the end of the list. Unlike arrays, linked
lists do not require contiguous memory locations and allow dynamic memory allocation.
class SinglyLinkedList:
def __init__(self):
self.head = None
# Example usage:
sll = SinglyLinkedList()
sll.append(1)
sll.append(2)
sll.append(1)
sll.append(3)
sll.append(1)
item_to_find = 1
print("Occurrences of", item_to_find, ":",
sll.count_occurrences(item_to_find))
Output:
If item_to_find = 1, the output will be:
Occurrences of 1: 3
C)Let The keys:46,34,23,52,33 are inserted into an empty hash tableusing function h )key)=key mod
10.Given table content after every insertion ,if open addressing with linear probling is used to deal
with collision?
Step-by-Step Insertion:
1. Insert 46:
o Hash value: h(46)=46mod 10=6h(46) = 46 \mod 10 = 6h(46)=46mod10=6.
o Slot 666 is empty. Insert 464646 at index 666.
o Hash Table: [--, --, --, --, --, --, 46, --, --, --]
2. Insert 34:
o Hash value: h(34)=34mod 10=4h(34) = 34 \mod 10 = 4h(34)=34mod10=4.
o Slot 444 is empty. Insert 343434 at index 444.
o Hash Table: [--, --, --, --, 34, --, 46, --, --, --]
3. Insert 23:
o Hash value: h(23)=23mod 10=3h(23) = 23 \mod 10 = 3h(23)=23mod10=3.
o Slot 333 is empty. Insert 232323 at index 333.
o Hash Table: [--, --, --, 23, 34, --, 46, --, --, --]
4. Insert 52:
o Hash value: h(52)=52mod 10=2h(52) = 52 \mod 10 = 2h(52)=52mod10=2.
o Slot 222 is empty. Insert 525252 at index 222.
o Hash Table: [--, --, 52, 23, 34, --, 46, --, --, --]
5. Insert 33:
o Hash value: h(33)=33mod 10=3h(33) = 33 \mod 10 = 3h(33)=33mod10=3.
o Slot 333 is occupied by 232323 (collision occurs).
o Use linear probing: Check the next available slots (indices 4,54, 54,5, etc.).
o Slot 444 is occupied by 343434.
o Slot 555 is empty. Insert 333333 at index 555.
o Hash Table: [--, --, 52, 23, 34, 33, 46, --, --, --]
After inserting all the keys, the hash table looks like this:
[--, --, 52, 23, 34, 33, 46, --, --, --]
Q3.A)What is selection sort > sort the number following numbers in ascending order and also show
the worst case time complexity of selection sort is O(n2)?
Ans:-
1. Start with the first element and find the smallest element in the unsorted portion of the list.
2. Swap the smallest element with the first element of the unsorted portion.
3. Move to the next element and repeat the process until all elements are sorted.
Example: Sorting Numbers in Ascending Order
Step-by-Step Process:
1. First Pass:
o Find the smallest element in the array (111111) and swap it with the first element
(646464).
o Array after first pass: 11,25,12,22,6411, 25, 12, 22, 6411,25,12,22,64
2. Second Pass:
o Consider the unsorted portion (25,12,22,6425, 12, 22, 6425,12,22,64). The smallest
element is 121212. Swap it with the second element (252525).
o Array after second pass: 11,12,25,22,6411, 12, 25, 22, 6411,12,25,22,64
3. Third Pass:
o Consider the unsorted portion (25,22,6425, 22, 6425,22,64). The smallest element is
222222. Swap it with 252525.
o Array after third pass: 11,12,22,25,6411, 12, 22, 25, 6411,12,22,25,64
4. Fourth Pass:
o Consider the unsorted portion (25,6425, 6425,64). The smallest element is 252525,
which is already in place. No swap needed.
o Array after fourth pass: 11,12,22,25,6411, 12, 22, 25, 6411,12,22,25,64
5. Fifth Pass:
o The only element left (646464) is already sorted.
Key Points:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
Operations on the stack include:
Stack Details:
i) Push(f):
ii) Pop(top):
iii) Push(g):
iv) Push(h):
v) Pop(top):
vi) Push(i):
[a, b, c, d, e, i]
Situations Raised:
During operation iv (Push h), a stack overflow occurred because the stack was already full.
C) Explain How to implement two stack in one array A[I..N] in such a way that neither stack
overflow unless the total number of element in both the stacks together is N. Note that ,Push()
And Pop() operation should be run in 0(1) time ?
1. Neither stack overflows unless the total number of elements in both stacks equals NNN.
2. Push and Pop operations run in O(1)O(1)O(1) time.
We use a shared array where the two stacks grow from opposite ends of the array.
Approach
1. Array Layout:
o Use a single array A[0..N−1]A[0..N-1]A[0..N−1].
o Stack 1 grows from left to right starting at A[0]A[0]A[0].
o Stack 2 grows from right to left starting at A[N−1]A[N-1]A[N−1].
2. Pointers:
o Use two pointers:
top1top1top1: Points to the top of Stack 1. Initially set to −1-1−1.
top2top2top2: Points to the top of Stack 2. Initially set to NNN.
3. Conditions:
o The stacks are full when top1+1=top2top1 + 1 = top2top1+1=top2.
o The stacks are empty when:
top1=−1top1 = -1top1=−1 for Stack 1.
top2=Ntop2 = Ntop2=N for Stack 2.
Operations
Push Operation
Push in Stack 1:
python
Copy code
if top1 + 1 < top2:
top1 += 1
A[top1] = value
else:
raise Exception("Stack Overflow")
Push in Stack 2:
python
Copy code
if top1 + 1 < top2:
top2 -= 1
A[top2] = value
else:
raise Exception("Stack Overflow")
Pop Operation
Pop from Stack 1:
python
Copy code
if top1 >= 0:
value = A[top1]
top1 -= 1
return value
else:
raise Exception("Stack Underflow")
Pop from Stack 2:
python
Copy code
if top2 < N:
value = A[top2]
top2 += 1
return value
else:
raise Exception("Stack Underflow")
Illustration
Initial Configuration:
Array size N=10N = 10N=10, initially empty.
top1=−1top1 = -1top1=−1, top2=10top2 = 10top2=10.
After Operations:
1. Push(5) in Stack 1:
o top1=0top1 = 0top1=0, A=[5,−−,−−,−−,−−,−−,−−,−−,−−,−−]A = [5, --, --, --, --, --, --, --, --,
--]A=[5,−−,−−,−−,−−,−−,−−,−−,−−,−−].
2. Push(15) in Stack 2:
o top2=9top2 = 9top2=9, A=[5,−−,−−,−−,−−,−−,−−,−−,−−,15]A = [5, --, --, --, --, --, --, --, --,
15]A=[5,−−,−−,−−,−−,−−,−−,−−,−−,15].
3. Push(10) in Stack 1:
o top1=1top1 = 1top1=1, A=[5,10,−−,−−,−−,−−,−−,−−,−−,15]A = [5, 10, --, --, --, --, --, --, -
-, 15]A=[5,10,−−,−−,−−,−−,−−,−−,−−,15].
4. Push(20) in Stack 2:
o top2=8top2 = 8top2=8, A=[5,10,−−,−−,−−,−−,−−,−−,20,15]A = [5, 10, --, --, --, --, --, --,
20, 15]A=[5,10,−−,−−,−−,−−,−−,−−,20,15].
Complexity
1. Push: O(1)O(1)O(1), as we only adjust the pointer and insert the value.
2. Pop: O(1)O(1)O(1), as we only adjust the pointer and return the value.
This approach efficiently uses the shared array and prevents overflow unless the combined stack size
exceeds NNN.
Q4.A) What are the difference types of the linked list ? Give advantages and disadvantages each of
the linked list over another?
Advantages:
o Simpler to implement compared to other types.
o Uses less memory per node (only one pointer required).
o Efficient for operations like insertion and deletion at the beginning.
Disadvantages:
o Traversal is one-way, making reverse traversal impossible.
o Insertion and deletion at the end or middle require traversal from the head.
Advantages:
o Allows bidirectional traversal (both forward and backward).
o Easier to delete a node when a pointer to it is given (no need to traverse the list).
o More flexible for complex operations like inserting before a node.
Disadvantages:
o Requires more memory per node (two pointers: next and prev).
o Slightly more complex to implement due to maintaining two pointers per node.
Advantages:
o Efficient for applications requiring continuous loops, such as buffers or playlists.
o No need to reset pointers to NULL; traversal automatically cycles back.
o Allows insertion/deletion at the head or tail without additional traversal.
Disadvantages:
o Traversal requires additional care to avoid infinite loops.
o Difficult to implement and debug compared to singly linked lists.
Advantages:
o Combines the benefits of both circular and doubly linked lists.
o Allows efficient bidirectional traversal in a circular structure.
o Particularly useful for implementing advanced data structures like deques.
Disadvantages:
o Higher memory overhead due to maintaining both next and prev pointers.
o More complex to manage and maintain than singly or circular singly linked lists
B)Assume The following letters are inserted into an empty binary search tree in given order
J,B,D,F,N,K,O construct binary search tree and also give height of the tree?
Ans:- To construct a binary search tree (BST) from the given sequence of letters, let's
proceed step by step, following the BST property where the left child is smaller than the
parent node and the right child is greater than the parent node.
Given sequence: J, B, D, F, N, K, O
Step-by-Step Insertion:
1. Insert J:
o The tree is empty, so J becomes the root of the tree.
markdown
Copy code
J
2. Insert B:
o B is less than J, so B goes to the left of J.
css
Copy code
J
/
B
3. Insert D:
o D is greater than B but less than J, so D goes to the right of B.
css
Copy code
J
/
B
\
D
4. Insert F:
o F is greater than D but less than J, so F goes to the right of D.
css
Copy code
J
/
B
\
D
\
F
5. Insert N:
o N is greater than J, so N goes to the right of J.
mathematica
Copy code
J
/ \
B N
\
D
\
F
6. Insert K:
o K is less than N but greater than J, so K goes to the left of N.
mathematica
Copy code
J
/ \
B N
\ /
D K
\
F
7. Insert O:
o O is greater than N, so O goes to the right of N.
mathematica
Copy code
J
/ \
B N
\ / \
D K O
\
F
Final Binary Search Tree:
mathematica
Copy code
J
/ \
B N
\ / \
D K O
\
F
Height of the Tree:
The height of a tree is defined as the longest path from the root to a leaf. In this case:
The root is J.
The longest path is from J → N → O, which has 3 edges.
Therefore, the height of the tree is 3.
Final Answer:
C) What is threded binary trees? Give the threaded binary tree of the following binary tree?
D K
B J
In-Order Traversal:
B, D, F, J, K
Now, we need to add threads (or links to in-order predecessors and successors) for the null
pointers.
1. Node B:
o Left child: None → The left pointer of B points to null.
o Right child: D → The right pointer of B will point to D (its successor in the in-order
traversal).
2. Node D:
o Left child: B → The left pointer of D will point to B (its predecessor).
o Right child: F → The right pointer of D will point to F (its successor in the in-order
traversal).
3. Node F:
o Left child: D → The left pointer of F will point to D (its predecessor).
o Right child: J → The right pointer of F will point to J (its successor in the in-order
traversal).
4. Node J:
o Left child: None → The left pointer of J will point to F (its predecessor).
o Right child: K → The right pointer of J will point to K (its successor in the in-order
traversal).
5. Node K:
o Left child: J → The left pointer of K will point to J (its predecessor).
o Right child: None → The right pointer of K will point to null (no successor).
css
Copy code
F
/ \
D------K
/ \
B---->J---->NULL
Explanation:
B: The left pointer is null (no predecessor), and the right pointer is threaded to D.
D: The left pointer is threaded to B (its predecessor), and the right pointer is threaded to F.
F: The left pointer is threaded to D (its predecessor), and the right pointer is threaded to J.
J: The left pointer is threaded to F (its predecessor), and the right pointer is threaded to K.
K: The left pointer is threaded to J (its predecessor), and the right pointer is null (no
successor).
Q5. A)
Ans:-
iii)b tree
iv)b+tree
A Red-Black Tree is a balanced binary search tree with an additional color property for each
node (either red or black) to ensure that the tree remains balanced, leading to efficient
operations.
Key Properties:
Red-Black trees are useful in scenarios where efficient insertion and deletion are important,
such as in associative containers (maps, sets) in C++'s Standard Template Library (STL).
An M-Way Search Tree (also called a Multi-way Tree) is a generalization of the binary
search tree where each node can have up to M children (instead of just 2). This allows the
tree to have more branches, which can be helpful in improving the efficiency of searching
and sorting operations.
Key Features:
iii) B Tree
A B Tree is a self-balancing search tree that generalizes a binary search tree by allowing
nodes to have more than two children. It is commonly used in databases and file systems to
store sorted data and support efficient insertions, deletions, and searches.
Key Properties:
iv) B+ Tree
A B+ Tree is an extension of the B tree that stores all actual data in the leaf nodes and uses
internal nodes only for routing purposes. It is frequently used in database indexing because of
its efficiency in range queries.
Key Properties:
v) Sparse Matrix
A Sparse Matrix is a matrix in which most of the elements are zero or near-zero. Storing
sparse matrices as regular dense matrices (which store every element, including zeroes) can
be inefficient in terms of memory usage. Special data structures are used to only store the
non-zero elements, significantly reducing memory consumption.
Key Features:
Only non-zero elements are stored, often using formats like Compressed Sparse Row (CSR)
or Compressed Sparse Column (CSC).
Typically used in scientific computing, image processing, and machine learning where
matrices tend to have many zero values.
An AVL Tree is a type of self-balancing binary search tree (BST) where the difference in
heights between the left and right subtrees (called the balance factor) of any node is at most
1. This ensures that the tree remains balanced, providing efficient search, insertion, and
deletion operations.
Key Properties:
For each node, the balance factor (height of left subtree - height of right subtree) is -1, 0, or
+1.
If the balance factor becomes outside this range, rotations (left or right) are performed to
restore balance.
The AVL tree guarantees that operations such as search, insertion, and deletion are O(log n),
making it efficient for dynamic sets of ordered data.