1050_Lab8_Worksheet

docx

School

Langara College *

*We aren’t endorsed by this school

Course

1050

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

5

Uploaded by CaptainGalaxyWasp15

Report
CPSC 1050 – Lab 8 – Worksheet (32 marks) Abstract Data Types For each of the following questions write your complete work. The first 3 questions are from "Lab 9 Abstract Data Types" (Lab9_Manual.pdf). You may write your answers in a document, save as PDF, and submit on D2L. 1. (4 marks) Lab 9 / Exercise 1. Ans- 1,2- Alice 3- while(more data) read value push(stack,value) while(NOT empty(stack)) pop(stack,value) write value ========================================================================================== 2. (4 marks) Lab 9 / Exercise 4. Ans- 1,2,3,4- The structure of the tree looks like linear. The each and every node of the tree being the child one after another. 5- It will remain sorted until and unless we change it. 6- It is a stick. 7- 1
The order is: Daisy Echinacea Fern Aster Bluebell Coreopsis Gladiolus 8- Yes many orders are possible. Because it is inserted alphabetically ========================================================================================== 3. (4 marks) Lab 9 / Exercise 5. Ans- 1,2,3- 4- The name inserts after the old duplicate name. 5- making new name of child of another node / making new name in its own node 6- no the app gets confused. It turns to red. ========================================================================================== 2
4. (4 marks) What is the output of the following stack algorithm? Push(myStack, 2) Push(myStack, 7) Push(myStack, 8) Push(myStack, 5) Push(myStack, 9) Push(myStack, 1) Pop(myStack, item) Pop(myStack, item) Pop(myStack, item) Push(myStack, item) Push(myStack, item) WHILE(NOT IsEmtpy(myStack)) Pop(myStack, item) Write item, ", " 2 is the output of the Stack and Queues of the Code. ======================================================================== 5. (4 marks) What is the output of the following queue algorithm? Enque(myQueue, 2) Enque(myQueue, 7) Enque(myQueue, 8) Enque(myQueue, 5) Enque(myQueue, 9) Enque(myQueue, 1) Deque(myQueue, item) Deque(myQueue, item) Deque(myQueue, item) Enque(myQueue, item) WHILE(NOT IsEmtpy(myQueue)) Deque(myQueue, item) Write item, ", " 5918 is the output ======================================================================== 6. (4 marks) Write an algorithm that sets variable bottom equal to the last element in a stack: a. leaving the stack empty. ANS- WHILE (NOT IsEmpty(myStack)) Pop(myStack, bottom) 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
b. leaving the stack unchanged. [Hint: Store the stack elements in another stack as you pop them and then push them back to the original stack.] we will have to copy all the elements to an auxstack (temp stack) WHILE (NOT IsEmpty(myStack)) POP (myStack,bottom) PUSH (auxStack,bottom) WHILE (NOT IsEmpty(auxStack)) POP (auxStack, temp) PUSH(myStack, temp) ========================================================================================== 7. (4 marks) Write an algorithm that sets variable last equal to the last element in a queue: a. leaving the queue empty. We need to pop elements off the queue until it is empty. while(NOT IsEmpty(myQueue)) DEQUEUE (myStack, last) b. leaving the queue unchanged. [Hint: Store the queue elements into another queue as you dequeue them and then store them back to the original queue.] While(not isEmpty(originalQueue)) Dequeue(originalQueue, last) Enqueue(tempQueue,last) While(Not isEmpty(tempQueue)) Dequeue(tempQueue,item) Enqueue(originalQueue,item) ========================================================================================== 8. (4 marks) Given the following set of numbers: 88 93 24 18 24 99 9 66 27 18 35 85 a. Draw the Binary Search Tree that results from inserting the numbers in the given order. 4
b. How many comparisons are required to find the value 27 in this tree? We need 3 comparisons to find 27 in this tree 5