Professional Documents
Culture Documents
Enrolment – GL3134
Ques 1: Show how to implement a queue using 2 stacks. You need to show how insert and delete operations are
performed in queue using two stacks. (No code is needed, just a diagram and pseudo code is sufficient)
Solution:
Let us consider two queues Q1 and Q2. In a queue push or enqueue operation takes place at the REAR end and pop or
dequeue operation takes place at the FRONT end.
Push(x):
Step 2 – Now pop elements from queue Q1 (if any) and push every element one by one to queue Q2 (This operation makes
sures that the inserted element remains at the TOP which is the property of stack.)
Pop():
illustration
Now push(6)
Queue Q1: 3 4 5 7 8 9
Queue Q2: 6
Queue Q2: 6 3 4 5 7 8 9
Queue Q1: 6 3 4 5 7 8 9
Pop illustration
Queue Q1: 6 3 4 5 7 8 9
Queue Q2: empty
Now suppose we call Pop() therefore we will pop element from Q1 therefore Q1 and Q2 becomes
Queue Q1: 3 4 5 7 8 9
Ques 2: Transform each of the following expressions to prefix and postfix. Show the current expression and stack
position after each step.
a) (A+B)*(C-D)^E*F
b) (A+B)*(C^(D-E)+F)-GA)
Solution:
Infix to Prefix
Infix to Postfix
Infix to Prefix
Infix to Postfix
Solution:
Initial Array: 91 68 6 34 30 3 63 19 81 59 71 49
Quick Sort
[comparisons: 4] Array: 3 59 6 34 30 19 49 63 81 68 71 91
[comparisons: 3] Array: 3 59 6 34 30 19 49 63 68 81 71 91
[comparisons: 2] Array: 3 59 6 34 30 19 49 63 68 71 81 91
[comparisons: 8] Array: 3 30 6 19 34 59 49 63 68 71 81 91
[comparisons: 2] Array: 3 30 6 19 34 49 59 63 68 71 81 91
[comparisons: 3] Array: 3 6 30 19 34 49 59 63 68 71 81 91
[comparisons: 2] Array: 3 6 19 30 34 49 59 63 68 71 81 91
After pass 1: 68 91 6 34 3 30 19 63 59 81 49 71
After pass 2: 6 34 68 91 3 19 30 63 49 59 71 81
After pass 3: 3 6 19 30 34 63 68 91 49 59 71 81
After pass 4: 3 6 19 30 34 49 59 63 68 71 81 91
Ques 4: Write the non-recursive algorithms for preorder, inorder and postorder traversal of binary tree. Generate a list
of 10 random integers between 1 and 100 as in question 3. Create a binary search tree (BST) by inserting these integers
into the tree. The order of insertion of integers in the BST must be same as the order of integers in the list. Show the
state of BST after every insertion. Traverse the resulting BST in preorder, inorder and postorder traversal using non-
recursive traversal algorithm. Show the state of stack and traversal after each intermediate step.
Solution:
#include <iostream>
#include <queue>
#include <stack>
class Node {
private:
int data;
Node* left;
Node* right;
public:
void IterativeInorder(Node*);
void IterativePostOrder(Node*);
void IterativePreOrder(Node*);
void ShowStack(std::stack<T>);
};
template<typename T>
while (!st.empty()) {
st.pop();
if (root == nullptr) {
if (root->left == nullptr) {
} else {
Insert(root->left, data);
} else {
if (root->right == nullptr) {
} else {
Insert(root->right, data);
std::stack<Node*> st;
std::queue<int> out;
Node* current = root;
st.push(current);
current = current->left;
current = st.top();
st.pop();
current = current->right;
ShowStack(st);
while (!out.empty()) {
out.pop();
std::stack<Node*> st;
std::stack<int> out;
st.push(root);
while (!st.empty()) {
st.pop();
out.push(curr->data);
if (curr->left) st.push(curr->left);
if (curr->right) st.push(curr->right);
ShowStack(st);
while (!out.empty()) {
out.pop();
}
std::cout << "\n";
std::stack<Node*> st;
std::queue<int> out;
st.push(root);
while (!st.empty()) {
st.pop();
if (current->right) st.push(current->right);
if (current->left) st.push(current->left);
ShowStack(st);
while (!out.empty()) {
out.pop();
std::cout << "Status After Inserting " << x << "-> \n";
BST.IterativePreOrder(root);
BST.IterativeInorder(root);
BST.IterativePostOrder(root);
int main() {
Node BST;
BST.Insert(root, 71);
BST.Insert(root, 55);
BST.Insert(root, 38);
ShowBSTState(BST, root, 38);
BST.Insert(root, 99);
BST.Insert(root, 46);
BST.Insert(root, 91);
BST.Insert(root, 59);
BST.Insert(root, 83);
BST.Insert(root, 37);
BST.Insert(root, 51);
return 0;