York University AK/ITEC 262 3. INTRODUCTION TO DATA STRUCTURES Final Sample II Examiner: S. Chen Duration: Three hours This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g. for computing and/or communicating) is NOT permitted. Do not unstaple this test book any detached sheets will be discarded. Answer all questions in the space provided. No additional sheets are permitted. Work independently. The value of each part of each question is indicated. The total value of all questions is 1. Write your name and student number in the space below. Do the same on the top of each sheet of this exam where indicated. Surname: Given Names: Student Number: Q1. Q6. Totals Q2. Q7. Q3. Q8. Q4. Q9. Q5. Q1. Page 1 of 15
Question 1 (1 marks) Complexity Analysis/Estimation: Assume that an array has n random values. What is the time complexity of the following method that determines if the values in the array are sorted from smallest to largest? Note: you must show all of your work to receive full credit. public boolean issorted (int[] intarray) { for (int i = ; i < intarray.length-1; i++) { if (intarray[i] > intarray[i+1]) return false; } } return true; Page 2 of 15
Question 2 (1 marks) Linked Data Structures: Answer both parts below. Part A (5 marks): The in-order traversal of a binary tree processes the nodes in the order ABCD. The post-order traversal of the same binary tree processes the nodes in the order ACDB. Draw the tree below. Part B (5 marks): Inserting one element at a time, what is the best case time complexity to add n elements to an initially empty linked list so that the stored values will be sorted from smallest to largest? Explain. A BST has n nodes. What is the time complexity of an in-order traversal? Page 3 of 15
Question 3 (1 marks) Recursion: Write a recursive method that will print in order the first i nodes of a binary tree. root D B E A C Note: root is an instance of the class BinNode: public class BinNode { public char value; public BinNode left; public BinNode right; } Thus, the following statements would lead to the underlined output: Example 1: Example 2: printinodes (root, 4, ); A B C D printinodes (root, 12, ); A B C D E Please write your method on the following page. You may use this page for rough work, but anything on this page will not be graded. Page 4 of 15
public static int printinodes (BinNode node, int i, int count) { } Page 5 of 15
Question 4 (1 marks) Heaps: Answer both parts below. Part A (5 marks): Heapify the elements of the following array into a max-heap. original array 1 5 2 6 8 7 9 3 1 heapified array Part B (5 marks): Remove the largest element from the following array which represents a max-heap. original array 1 8 9 7 1 2 3 5 6 final array Page 6 of 15
First name: Student #: Question 5 (1 marks) Grammars: Answer both parts below. Part A (5 marks): A grammar G = (T,N,S,P) has T = {x,y,z}, N = {Z,A}, S = Z, and the following productions P: Z A A x y A A A z A A x A x A y A z Show a parse tree for the sentence x y z z x. Part B (5 marks): Write a grammar for a language whose sentences start with an even and non-zero number of x s, end with an odd number of z s, and have a number of y s in between equal to the total number of x s and z s in the sentence. For example, xxyyyyyzzz is a valid sentence. Page 7 of 15
Question 6 (1 marks) Programming with Data Structures: The API for the Queue class is given below. Constructor Summary Queue () Construct an empty Queue. Method Summary Object dequeue () Remove and return the first element in the Queue. Return null if the Queue is empty. void enqueue (Object item) Add the given item to the end of the Queue. The API for the Node class is given below. Constructor Summary Node () Construct a node that can be used to make a binary tree. The child links will be null and the value will be -1. Node (int value) Construct a Node with the value set to item. Method Summary Node getleft () Returns the left child of this Node. Node getright () Returns the right child of this Node. int getvalue () Returns the value of this Node. Page 8 of 15
Using the above API s for Queues and Nodes, write a method that will perform a level order traversal of a binary tree. Example: binary tree: root 1 2 3 4 5 6 7 8 9 call: printinlevelorder(root); output: 1 2 3 4 5 6 7 8 9 Please write your method on the following page. You may use the above pages for rough work, but anything on these pages will not be graded. Page 9 of 15
public static void printinlevelorder (Node root) { } Page 1 of 15
First name: Student #: Question 7 (1 marks) Graph Algorithms: Shown below is an undirected weighted graph G. a 5 b 1 c 8 2 9 d 8 e 3 4 f 6 5 g 8 6 3 h Using Dijkstra s algorithm on the above graph to determine the Shortest Paths to node F. Fill in the following table that provides the distance estimates after each node is processed. Nodes A B C D E F G H Current Estimate Final Values Page 11 of 15
First name: Student #: Question 8 (1 marks) Graph Algorithms II: Shown below is a weighted graph G. a 7 b 1 c 8 2 3 d 8 e 5 4 f 6 5 g 2 9 7 h Use Prim s algorithm on the above graph to determine the Minimum-Cost Spanning Tree of directed edges with node E as the root of the MCST (e.g. node E is the source of a cable broadcast, and the MCST shows how the signal will be transmitted to every station in the network). Show the graph of your MCST below using an adjacency-list representation, and draw the directed MCST on the graph above. Ties may be broken randomly. A B C D E F G H Page 12 of 15
Question 9 (1 marks) Game Trees: Answer both parts below. Part of the game tree for a fictitious game is shown below. The value returned by the evaluation function applied to each leaf of the tree is indicated inside the leaf. As indicated, the machine has a choice among three moves (a, b, c). The machine is attempting to maximize, and its opponent is attempting to minimize. max a b c min max 3 2 4 6 3 5 4 2 6 min 3 1 1 7 6 3 Part A (4 marks): Using the min-max algorithm, which move should be made by the machine. a, b, or c? What is the end value of this move? Part B (6 marks): Circle the parts of the tree that would be pruned if alpha-beta search is used. Assume the search is done depth-first, left-to-right. Page 13 of 15
Question 1 (1 marks) Hashing: Answer both parts below. Assume a hash table with size H = 11 with the following hash function: h: index = (key mod H) Part A (5 marks): Show the contents of the hash table after the following operations have been performed. Indicate next to each operation the series of cells examined. Assume that linear probing is used to resolve collisions and that the hash table is initially as shown. cells insert 6 6 7 insert 5 insert 15 search 28 delete 4 insert 6 1 2 3 4 5 6 7 8 9 1 114 116 6 Page 14 of 15
Part B (5 marks): Show the contents of the hash table after the following operations have been performed. Indicate next to each operation the series of cells examined. Assume that quadratic probing is used to resolve collisions and that the hash table is initially as shown. insert 3 insert 14 cells 1 2 3 4 114 search 28 delete 3 search 3 5 6 7 8 9 116 1 Page 15 of 15