0% found this document useful (0 votes)
6 views15 pages

All Algorithms (2 Files Merged)

The document provides definitions and algorithms for two linear data structures: Stack and Queue, emphasizing their operations such as Push, Pop, Enqueue, and Dequeue. It also covers concepts of Data Structures and Algorithms (DSA), including types of data structures, asymptotic analysis, and tree structures like Binary Trees and Binary Search Trees (BST). Additionally, it explains traversal methods and operations for BST, including insertion and deletion algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

All Algorithms (2 Files Merged)

The document provides definitions and algorithms for two linear data structures: Stack and Queue, emphasizing their operations such as Push, Pop, Enqueue, and Dequeue. It also covers concepts of Data Structures and Algorithms (DSA), including types of data structures, asymptotic analysis, and tree structures like Binary Trees and Binary Search Trees (BST). Additionally, it explains traversal methods and operations for BST, including insertion and deletion algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1) Define Stack data structure and write down its algorithm?

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

1. Initialize top = -1 // Stack is empty

Algorithm: Push(x)

1. If top == MAX - 1, print "Stack is full!" and return

2. Increment top by 1

3. Set arr[top] = x // Place element at the top

Algorithm: Pop

1. If top == -1, print "Stack is empty!" and return -1

2. Store the element arr[top] in a variable

3. Decrement top by 1

4. Return the stored element

Algorithm: Peek

1. If top == -1, print "Stack is empty!" and return -1

2. Return arr[top] // Return the top element

Algorithm: IsFull

1. Return (top == MAX - 1) // True if stack is full

Algorithm: IsEmpty

1. Return (top == -1) // True if stack is empty


Algorithm: PrintStack

1. If top == -1, print "Stack is empty!" and return

2. For i = 0 to top:

- Print arr[i] followed by " -> "

3. Print "End of Stack"

Q2) Define linear queue and also give its algorithm?

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

1. Initialize front = -1 and rear = -1 // Queue is empty

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

1. If IsEmpty() is true, return -1 // Queue is empty

2. Return arr[front] // Return the front element of the queue

Algorithm: Enqueue(x)

1. If IsFull() is true, return -1 // Queue is full


2. If front == -1, set front = 0 // If first element is being added, set front to 0

3. Increment rear by 1

4. Set arr[rear] = x // Add x to the queue at position rear

5. Return 0 // Enqueue successful

Algorithm: Dequeue

1. If IsEmpty() is true, return -1 // Queue is empty

2. Set dequeued_element = arr[front] // Store the front element

3. Increment front by 1 // Move front to the next element

4. If front > rear, set front = rear = -1 // Reset queue if it becomes empty

5. Return dequeued_element // Return the dequeued element

Algorithm: PrintQueue

1. If IsEmpty() is true, return -1 // Queue is empty

2. For i = front to rear:

- Print arr[i] followed by " -> " // Print each element in the queue

3. Print "Nothing Ahead!" // Indicate the end of 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.

2) What will we study in DSA?

- Stack

- Queue and types of queues (like circular queue, priority queue, deque, etc.)

- Linked lists and its types

- Trees and its types

- Maps and its types

- Searching and sorting techniques

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

4) What are DSA types?

Data Structures are mainly divided into two types:

- Linear Data Structures:

In these, data is stored sequentially. Example: Array, Stack, Queue, Linked List.

- Non-Linear Data Structures:

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.

Examples: Array, Stack, Queue, Linked List, etc.

- It has a one-on-one relationship between elements.

- Each element follows a single path.

6) What is Non-Linear Data Structure?

When the data is stored in a tree-like or irregular manner, it is called a non-linear data structure.

Examples: Tree, Graph, Heap, Map, etc.

- It has one-to-many or many-to-many relationships between elements.

- Each node can be connected to multiple nodes.

L2:

1) Asymptotic Analysis: Big-O, Omega, Theta

- Asymptotic analysis studies how the running time of an algorithm changes with input size.

- Three major notations:

- Big-O (O): Upper Bound (Worst Case)

- Omega (Ω): Lower Bound (Best Case)

- Theta (Θ): Tight Bound (Average Case)

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

- Stack follows LIFO (Last In First Out) principle.

- Two main operations:

- Push(): Add an element

- Pop(): Remove the top element

- Other common functions:

- IsEmpty(): Checks if the stack is empty

- IsFull(): Checks if the stack is full

- Peek(): Looks at the top element without removing it

Important Points:

- Stack has one open end (for push/pop) and three closed sides.

- If a stack is full, pushing another element causes an overflow error.

- If a stack is empty and a pop is attempted, underflow error occurs.

- "Top" is not a pointer to an address — it's more like an index or a flag to show the current top
position.

3) Working of Stack in DSA

- A variable called `Top` tracks the current top position.

- Stack initialized with `Top = -1` meaning it is empty.

- On pushing:

- `Top++`

- Place the new element at `stack[Top]`

- On popping:
- Return `stack[Top]`

- Then `Top--`

- Stack becomes empty again when `Top == -1`.

L3:

1) Queue Characteristics, Operations, Algorithm?

1. A Queue follows the FIFO (First In First Out) principle.

2. It has two ends: Front and Rear.

3. Front: Where elements are removed (Dequeued).

4. Rear: Where elements are added (Enqueued).

5. It is an abstract data structure (ADS).

Queue Operations:

1. Peek:

- Return the element at the front of the queue (without removing it).

