data structure cia notes
data structure cia notes
data structure cia notes
6. What is node?
7. Define PEEK
8. What is Underflow?
16. How to Insert and Delete an Element in the Circular Linked List.
ANSWERS:
Example:
int arr[10];
4. Applications of Array
● Used for storing and manipulating data in sorting and searching algorithms.
● Used to implement data structures like stacks, queues, and hash tables.
A Circular Linked List is a linked list where the last node is connected back to the first node,
forming a circle. This can be implemented for both singly and doubly linked lists.
6. What is a Node?
A node is a fundamental unit of a data structure (like a linked list or tree) that contains:
PEEK is an operation in a stack that retrieves the top element without removing it from the
stack.
8. What is Underflow?
Underflow occurs when an attempt is made to remove an element from an empty data
structure (e.g., popping from an empty stack).
A Binary Tree is a hierarchical data structure where each node has at most two children,
referred to as the left child and the right child.
The root node is the topmost node in a tree data structure from which all other nodes
descend. It has no parent.
1. Implementing databases.
2. Handling large datasets in machine learning algorithms.
3. Storing and organizing web browser history.
4. Optimizing routing in network systems.
5. Managing memory in operating systems.
A Doubly Linked List is a type of linked list where each node contains:
Advantages:
Insertion:
1. At the Beginning:
Deletion:
1. At the Beginning:
Tree traversal is the process of visiting all nodes in a tree in a specific order. The main types
of traversal are:
1. Inorder (Left → Root → Right): Used in binary search trees for sorted output.
2. Preorder (Root → Left → Right): Used for creating copies of trees.
3. Postorder (Left → Right → Root): Used in deleting a tree.
4. Level Order (Breadth-first): Visits nodes level by level, starting from the root.
1. Linked Representation:
Example:
struct Node {
int data;
struct Node* left;
struct Node* right;
};
○
2. Array Representation:
○Represented as a list of lists, where each list corresponds to a node and its
children.
4. Parent Array Representation:
○ Each element in the array represents the parent of the corresponding node.
A Singly Linked List is a linear data structure where each element (node) is connected to
the next by a pointer. It consists of nodes, and each node has two parts:
Features:
Operations:
1. Insertion:
○ At the beginning: Update the new node's pointer to point to the head, then
update the head to the new node.
○ At the end: Traverse to the last node, update its pointer to the new node.
○ At a specific position: Adjust the pointers of adjacent nodes to insert the new
node.
2. Deletion:
Advantages:
Disadvantages:
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The
last element added is the first one to be removed.
Representation:
Operations:
Push: Adds an element to the top of the stack.
Example:
stack[++top] = element;
1.
element = stack[top--];
2.
3. Peek/Top: Retrieves the top element without removing it.
Applications:
Steps:
1. Initialize:
Example:
Infix: (A + B) * C
Postfix: A B + C *
Applications:
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. The
first element added is the first one to be removed.
Representation:
● Array-Based Implementation: Fixed size; uses two pointers (front and rear).
● Linked List Implementation: Dynamic size; uses nodes with pointers for front and
rear.
Operations:
Enqueue: Adds an element to the rear of the queue.
Example:
queue[rear++] = element;
1.
Dequeue: Removes an element from the front of the queue.
Example:
element = queue[front++];
2.
3. IsEmpty: Checks if the queue is empty.
4. IsFull: Checks if the queue is full (in the case of an array implementation).
Types:
● Circular Queue: Rear connects to the front when the end is reached.
● Priority Queue: Elements are dequeued based on priority.
● Double-Ended Queue (Deque): Insertions and deletions can occur at both ends.
Applications:
A Binary Tree is a hierarchical data structure where each node has at most two children.
1. Array Representation:
Array: [A, B, C]
2. Linked Representation:
struct Node {
int data;
struct Node* left;
struct Node* right;
};
● An array where each element represents the parent of the corresponding node.
Example:
For the tree:
A
/\
B C
Applications:
PART C (3 x 10 = 30 Marks)
ANSWERS:
A sparse matrix is a matrix in which most of the elements are zero. It is typically stored
using efficient data structures to save space, such as arrays or linked lists for non-zero
elements.
2. Define Data Structure.
A data structure is a way of organizing and storing data in a computer so that it can be
accessed and used efficiently. Examples include arrays, linked lists, stacks, queues, trees,
and graphs.
A circular linked list is a type of linked list where the last node points back to the first node,
forming a circle. It can be singly or doubly linked.
The header in a linked list is a special node at the beginning of the list. It often contains
metadata like the size of the list or serves as a starting point for traversal.
A binary tree is a hierarchical data structure in which each node has at most two children,
referred to as the left and right child.
Tree traversal is the process of visiting all the nodes in a tree in a specific order. The main
types are:
A node is a fundamental unit of a data structure, such as a tree or linked list. It contains:
The balance factor of a node in an AVL tree is the difference between the heights of its left
and right subtrees.
Balance Factor=Height of Left Subtree−Height of Right Subtree\text{Balance Factor} =
\text{Height of Left Subtree} - \text{Height of Right Subtree}
It ensures that the tree remains balanced.
A directed graph (digraph) is a graph where each edge has a direction, going from one
vertex to another. Edges are represented as ordered pairs of vertices.
Breadth First Search (BFS) is a graph traversal algorithm that explores all neighbors of a
node before moving to the next level.
1. One-Dimensional Array:
1. Initialize an empty stack for operators and an empty string for the output.
2. Scan the infix expression from left to right.
3. For each character:
○ If it’s an operand, add it to the output string.
○ If it’s an operator:
■ Pop operators from the stack to the output until the stack is empty or
an operator of lower precedence is found.
■ Push the current operator onto the stack.
○ If it’s a left parenthesis, push it onto the stack.
○ If it’s a right parenthesis, pop and add to the output until a left parenthesis is
encountered, then discard the parenthesis.
4. After scanning, pop all remaining operators from the stack to the output.
● The last node points back to the first node, forming a loop.
● It can be either singly or doubly linked.
Advantages:
1. Insertion: Adjust pointers of the last and new nodes to maintain the circular nature.
2. Deletion: Adjust pointers to bypass the deleted node.
1. Array Representation:
○ Dynamic size.
○ Each node contains:
■ Data.
■ A pointer to the next node.
○ Front and Rear pointers are used to track the first and last nodes.
3. Circular Queue Representation:
1. Undirected Graph:
○ Contains no cycles.
7. Connected Graph:
Applications:
● BFS: Used in shortest path algorithms.
● DFS: Used in cycle detection and topological sorting.
In graph theory, levels often refer to the distance from a starting node during traversal,
especially in BFS:
Example:
● Level 0: A.
● Level 1: Neighbors of A.
● Level 2: Neighbors of Level 1 nodes.
Levels are critical in algorithms like BFS and shortest path finding in unweighted graphs.
Data structures are essential in various fields of computer science and software
development. Here are some key applications:
1. Arrays:
○ Used in matrix operations, image processing, and as building blocks for more
complex data structures like heaps and hash tables.
○ Examples: Storing game boards, database records.
2. Linked Lists:
Characteristics:
1. Insertions and deletions occur only at one end called the top.
2. Useful for tasks where data must be reversed or backtracked.
Operations:
stack[++top] = value;
2.
3. Pop: Removes the top element from the stack.
value = stack[top--];
4.
5. Peek/Top: Retrieves the top element without removing it.
Applications:
Types:
○First visits the left subtree, then the root, followed by the right subtree.
○Example: Binary Search Trees (BSTs) produce sorted order.
○Steps:
1. Traverse the left subtree.
2. Visit the root node.
3. Traverse the right subtree.
2. Preorder Traversal (Root → Left → Right):
Applications:
● Inorder: Sorting.
● Preorder: Generating trees.
● Postorder: Deleting trees.
23. AVL Tree
An AVL Tree is a self-balancing binary search tree where the difference in height between
the left and right subtrees (balance factor) is at most 1 for all nodes.
Characteristics:
Operations:
1. Insertion:
Applications:
Dijkstra's Algorithm is used to find the shortest path from a source node to all other nodes
in a weighted graph.
Steps:
1. Initialize:
○ Set the distance to the source node as 0 and all other nodes to infinity.
○ Mark all nodes as unvisited.
2. Select Node:
○
For each neighbor of the current node, calculate the tentative distance:
Tentative Distance=Current Distance+Edge Weight\text{Tentative Distance} =
\text{Current Distance} + \text{Edge Weight}
○ If the tentative distance is smaller, update it.
4. Mark as Visited:
○ Once all neighbors are processed, mark the current node as visited.
5. Repeat:
○ Continue until all nodes are visited or the shortest paths are found.
Example:
For a graph:
A→B(1),B→C(2),A→C(4)A → B (1), B → C (2), A → C (4)
Applications: