Data Structure
Data Structure
Q2. What are the problems with singly linked list that are overcome by doubly linked list? Explain how a
node can be inserted in a doubly linked list.
Diagram:
Before Insertion:
[A] <-> [B]
After Inserting C:
[A] <-> [C] <-> [B]
Q3. b) Write short notes on following:
i) Linked List representations
ii) Indirect addressing
Memory Representation:
In memory, nodes are not stored in contiguous locations. Instead, they are scattered, and pointers are used to
maintain the sequence.
Diagram (Singly Linked List):
[Data | Next] → [Data | Next] → [Data | NULL]
This dynamic memory allocation and flexibility make linked lists useful in implementing stacks, queues,
graphs, etc.
Key Points:
Offers flexibility in accessing data.
Common in pointer-based programming (e.g., C/C++).
Widely used in arrays, linked lists, trees, and dynamic structures.
Enables dynamic memory usage and complex data management.
Example:
If register R contains address 500, and memory at address 500 has value 42,
then using indirect addressing, we access value 42 indirectly through address 500.
Diagram:
Instruction → [Address 500] → [Memory = 42]
Indirect addressing is crucial in linked lists, function pointers, and memory management in operating
systems.
Q.3 a) Write short notes on following:
i) Abstract Data Type
ii) Derived classes and inheritance
Q4. What is a Stack Data Structure? Write procedures for significant operations. Explain Stack
Overflow and Underflow.
STACK OVERFLOW
Happens when you try to push an element into a full stack.
Example:
o Stack size = 5
Result: Memory can’t store the extra element. Program may crash or give error.
STACK UNDERFLOW
Happens when you try to pop from an empty stack.
Example:
o Stack is empty (TOP = -1)
APPLICATIONS OF STACK:
Expression evaluation (postfix/infix).
Undo operations in editors.
Function call management (call stack).
Backtracking problems (maze, recursion).
Diagram:
Stack (Top to Bottom)
[40] ← Top
[30]
[20]
[10]
Push(50):
[50] ← Top
[40]
...
Pop():
Removes [50], new top is [40]
Q5. What is Binary Tree traversal? Explain various methods of tree traversals.
Inorder: D → B → E → A → C
Applications:
Inorder: Retrieves values in sorted order (for BSTs).
Preorder: Used for copying a tree.
Postorder: Used to delete a tree or evaluate postfix expressions.
Level Order: Used in Breadth-First Search.
Q6. Write short notes on following:
i) Priority Queues
ii) Tournament Trees
i) Priority Queues
A Priority Queue is an abstract data structure that supports the operations of a queue with an added feature:
each element has a priority associated with it. Elements are dequeued in order of their priority, with the
element having the highest priority being dequeued first. If two elements have the same priority, they are
dequeued in the order they were added (this behavior can vary depending on the implementation).
Operations:
1. Insert: Add an element to the queue with a given priority.
2. Remove: Remove the element with the highest priority.
3. Peek: View the element with the highest priority without removing it.
Types of Priority Queues:
Max Priority Queue: The element with the largest value is dequeued first.
Min Priority Queue: The element with the smallest value is dequeued first.
Implementation:
Priority queues can be implemented using:
Array/Linked List: Simple but inefficient for large datasets (O(n) for insertion and removal).
Binary Heap: More efficient, where insertion and removal both take O(log n) time.
Fibonacci Heap: Provides even better time complexity for certain operations like decrease-key.
ii) Tournament Trees
A Tournament Tree is a type of binary tree used to find the winner in a series of matches, where each match
involves two players, and the winner advances to the next level. It is a specialized data structure for
problems such as finding the k-th largest element in an array or efficiently implementing sorting algorithms.
In a tournament tree:
Each leaf node represents a player or an element.
Each internal node represents a match, where the winner is the element with the higher value.
The root node holds the winner of the tournament (the maximum or minimum element, depending on
the problem's requirements).
Applications:
Selection algorithms: Find the k-th largest element in an array.
Sorting algorithms: Used in tournament sort for comparison-based sorting.
Game theory: Simulate knockout rounds of tournaments.
Complexity:
Build Tournament Tree: O(n) time.
Find Winner (Root): O(1) time.
Update (after match results): O(log n) time.
Diagram:
(Root)
/ \
(Match) (Match)
/ \ / \
(Leaf) (Leaf)(Leaf)(Leaf)
In this example, each match node compares the winners of its two child nodes and passes the winner up to
its parent, ultimately determining the overall winner at the root.
Q7. Write short notes on following:
i) AVL Trees
ii) Node structure in B-Tree
Ans. i) AVL Trees
An AVL Tree is a type of self-balancing binary search tree (BST) where the difference in height
(balance factor) between the left and right subtrees of any node is at most 1.
Key Features:
Maintains O(log n) time complexity for insertion, deletion, and search operations.
Balance Factor:
For AVL Tree, balance factor must be -1, 0, or +1 for all nodes.
Rotations to Balance:
Diagram:
Before LL Rotation:
30
/
20
/
10
After LL Rotation:
20
/ \
10 30
A B-Tree is a self-balancing multi-way search tree used primarily in databases and file systems for
efficiently handling large volumes of data stored on disks.
Node Structure:
Properties:
[20]
/ \
[10] [30, 40]
Here:
B-Trees are optimal for disk-based storage systems due to their low height and high branching factor.
Comparison Table:
Suitable For Shortest path in unweighted graphs Topological sort, maze solving
Feature BFS DFS
Conclusion:
BFS is ideal for finding the shortest path in unweighted graphs.
DFS is suitable for exploring components, cycle detection, and topological sorting.
Both methods are fundamental for solving a wide range of problems in computer science and real-world
applications.
B. Hashing
Hashing is a technique to map data (keys) to a fixed-size table (called a hash table) using a hash function.
Hash Function:
A function that converts a key into an index in the hash table.
Example: index = hash(key) % table_size
Collision Resolution Techniques:
1. Chaining: Store multiple elements at the same index using linked lists.
2. Open Addressing: Find another empty slot using methods like:
o Linear probing
o Quadratic probing
o Double hashing
Together, skip lists and hashing provide efficient methods for managing and accessing data in dynamic
applications.
Q10. Define a graph. What are important applications of Graph? Explain different representation of
Graph in memory using suitable examples.
Definition of Graph:
A graph is a non-linear data structure consisting of a set of vertices (nodes) and a set of edges that connect
pairs of vertices.
Mathematically, a graph is represented as:
G=(V,E)G = (V, E)
Where:
VV = Set of vertices
EE = Set of edges connecting the vertices
Applications of Graphs:
1. Social Networks – Users as nodes, relationships as edges.
2. Google Maps / GPS – Locations as vertices, roads as edges.
3. Computer Networks – Routers and computers as nodes, connections as edges.
4. Web Page Linking – Web pages as nodes, hyperlinks as edges.
5. Project Scheduling – Tasks and dependencies modeled using graphs.
6. Recommendation Systems – Users and products as graph elements.
7. Electric Circuits – Components and wires modeled as graphs.
1. Adjacency Matrix:
A 2D array of size V×VV \times V
Each cell matrix[i][j] = 1 if there is an edge from vertex i to j, else 0
Example:
Graph:
A
/\
B C
Vertices: A=0, B=1, C=2
ABC
A0 1 1
B1 0 0
C1 0 0
Advantages:
Easy to implement
Good for dense graphs
Disadvantages:
Takes O(V²) space even for sparse graphs
2. Adjacency List:
Uses an array of lists
Each vertex stores a list of its adjacent vertices
Example:
Graph:
A→B
A→C
B→D
Adjacency List:
A→B→C
B→D
C→
D→
Advantages:
Saves space for sparse graphs
Efficient neighbor traversal
Disadvantages:
Slower for checking direct edge existence between nodes
Diagram Comparison:
Graph: Adjacency Matrix: Adjacency List:
A ABCD A→B→C
/\ ------- B→D
B C A|0 1 1 0 C→
| B|1 0 0 1 D→
D C|1 0 0 0
D|0 1 0 0
Conclusion:
Graph representation depends on the type of graph (sparse/dense) and the operations required.
Use adjacency matrix for quick edge lookup
Use adjacency list for efficient memory usage in sparse graphs
Define following terms with illustration:
i) Digraph
ii) Complete graph
iii) Acyclic graph
iv) Weighted graph
v) Degree of vertex
vi) Indegree
vii) Outdegree
viii) Simple path
Ans.
i) Digraph (Directed Graph):
A digraph is a graph in which each edge has a direction, i.e., it goes from one vertex to another.
Example:
A→B→C
vi) Indegree:
In a directed graph, indegree of a vertex is the number of edges coming into it.
Example:
In
A→B←C
indegree(B) = 2
vii) Outdegree:
In a directed graph, outdegree of a vertex is the number of edges going out from it.
Example:
In
A→B←C
outdegree(A) = 1
Weighted: A --3--> B
Indegree: A → B ← C (indeg(B)=2)
Outdegree: A → B ← C (outdeg(A)=1)