2. IsFull:

- If Rear == MAX - 1, the queue is full.

- Return True if the queue is full.

3. IsEmpty:

- If Front == -1, the queue is empty.

- Return True if the queue is empty.

4. Enqueue(x):
- Check if the queue is full.

- If it's the first element, set Front = 0.

- Increment the Rear pointer.

- Insert the element at the rear of the queue (arr[Rear] = x).

5. Dequeue:

- Check if the queue is empty (Front == -1).

- Remove the element from the front (store the value arr[Front]).

- Increment the Front pointer.

- If Front > Rear, reset both Front and Rear to -1 (empty queue).

Queue Algorithm:

1. CreateQueue:

- Set Front = -1 and Rear = -1 (initial empty state).

2. Enqueue:

- If Rear == MAX - 1, print "Queue is full!".

- If Front == -1, set Front = 0 (first element).

- Increment Rear by 1 and set arr[Rear] = x.

3. Dequeue:

- If Front == -1, print "Queue is empty!" and return -1.

- Store the element at arr[Front].

- Increment Front by 1.

- If Front > Rear, reset Front and Rear to -1.


4. Peek:

- If Front == -1, print "Queue is empty!" and return -1.

- Return arr[Front] (front element).

5. IsFull:

- Return (Rear == MAX - 1) (True if full).

6. IsEmpty:

- Return (Front == -1) (True if empty).

Queue Example Walkthrough:

1. Enqueue 10 → Front = 0, Rear = 0

2. Enqueue 12 → Front = 0, Rear = 1

3. Enqueue 13 → Front = 0, Rear = 2

4. Dequeue → Front = 1, Rear = 2 (10 removed)

5. Enqueue 15 → Front = 1, Rear = 3

6. Dequeue → Front = 2, Rear = 3 (12 removed)

7. Enqueue 18 → Front = 2, Rear = 4


Graphs:

- It is a hierarchical data structure.

- The first node is called root node.

- Nodes with no children are called leaf nodes.

- The connection between nodes is called an edge.

Important Terms:

- Node: Element that stores data.

- Edge: Connection between two nodes.

- Internal Node: Node with at least one child.

- External Node (Leaf): Node with no children.

- Root: Topmost node of a tree.

- Height of a Node: Number of edges from node to the deepest leaf.

- Depth of a Node: Number of edges from root to the node.

- Degree of Node: Number of children (edges) a node has.

- Forest: Collection of disjoint (separate) trees.

- Subtree: Remaining tree after removing a root node.

Important Points:

- Root node has no depth (depth=0).

- Leaf node has no height (height=0).

- Trees have high complexity sometimes.

- Trees are non-linear structures.

- Every node has either subtrees or child nodes.

- In every subtree, the top node is its own root.


Functions in a Tree:

- Traversal (visiting all nodes)

- Searching, Insertion, Deletion

Applications of Trees:

1) Binary Search Tree(BST) is used to quickly search if an element exists.

2) Heap is a tree used for heap sort.

3) Tries (modified tree) are used in routers for routing information.

4) Databases use B-trees, T-trees.

5) Compilers use Syntax Trees to validate code syntax.

Types of Tree Traversal:

Preorder Traversal:

- Visit root node first.

- Visit left subtree.

- Visit right subtree.

Inorder Traversal:

- Visit left subtree first.

- Visit root node.

- Visit right subtree.

Postorder Traversal:

- Visit left subtree first.

- Visit right subtree.

- Visit root node.


Traversal Tip:

- Preorder => root first

- Inorder => root middle

- Postorder => root last

Next Topic: Binary Tree

Q1) Binary Tree:

A tree in which every node has at most two children is called a Binary Tree.

Types of Binary Tree:

a) Full Binary Tree:

Every parent node either has exactly two children or no children at all.

b) Perfect Binary Tree:

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.

c) Complete Binary Tree:

A CBT is like a full BT but with major differences:

1) Every level must be completely filled except possibly the last level.

2) All leaf nodes must lean towards the left side.

3) The last leaf node might not have a right sibling.

4) Traverses left to right filling fully line by line.


Important Note:

A Complete Binary Tree must not necessarily be a Full Binary Tree.

Q2) How a Complete Binary Tree is created:

- Insert nodes level by level from top to bottom.

- Fill the left child first, then the right child.

- If a node's left child is empty, insert there.

- If left is filled but right is empty, insert on the right.

- Continue to the next node if both children are filled.

Q3) What are Internal nodes and External nodes:

Internal Node:

A node that has at least one child.

External Node (Leaf):

A node that has no children.

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.

Q2) How to search in a Binary Search Tree:

Search operation algorithm:

if root==NULL

return NULL
if number==root->data

return root->data

if number < root->data

return search(root->left)

if number > root->data

return search(root->right)

Q3) How to insert and delete in BST:

Insertion algorithm:

if root==NULL

create new node

if number < root->data

insert into left subtree

else

insert into right subtree

Deletion algorithm:

if root==NULL

return NULL

if number < root->data

root->left=delete(root->left,number)

else if number > root->data

root->right=delete(root->right,number)

else

if root has no child


delete root

else if root has one child

replace root with child

else

find inorder successor

copy successor's data to root

delete successor

Traversal in BST:

Inorder traversal:

- Traverse left subtree

- Visit root

- Traverse right subtree

Successor:

The minimum value node from the right subtree is called the inorder successor.

You might also like