Question

(ii) [6 marks] Assume that we have an empty stack S and an empty queue Q. Given a series of stack operations on S as below: P

0 0
Add a comment Improve this question Transcribed image text
Answer #1

\blacklozenge We know that in a Stack when we insert or push elements it follows last-in-first-out ( LIFO ) rule, that is, last inserted element will be the first to be deleted or popped. This is because the Stack pointer, top of stack ( TOS ), always points to the new element being inserted.

So, after push(S,10), push(S,7) and push(S,23) where S is the stack and the other parameter is the element to be inserted, our Stack S would look like

23
7
10

And TOS will point to 23.

then first pop(S) will give 23. And stack will become, for us,

7
10

And TOS points to 7. Then push(S,9) is performed. Stack becomes

9
7
10

And TOS points to 9.

Then Second pop(S) will give 9. TOS will then point to 7 again.

And third pop(S) will give 7. The stack at the end will become

10

\blacklozenge We know that in a queue when we insert or enqueue elements it follows first-in-first-out ( FIFO ) rule, that is, first inserted element will be the first to be deleted or dequeued. This is because the queue maintains two pointers,

front pointer (f) : points to the first element to be deleted and,

rear pointer(r) : points where the next element will be enqueued,

So, after enqueue(Q,10) , enqueue(Q,20) and enqueue(Q,33) our queue Q will look like :

10 20 33

f points to 10, and r points to 33.

first dequeue(Q) will give 10. And queue will be

20 33

where f will now point to 20 and r will point to 33.

After enqueue(Q,55) queue will become

20 33 55

where f will point to 20 and r will now point to 55.

Then second dequeue(Q) will give 20. And queue will be

33 55

where f will now point to 33 and r will point to 55.

Then third dequeue(Q) will give 33. And queue will be

55

where f will also point to 55 along with r .

\blacklozenge BInary Search Tree is based on the fact that the left child of a node is always less than the node and the right child is always greater than the node, that is, the left subtree of the root will consist of nodes lesser than the root and right subtree will consist of nodes greater than the root.

25 20 50 15 30 60 18

Here, we can see that first element we inserted was 25, which becomes our root node as the tree was initially empty.

Second we insert 20, which goes to the left of root as it's less than 25.

Then we add 50 which is greater than root 25 and thus, it goes to the right of root node.

Then we add 15, which being less than 25 goes to left subtree, and then we check if 15 is less than or greater than 20, as it's less than 20 it becomes left child of 20.

Then we are adding 60 which is greater than root so, it goes to the right subtree. Then we compare it with 50 and as 60 is greater it becomes the right child of 50.

Then we add 30, which is greater than 25, it goes to right subtree, and then we compare it with 50 30 being less than 50, becomes the left child of 50.

At last we add 18 which being less than 25 goes to the left subtree and then we compare it with 20, which is greater than 18 so 18 should have been in the left of 20, but as 15 is already there so it is again compared with 15 and 18 is greater than 15 so, 18 becomes the right child of 15.

And thus. BST is completed.

Add a comment
Know the answer?
Add Answer to:
(ii) [6 marks] Assume that we have an empty stack S and an empty queue Q....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose that we start with an empty stack and execute the operations: push(5) push(12) popo) push(3)...

    Suppose that we start with an empty stack and execute the operations: push(5) push(12) popo) push(3) push(7) popo) What values are returned by the two pop() operations above, in order? First pop(): Second pop(): Suppose that we start with an empty stack and execute the operations: enqueue(5) enqueue(12) dequeue enqueue(3) enqueue(7) dequeuel) What values are returned by the two pop() operations above, in order? First dequeue(): Second dequeue():

  • A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2...

    A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2 push operations, the number of elements in the stack would be: B. Starting with an initially empty queue, after 5 enqueue operations, 4 dequeue operations, and 6 enqueue operations, the number of elements in the queue would be:

  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

    Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front = 0, back = 0; void enqueue(int x) { data[back] = x; back = (back + 1) % 6; } void dequeue() { front = (front + 1) % 6; } and we perform the following series of queue operations: enqueue(1); dequeue(); enqueue(2); dequeue(); enqueue(7); enqueue(3); enqueue(5); dequeue(); dequeue(); enqueue(4); enqueue(6); Write the state of the queue array after each operation, and at the end,...

  • Conceptual Questions Suppose you are given a (strange) computer that can only perform the followi...

    Python coding conceptual questions - Conceptual Questions Suppose you are given a (strange) computer that can only perform the following instructions (in addition to if and while): S-create_stack() create stack makes a new stack s iS.pop() removes the top item from stack s and places it in variable i .S.push (i) makes item i the top item in stack s Solve the following problems and justify your answers: 1. (10 pts) Show how you can use these operations to implement...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT