Adva
Adva
Key Features:
Advantages:
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.
A Singly Linked List (SLL) stores only one pointer (next), which leads to several limitations:
Problems in SLL:
This allows bidirectional traversal, and makes insertion and deletion easier.
Steps:
2. Set C.prev = A
3. Set C.next = B
4. Set A.next = C
5. Set B.prev = C
Diagram:
Before Insertion:
After Inserting C:
Doubly Linked List: Two pointers – one to next and one to previous node.
Circular Linked List: Last node points back to the first node.
Memory Representation:
In memory, nodes are not stored in contiguous locations. Instead, they are scattered, and pointers are used to
maintain the sequence.
Indirect Addressing is a memory addressing technique where the address of the data is not directly given,
but stored in another location (like a register or memory cell), which is then accessed to get the actual data.
Key Points:
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:
An Abstract Data Type (ADT) is a mathematical model for a certain class of data structures that defines
the type of data stored, operations allowed, and the behavior of these operations, without specifying
implementation.
Key Characteristics:
Abstraction: Hides internal structure, shows only what the object does.
Examples:
Stack ADT: Operations like push(), pop(), and peek().
Queue ADT: Operations like enqueue(), dequeue().
Top → [40]
[30]
[20]
[10]
Inheritance is a feature of Object-Oriented Programming (OOP) that allows a class to inherit the
properties and behavior (methods) of another class.
Types of Inheritance:
Advantages:
Example:
If a base class Animal has a function speak(), the derived class Dog can use or override this function.
Diagram:
[Class Animal]
|
[Class Dog inherits Animal]
Inheritance makes programs modular, reduces redundancy, and supports real-world modeling using class
hierarchies.
Q4. What is a Stack Data Structure? Write procedures for significant operations. Explain Stack
Overflow and Underflow.
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. This means the last
element inserted into the stack is the first one to be removed.
Index → 0 1 2 3 4
[10][20][30][40][50] ← Top
if TOP == MAX - 1
TOP ← TOP + 1
stack[TOP] ← item
PROCEDURE FOR POP OPERATION:
Procedure POP(stack)
if TOP == -1
else
item ← stack[TOP]
TOP ← TOP - 1
return item
STACK OVERFLOW
Example:
o Stack size = 5
Result: Memory can’t store the extra element. Program may crash or give error.
STACK UNDERFLOW
APPLICATIONS OF STACK:
Diagram:
[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.
Binary Tree Traversal is the process of visiting each node in a binary tree exactly once in a systematic way.
Since trees are hierarchical structures (not linear like arrays), we need specific traversal techniques.
In DFT, we go deep in the tree structure before backtracking. It is further classified into:
[A]
/ \
[B] [C]
/\
[D] [E]
Inorder: D → B → E → A → C
Example:
Preorder: A → B → D → E → C
Example:
Postorder: D → E → B → C → A
In Level Order Traversal, we visit nodes level by level from top to bottom and left to right at each level. This
traversal uses a Queue.
Example:
Level Order: A → B → C → D → E
[A]
/ \
[B] [C]
/\
[D] [E]
Summary Table:
Applications:
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.
3. Peek: View the element with the highest priority without removing it.
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 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:
Complexity:
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.
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:
1. Every node can have at most m children, where m is the order of the B-Tree.
2. Every internal node (except root) has at least ⌈m/2⌉ children.
3. Keys within a node are sorted in ascending order.
4. All leaves appear at the same level (height-balanced).
5. The root has at least two children unless it is a leaf.
Example Diagram (Order 3 B-Tree):
[20]
/ \
[10] [30, 40]
Here:
B-Trees are optimal for disk-based storage systems due to their low height and high branching factor.
Graph search methods are algorithms used to traverse or search a graph's nodes (vertices) and edges in a
systematic way. The two most common graph search algorithms are:
Definition:
BFS explores the graph level by level, visiting all neighbors of a node before moving to the next level of
nodes.
Steps:
Example:
/\
B C
/ \
D E
Level 0: A
Level 1: B, C
Level 2: D, E
Definition:
DFS explores as far as possible along one branch before backtracking. It goes deep into the graph before
exploring other branches.
Steps:
Example:
/\
B C
/ \
D E
Start at A
→ Go to B
→ Backtrack to B → A
→ Go to C
→ Go to E
Comparison Table:
Suitable For Shortest path in unweighted graphs Topological sort, maze solving
Conclusion:
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 C
/ \
D E
BFS: A → B → C → D → E
DFS: A → B → D → C → E
Graph Iterators are special tools or objects used to systematically traverse elements (nodes or edges) of a
graph data structure, similar to how array or list iterators work in linear data structures.
3. Adjacency Iterator: Iterates over all adjacent vertices (neighbors) of a given vertex.
Purpose:
Example (Python-like):
print(vertex)
print(edge)
Applications:
Topological sort
A Skip List is a probabilistic data structure that allows fast search, insertion, and deletion operations —
similar to balanced trees but simpler to implement.
Structure:
Each element may appear in multiple levels depending on random coin flips.
Advantages:
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
1. Chaining: Store multiple elements at the same index using linked lists.
o Linear probing
o Quadratic probing
o Double hashing
Diagram (Chaining):
Index 0: → 20 → 30
Index 1: → 21
Index 2: → 42 → 72
Applications:
Caches
Databases (indexing)
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.
G=(V,E)G = (V, E)
Where:
VV = Set of vertices
1. Adjacency Matrix:
Example:
Graph:
A
/\
B C
ABC
A0 1 1
B1 0 0
C1 0 0
Advantages:
Easy to implement
Disadvantages:
Takes O(V²) space even for sparse graphs
2. Adjacency List:
Example:
Graph:
A→B
A→C
B→D
Adjacency List:
A→B→C
B→D
C→
D→
Advantages:
Disadvantages:
Diagram Comparison:
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.
Ans.
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
A complete graph is a graph in which every pair of distinct vertices is connected by a unique edge.
Example (K4):
A —— B
|\ /|
|\/|
D —— C
Example:
A→B→C
A --5--> B --2--> C
v) Degree of Vertex:
In an undirected graph, the degree of a vertex is the number of edges incident to it.
Example:
In
A—B—C
deg(B) = 2
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
In
A→B→C→D
Weighted: A --3--> B
Indegree: A → B ← C (indeg(B)=2)
Outdegree: A → B ← C (outdeg(A)=1)