All Algorithms (2 Files Merged)
All Algorithms (2 Files Merged)
Ans) A stack is a liner data structure which uses LIFO principal to store data in an array here is
the algorithm for stack implementation:
Algorithm: CreateStack
Algorithm: Push(x)
2. Increment top by 1
Algorithm: Pop
3. Decrement top by 1
Algorithm: Peek
Algorithm: IsFull
Algorithm: IsEmpty
2. For i = 0 to top:
Ans) A queue is a linear data structure it uses FIFO concept to input and output data it is mostly
implemented on arrays here is its algorithm:
Algorithm: CreateQueue
Algorithm: IsFull
1. Return (rear == MAX - 1) // Queue is full if rear reaches the last index
Algorithm: IsEmpty
1. Return (front == -1 or front > rear) // Queue is empty if front is -1 or front > rear
Algorithm: Peek
Algorithm: Enqueue(x)
3. Increment rear by 1
Algorithm: Dequeue
4. If front > rear, set front = rear = -1 // Reset queue if it becomes empty
Algorithm: PrintQueue
- Print arr[i] followed by " -> " // Print each element in the queue
Q3)
All lectures Up-to Mid:
L1:
1) What is DSA?
DSA (Data Structures and Algorithms) is the study of optimization techniques used to store and
organize data efficiently through models and techniques, enabling faster and more effective
problem-solving.
- Stack
- Queue and types of queues (like circular queue, priority queue, deque, etc.)
3) What is an Algorithm?
Breaking down a complex task into simple steps and then implementing it as a whole is called
an algorithm. It is like a design stage if we compare it to software engineering
In these, data is stored sequentially. Example: Array, Stack, Queue, Linked List.
In these, data is stored hierarchically or irregularly. Example: Tree, Graph, Heap, HashMap.
5) What is Linear Data Structure?
When the data is stored in a sequential manner, it is called a linear data structure.
When the data is stored in a tree-like or irregular manner, it is called a non-linear data structure.
L2:
- Asymptotic analysis studies how the running time of an algorithm changes with input size.
Example:
If an array has 10 elements in reverse order, sorting them will take multiple passes.
The time grows with input size, and asymptotic analysis helps predict this growth.
2) Stack - Basic Concepts
Important Points:
- Stack has one open end (for push/pop) and three closed sides.
- "Top" is not a pointer to an address — it's more like an index or a flag to show the current top
position.
- On pushing:
- `Top++`
- On popping:
- Return `stack[Top]`
- Then `Top--`
L3:
Queue Operations:
1. Peek:
- Return the element at the front of the queue (without removing it).
2. IsFull:
3. IsEmpty:
4. Enqueue(x):
- Check if the queue is full.
5. Dequeue:
- Remove the element from the front (store the value arr[Front]).
- If Front > Rear, reset both Front and Rear to -1 (empty queue).
Queue Algorithm:
1. CreateQueue:
2. Enqueue:
3. Dequeue:
- Increment Front by 1.
5. IsFull:
6. IsEmpty:
Important Terms:
Important Points:
Applications of Trees:
Preorder Traversal:
Inorder Traversal:
Postorder Traversal:
A tree in which every node has at most two children is called a Binary Tree.
Every parent node either has exactly two children or no children at all.
If all internal nodes have exactly two children and all leaf nodes are at the same depth, the tree
is called a Perfect Binary Tree.
1) Every level must be completely filled except possibly the last level.
Internal Node:
Q1) What is a Binary Search Tree (BST) and what do you need to know to understand it:
In a BST, data is stored such that if the value is greater than root, it is stored on the right; if
smaller, it is stored on the left. Every node is a root of its own subtree.
if root==NULL
return NULL
if number==root->data
return root->data
return search(root->left)
return search(root->right)
Insertion algorithm:
if root==NULL
else
Deletion algorithm:
if root==NULL
return NULL
root->left=delete(root->left,number)
root->right=delete(root->right,number)
else
else
delete successor
Traversal in BST:
Inorder traversal:
- Visit root
Successor:
The minimum value node from the right subtree is called the inorder successor.