2.
a. #include <iostream>
#include <cctype>
#include <string>
using namespace std;
class Queue {
int *arr;
int front,rear, maxSize;
public:
Queue(int size) {
maxSize = size;
arr = new int[size];
front = 0;rear = 0;}
bool isEmpty() {
return front == rear;}
bool isFull() {
return rear == maxSize;}
bool enqueue(int val) {
if (isFull()) {
cout << "Queue is Overflowing\n";
return false;}
arr[rear] = val;
rear++;
return true;}
bool dequeue() {
if (isEmpty()) {
cout << "empty queue.\n";
return false;}
front++;
return true;}
int frontValue() {return arr[front];}
void show() {
if (isEmpty()) {
cout << "Queue is empty\n";}
for (int i = front; i < rear; i++) {
cout << arr[i] << endl;}}
};
class Stack {
int *arr;
int top,maxSize;
public:
Stack(int size) {
maxSize = size;
arr = new int[size];
top = 0;}
bool isEmpty() {
return top == 0; }
bool isFull() {
return top == maxSize;}
bool push(int val) {
if (isFull()) {
cout << "Stack is Overflowing.\n";
return false;}
arr[top] = val;
top++;
return true;}
bool pop() {
if (isEmpty()) {
cout << "Stack is empty\n";
return false;}
top--;
return true;}
int peek() {
return arr[top-1];}
void show() {
if (isEmpty()) {
cout << "Stack is empty\n";}
for (int i=top-1; i>=0; i--) {
cout << arr[i] << endl;}}
};
int prece(char x) {
if (x == '+' || x == '-') return 1;
if (x == '*' || x == '/') return 2;
return 0;}
string infixToPostfix(const string& infix) {
Stack stack(30);
Queue queue(30);
for (char y : infix) {
if (isalnum(y)) {
queue.enqueue(y);}
else if (y == '(') {
stack.push(y);}
else if (y == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
queue.enqueue(stack.peek());
stack.pop();}
stack.pop();}
else if (y == '+' || y == '-' || y == '*' || y == '/') {
while (!stack.isEmpty() && prece(stack.peek()) >= prece(y)) {
queue.enqueue(stack.peek());
stack.pop();}
stack.push(y);}}
while (!stack.isEmpty()) {
queue.enqueue(stack.peek());
stack.pop();}
string r = "";
while (!queue.isEmpty()) {
r+= queue.frontValue();
r+= " ";
queue.dequeue();}
return r;}
int main() {
string infix;
cout << "Enter the infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;}
b. #include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
class BST {
public:
Node* root;
BST() : root(nullptr) {}
void insert(int val) {
Node* newNode = new Node(val);
if (root == nullptr) {
root = newNode;
return;}
Node* c = root;
Node* parent = nullptr;
while (c != nullptr) {
parent = c;
if (val < c->data) {
c = c->left;
} else {
c = c->right;}}
if (val < parent->data) {
parent->left = newNode;
} else {
parent->right = newNode;}}
void dltNode(int val) {
Node* c = root;
Node* parent = nullptr;
while (c!= nullptr && c->data != val) {
parent = c;
if (val < c->data) {
c = c->left;
} else {
c = c->right;}}
if (c == nullptr) {
cout << "Value is not in tree." << endl;
return;}
if (c->left == nullptr && c->right == nullptr) {
if (c != root) {
if (parent->left == c) {
parent->left = nullptr;
} else {
parent->right = nullptr;
}} else {
root = nullptr;}
delete c;}
else if (c->left == nullptr || c->right == nullptr) {
Node* child = (c->left) ? c->left : c->right;
if (c != root) {
if (c == parent->left) {
parent->left = child;
} else {
parent->right = child;}
} else {
root = child;}
delete c;}
else {
Node* s = getMinKey(c->right);
int val = s->data;
dltNode(s->data);
c->data = val;}}
Node* getMinKey(Node* c) {
while (c->left != nullptr) {
c = c->left;}
return c;}
void inorderTraversal(Node* node) {
if (node == nullptr) return;
inorderTraversal(node->left);
cout << node->data << " ";
inorderTraversal(node->right);}
int height(Node* node) {
if (node == nullptr) return 0;
int leftHeight = height(node->left);
int rightHeight = height(node->right);
return max(leftHeight, rightHeight) + 1;}};
int main() {
BST tree;
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);
cout << "Inorder Traversal: ";
tree.inorderTraversal(tree.root);
cout << endl;
cout << "Height of the BST: " << tree.height(tree.root) << endl;
tree.dltNode(20);
cout << "Inorder Traversal after deleting 20: ";
tree.inorderTraversal(tree.root);
cout << endl;
cout << "Height of the BST after deletion: " << tree.height(tree.root) <<
endl;
return 0;}
3.
a.
#include <iostream>
using namespace std;
struct Node {
char data;
Node* left;
Node* right;
Node(char val) : data(val), left(nullptr), right(nullptr) {}
};
int find(char arr[], int start, int end, char val) {
for (int i = start; i <= end; i++) {
if (arr[i] == val) {
return i;}}
return -1;}
Node* conTree(char pre[], char post[], int& preIndex, int l, int h, int size) {
if (preIndex >= size || l > h) return nullptr;
Node* root = new Node(pre[preIndex]);
preIndex++;
if (l == h || preIndex >= size) return root;
int postIndex = find(post, l, h, pre[preIndex]);
if (postIndex != -1 && postIndex <= h) {
root->left = conTree(pre, post, preIndex, l, postIndex, size);
root->right = conTree(pre, post, preIndex, postIndex + 1, h - 1, size);}
return root;}
void inoTrav(Node* node) {
if (node == nullptr) return;
inoTrav(node->left);
cout << node->data << " ";
inoTrav(node->right);}
int main() {
int size;
cout << "the number of nodes you want: ";
cin >> size;
char pre[size], post[size];
cout << " please enter the preorder sequence: ";
for (int i = 0; i < size; i++) {
cin >> pre[i];}
cout << "Enter the postorder sequence: ";
for (int i = 0; i < size; i++) {
cin >> post[i];}
int preIndex = 0;
Node* root = conTree(pre, post, preIndex, 0, size - 1, size);
cout << "the Inorder Traversal is : ";
inoTrav(root);
cout << endl;
return 0;}
b.