Document
Document
Sequential Search: It's a simple search algorithm that checks each element in a list one by one until it
finds the target element or reaches the end of the list.
b. Interpolation Search: This search algorithm is similar to binary search, but it uses a different approach
to guess the position of the target element in a sorted list. It makes a more informed guess based on the
values of the first and last elements.
c. Merge Sort: It's a sorting algorithm that follows the divide-and-conquer approach. It divides the input
list into smaller sublists, sorts them individually, and then merges them back together to get a sorted
output.
d. Quick Sort: Another sorting algorithm that also uses the divide-and-conquer approach. It selects a
pivot element from the list and partitions the other elements into two sublists, one with elements
smaller than the pivot and the other with elements greater than the pivot. It then recursively sorts the
sublists.
Q2 a. A Tree: In computer science, a tree is a data structure that consists of nodes connected by edges.
It's called a tree because it resembles a real-life tree, with a root at the top and branches extending
downwards. Each node can have child nodes, forming a hierarchical structure.
b. Binary Tree: A binary tree is a specific type of tree where each node has at most two child nodes,
commonly referred to as the left child and the right child. It's a simple yet powerful data structure used
in many algorithms and applications.
c. Tower of Hanoi: The Tower of Hanoi is a classic puzzle that involves three pegs and a number of disks
of different sizes. The goal is to move all the disks from one peg to another, following a set of rules. It's a
fun and challenging problem that often requires recursive thinking to solve efficiently.
d. Hashing: Hashing is a technique used to map data to a fixed-size array, known as a hash table or hash
map. It involves applying a hash function to the input data, which generates a unique hash value. This
value is used as an index to store or retrieve the data in the hash table, providing fast access to the
stored information.
c. Doubly Linked: A doubly linked list is a type of linked list where each node contains a reference to the
previous node as well as the next node. This bidirectional connection allows for traversal in both
directions, forward and backward, making it easier to navigate and modify the list compared to a singly
linked list.
d. To convert the given infix expression into its equivalent postfix expression, we can use the concept of
the stack and the rules of operator precedence. Here's the step-by-step conversion:
3. If the scanned element is an operand (letter or number), add it to the output string.
a. If the stack is empty or the top of the stack is an opening parenthesis, push the operator onto the
stack.
b. If the precedence of the scanned operator is higher than the top of the stack, push it onto the stack.
c. If the precedence of the scanned operator is lower than or equal to the top of the stack, pop
operators from the stack and add them to the output string until a lower precedence operator is
encountered or the stack becomes empty. Then push the scanned operator onto the stack.
6. If the scanned element is a closing parenthesis, pop operators from the stack and add them to the
output string until an opening parenthesis is encountered. Discard the opening and closing parentheses.
8. Pop any remaining operators from the stack and add them to the output string.
Using this process, the given infix expression "A*(B+D)/E-F*(G+H/K)" would be converted to
"ABD+*E/FGHK/+*-".
Sure, here's a C++ implementation of the pop or remove operation algorithm for a stack:
```cpp
#include <iostream>
using namespace std;
class Stack {
int top;
int arr[MAX_SIZE];
public:
Stack() {
top = -1;
bool isEmpty() {
bool isFull() {
if (isFull()) {
return;
arr[++top] = item;
cout << "Pushed " << item << " into the stack." << endl;
}
void pop() {
if (isEmpty()) {
return;
cout << "Popped " << item << " from the stack." << endl;
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
stack.pop();
stack.pop();
return 0;
```
- Stack: On the other hand, a stack is a linear data structure that follows the Last-In-First-Out (LIFO)
principle. This means that the element inserted last will be the first one to be removed. Think of it like a
stack of plates, where you can only take the topmost plate.
- Infix Notation: This is the most common way of writing expressions, where operators are written
between the operands. For example, "2 + 3 * 4" is an infix expression.
- Prefix Notation (also known as Polish Notation): In prefix notation, the operator is written before the
operands. For example, "+ 2 * 3 4" is a prefix expression.
- Postfix Notation (also known as Reverse Polish Notation): In postfix notation, the