CPP Data Structures
CPP Data Structures
10, 7}. Write down its output when BST is traversed using prefix and postfix
notation.
Binary tree:
We will follow rule that all left Childs should be less than or equal to root
and right Childs are greater than root node.
Prefix Traversal:
Root Left Right
void prefix(Node* root){
if(root == NULL){
return;
}
else{
cout<<root->getData()<<" ";//First Node then left then right
prefix(root->getLeft());
prefix(root->getRight());
}
}
Question 2: Construct AVL tree using data in above question. Show step by
step construction of AVL tree. Data = {5, 12, 9, 18, 6, 2, 0, 14, 10, 7}
AVL is a self-balancing binary search tree where the difference between
heights of subtree and right subtree will always not greater than 1 for all
nodes.
To construct AVL I calculate balance factor for each node and and it should
not be greater than 1.
Step1: Insert(5)
As five is first element it is added at root.
Step2: Insert(12)
12 is greater than 5 it will go to right side.
Step3: Insert(9)
9 is greater than 5 but less than 12 it will go to left of 12
Step5: Insert(6)
6 is less than 9(root) but greater than left 0f 9(5) so goes to left of 5
Step9: Insert(10)
10 is greater than 9(root) but less than 12 and 14 so goes to left of 12
It is a balanced BST(AVL). Balanced factor for all nodes is not greater
than 1. No need of rotation.
Step10: Insert(7)
7 is less than 9(root) but greater than 5 and 6 so goes to right of 6.
Final AVL:
Question 3: You have a stack instance containing data {5, 10, 2, 9}, and a
queue instance containing data {4, 0, 11, 7, 8}. You have executed following
operations in that order.
Write the state of queue and stack instances on each step:
First of all, I will create initial stack (first in last out) and queue (first in first
out) using given values. In stack I will use a pointer head to point to the last
element of stack. Head will point to the top value of stack and I will use this
to remove or add an element. I queue I will use two pointers first and last.
First will point to the first element in queue and used to remove first element.
Last will point to the last element and used to add new element in queue.
Push: Push function is used to add new element in stack.
Pop: Pop is used to remove last element added from stack.
Enqueue: Enqueue is used to add new element in queue.
Dequeue: Dequeue is used to remove first element from stack.
1) stack.push(15)
After this line of code 15 is pushed in stack and head will start pointing to 15.
No effect on queue
2) queue.enque(12)
After this line of code 12 is added at end of queue and last will start pointing
to 12. No effect on stack.
3) stack.push(100)
After this line of code 100 is pushed in stack and head will start pointing to
100. No effect on queue
4) stack.push(queue.deque())
After this line 4 is dequeued from queue and pushed into the stack. Now head
of stack will point to 4 and first of queue will point to 0(now first element) of
queue.
5) stack.push(queue.deque())
After this line 0 is dequeued from queue and pushed into the stack. Now head
of stack will point to 0 and first of queue will point to 11(now first element)
of queue.
6) queue.enque(20)
After this line of code 20 is added at end of queue and last will start pointing
to 20. No effect on stack.
7) queue.enque(stack.pop())
After this line of code 0 is popped from stack and enqueued into queue as
stack work on first in last out manner. Head of stack will point to 4 and last
of queue will point to 0.