Computer Science Quiz 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Using the parenthesis method, what is the posftix expression for the infix expression (3+5)*2+(2/3^2)

(3+5)*2+(2/3^2) Step 1: If not done, parenthesize the entirely infix expression according to the order of priority you want. (((3+5)*2)+(2/(3^2))) Step 2: Move each operator to its corresponding right parenthesis (((3+5)*2)+(2/(3^2))) ==== > (((3 5)+2) *(2(3 2)^)/)+ Step 3: Remove all parentheses (((3+5)*2)+(2/(3^2))) ==== > 3 5 + 2 *2 3 2 ^ / +

Given a queue containing the values 4 ->3 -> 6 ->8 ->9 What are the values of head and tail? Given this queue, suppose we call dequeue twice and enqueue once. What would be the new values of head and tail?

1. Head = 4, Tail = 9 2. Head = 6, Tail = The value passed to enqueue

Assume you have a definition for the class Stack. What would be the output when the following code is executed? n = 50 s = Stack() out=[] while (n > 0): s.push(n % 2) n //= 2 while not s.isEmpty(): out.append(str(s.pop())) print(''.join(out))

110010

Consider the following list of integers: [4, 15, 12, 9, 1, 2, 5, 7, 23, 150]. Draw the min binary heap resulting from inserting the integers in the list in that order. What is the array representation of the heap after 2 call to deleteMin

Building Min-Heap. Add element to the complete tree, if it does not satisfy the min-order property (parent less than children), swap Heap array representation = [1, 4, 2, 7, 9, 12, 5, 15, 23, 150] Performing 2 calls to deleteMin. Removed values from the heap: 1 and 2 Heap array representation after 2 deleteMin calls: [4, 7, 5, 15, 9, 12, 150, 23]

For the given scenario, what is the best linear data structure to use: "The size of a file is unknown. The entries need to be entered as they come in. Entries must be deleted when they are no longer needed. It is important that the selected structure has flexible memory management"

Linked List

Build the expression tree for the arithmetic infix expression (3+5)*2+(2/3^2). Get the postorder traversal of the tree. Is it the same as your result from the previous question?

Postorder traversal of the tree: 3 5 + 2 *2 3 2 ^ / + It should be the same result, as post order traversal of an expression tree yields the postfix notation of the expression

Consider the following list of integers: [4, 15, 12, 9, 1, 2, 5, 7, 23, 150]. Draw the binary search tree resulting from inserting the integers in the list in that order. What is the height of the tree? Number of total levels Which nodes are in the subtree rooted at node 15? Nodes at level 4 What are the leaf nodes of the tree? Depth of the root node Depth of node 150 Children of node 1 Parent of node 9 Path from root to node 5 Order in which nodes are visited during a BFS, Preorder, Inorder and Postorder traversals

What is the height of the tree?5 Number of total levels:6 Which nodes are in the subtree rooted at node 15?: 15, 12, 23, 9, 150, 5, 7 Nodes at level 4:9, 150 What are the leaf nodes of the tree?:2, 7, 150 Depth of the root node:0 Depth of node 150:3 Children of node 1:2 Parent of node 9:12 Path from root to node 5: 4-15-12-9-5 Order in which nodes are visited during a BFS, Preorder, Inorder and Postorder traversals:BFS: 4, 1, 15, 2, 12, 23, 9, 150, 5, 7Preorder: 4, 1, 2, 15, 12, 9, 5, 7, 23, 150Inorder: 1, 2, 4, 5, 7, 9, 12, 15, 23, 150Postorder: 2, 1, 7, 5, 9, 12, 150, 23, 15, 4

In the LinkedList class write the method switchToFront () that moves the last element to the front of the Linked List. For example, if the Linked List is 1->2->3->4->5, then a call to this method should change the list to 5->1->2->3->4. Linked list has a reference to head and tail nodes >>> x=LinkedList() >>> x.add(4) >>> x.add(3) >>> x.add(2) >>> x.add(1) >>> x.append(5) >>> x Head: Node(1) Tail: Node(5) List: 1 -> 2 -> 3 -> 4 -> 5 >>> x.switchToFront() >>> x Head: Node(5) Tail: Node(4) List: 5 -> 1 -> 2 -> 3 -> 4

def switchToFront(self): if len(self)>1: # Save reference to old tail and starting pointcurrent = self.head moved_node = self.tail # Get previous node to current tail while current.next.next is not None: current = current.next # Update links current.next = None self.tail = current moved_node.next = self.head self.head = moved_node

Based on the following code, which of the following expressions has the None value ? class Node: def __init__(self, value): self.value = value self.next = None >>> p = Node(12) >>> q = Node(5) >>> q.next = p

q.next.next


संबंधित स्टडी सेट्स

Macroeconomics: Ch. 10 Trade Balance

View Set

Ch 3: Exploratory, Descriptive, and Causal Research Designs

View Set

Chapter 36 Organ Donation and Transplantation

View Set

Ch. 8: Skeletal System: Axial and Appendicular Skeleton

View Set

Survival and responses - 3.6.1.1

View Set

Harry Potter and the Sorcerer's Stone

View Set

CISSP Cram Test Questions: Domain 7 - Security Engineering

View Set