Assignment
COC2060 Data Structures and Algorithms
Name – Ayaan Khan (A2CO30)
Enrolment – GL3134
Faculty number – 19COB049
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:
A stack can be implemented using two queues in the following way.
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.
Pseudo code for push(x) in stack
Push(x):
Step 1 – Push x to queue Q2
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.)
Step 3 – Now swap Q1 and Q2
Pseudo code for pop() in stack
Pop():
Step 1 – Pop an element from queue Q1
illustration
suppose at some time the state of queues Q1 and Q2 are
Queue Q1: 345789
Queue Q2: empty
Now push(6)
Step 1 push 6 to Q2 thus Q1 and Q2 becomes
Queue Q1: 3 4 5 7 8 9
Queue Q2: 6
Step 2 pop from Q1 and push to Q2 all elements
Queue Q1: empty
Queue Q2: 6 3 4 5 7 8 9
Step 3 Now swap Q1 and Q2, thus we have
Queue Q1: 6 3 4 5 7 8 9
Queue Q2: empty
Pop illustration
Initially queue Q1 and Q2 are
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
Queue Q2: empty
Thus, push and pop works properly.
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:
a) Infix Expression: (A+B)*(C-D)^E*F
Infix to Prefix
Scanned Symbol Stack Expression
F ( F
* (* F
E (* FE
^ (*^ FE
( (*^( FE
D (*^( FED
- (*^(- FED
C (*^(- FEDC
) (*^ FEDC-
* (* FEDC-^*
( (*( FEDC-^*
B (*( FEDC-^*B
+ (*(+ FEDC-^*B
A (*(+ FEDC-^*BA
) (* FEDC-^*BA+
) Empty Reversing to get final Prefix as
*+AB*^-CDEF
Infix to Postfix
Scanned Symbol Stack Expression
( ((
A (( A
+ ((+ A
B ((+ AB
) ( AB+
* (* AB+
( (*( AB+
C (*( AB+C
- (*(- AB+C
D (*(- AB+CD
) (* AB+CD-
^ (*^ AB+CD-
E (*^ AB+CD-E
* (* AB+CD-E^*
F (* AB+CD-E^*F
) Empty AB+CD-E^*F*
b) Infix Expression: (A+B)*(C^(D-E)+F)-G
Infix to Prefix
Symbol Stack Expression
G ( G
- (- G
( (-( G
F (-( GF
+ (-(+ GF
( (-(+( GF
E (-(+( GFE
- (-(+(- GFE
D (-(+(- GFED
) (-(+ GFED-
^ (-(+^ GFED-
C (-(+^ GFED-C
) (- GFED-C^+
* (-* GFED-C^+
( (-*( GFED-C^+
B (-*( GFED-C^+B
+ (-*(+ GFED-C^+B
A (-*(+ GFED-C^+BA
) (-* GFED-C^+BA+
) Empty GFED-C^+BA+*-
Reversing to get final prefix as
-*+AB+^C-DEFG
Infix to Postfix
Symbol Stack Expression
( ((
A (( A
+ ((+ A
B ((+ AB
) ( AB+
* (* AB+
( (*( AB+
C (*( AB+C
^ (*(^ AB+C
( (*(^( AB+C
D (*(^( AB+CD
- (*(^(- AB+CD
E (*(^(- AB+CDE
) (*(^ AB+CDE-
+ (*(+ AB+CDE-^
F (*(+ AB+CDE-^F
) (* AB+CDE-^F+
- (- AB+CDE-^F+*
G (- AB+CDE-^F+*G
) Empty AB+CDE-^F+*G-
Ques 3: Go to https://www.random.org/integers/ and generate a list of 12 random integers between 1 and 100. (These
12 numbers will be different for each student if they don’t copy. Attach the snapshot of webpage for generating the
integers as given below)
Solution:
Radix Sort (using counting sort as sub routine)
Initial Array: 91 68 6 34 30 3 63 19 81 59 71 49
Sorting once digit: 30 91 81 71 3 63 34 6 68 19 59 49
Sorting Tens digit: 3 6 19 30 34 49 59 63 68 71 81 91
Sorted Array is: 3 6 19 30 34 49 59 63 68 71 81 91
Quick Sort
Unsorted Array is: 91 68 6 34 30 3 63 19 81 59 71 49
[comparisons: 12] Array: 3 68 6 34 30 91 63 19 81 59 71 49
[comparisons: 15] Array: 3 59 6 34 30 19 49 63 81 91 71 68
[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
Sorted Array is: 3 6 19 30 34 49 59 63 68 71 81 91
Merge Sort
Unsorted Array is: 91 68 6 34 30 3 63 19 81 59 71 49
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
Sorted Array is: 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:
Node() : data(0), left(nullptr), right(nullptr) {}
~Node() {}
Node(int data) : left(nullptr), data(data), right(nullptr) {}
void Insert(Node*&, int);
void IterativeInorder(Node*);
void IterativePostOrder(Node*);
void IterativePreOrder(Node*);
template <typename T>
void ShowStack(std::stack<T>);
};
template<typename T>
void Node::ShowStack(std::stack<T> st) {
while (!st.empty()) {
std::cout << st.top()->data << " ";
st.pop();
std::cout << "\n";
void Node::Insert(Node*& root, int data) {
if (root == nullptr) {
root = new Node(data);
} else if (data <= root->data) {
if (root->left == nullptr) {
root->left = new Node(data);
} else {
Insert(root->left, data);
} else {
if (root->right == nullptr) {
root->right = new Node(data);
} else {
Insert(root->right, data);
void Node::IterativeInorder(Node* root) {
if (root == nullptr) return;
std::stack<Node*> st;
std::queue<int> out;
Node* current = root;
while (current != nullptr || !st.empty()) {
while (current != nullptr) {
st.push(current);
current = current->left;
current = st.top();
st.pop();
out.push(current->data); // std::cout << current->data << " ";
current = current->right;
std::cout << "InOrder Stack: ";
ShowStack(st);
std::cout << "InOrder Traversal: ";
while (!out.empty()) {
std::cout << out.front() << " ";
out.pop();
std::cout << "\n";
void Node::IterativePostOrder(Node* root) {
std::stack<Node*> st;
std::stack<int> out;
st.push(root);
while (!st.empty()) {
Node* curr = st.top();
st.pop();
out.push(curr->data);
if (curr->left) st.push(curr->left);
if (curr->right) st.push(curr->right);
std::cout << "PostOrder Stack: ";
ShowStack(st);
std::cout << "PostOrder Traversal: ";
while (!out.empty()) {
std::cout << out.top() << " ";
out.pop();
}
std::cout << "\n";
void Node::IterativePreOrder(Node* root) {
if (root == nullptr) return;
std::stack<Node*> st;
std::queue<int> out;
st.push(root);
while (!st.empty()) {
Node* current = st.top();
out.push(current->data); // std::cout << current->data << " ";
st.pop();
if (current->right) st.push(current->right);
if (current->left) st.push(current->left);
std::cout << "PreOrder Stack: ";
ShowStack(st);
std::cout << "PreOrder Traversal: ";
while (!out.empty()) {
std::cout << out.front() << " ";
out.pop();
std::cout << "\n";
void ShowBSTState(Node BST, Node* root, int x) {
std::cout << "Status After Inserting " << x << "-> \n";
BST.IterativePreOrder(root);
BST.IterativeInorder(root);
BST.IterativePostOrder(root);
std::cout << "\n";
int main() {
Node BST;
Node* root = nullptr;
BST.Insert(root, 71);
ShowBSTState(BST, root, 71);
BST.Insert(root, 55);
ShowBSTState(BST, root, 55);
BST.Insert(root, 38);
ShowBSTState(BST, root, 38);
BST.Insert(root, 99);
ShowBSTState(BST, root, 99);
BST.Insert(root, 46);
ShowBSTState(BST, root, 46);
BST.Insert(root, 91);
ShowBSTState(BST, root, 91);
BST.Insert(root, 59);
ShowBSTState(BST, root, 59);
BST.Insert(root, 83);
ShowBSTState(BST, root, 83);
BST.Insert(root, 37);
ShowBSTState(BST, root, 37);
BST.Insert(root, 51);
ShowBSTState(BST, root, 51);
return 0;
A separate code file (.cpp) is attached with the submission.