Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

Similar documents
CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 2123: Collections: Priority Queues. Jeremy Morris

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Programming II (CS300)

CSE 373 Spring Midterm. Friday April 21st

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CS171 Final Practice Exam

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CS171 Final Practice Exam

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

TREES. Trees - Introduction

Trees. (Trees) Data Structures and Programming Spring / 28

Name CPTR246 Spring '17 (100 total points) Exam 3

CPSC 331 Term Test #2 March 26, 2007

Programming II (CS300)

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Binary Trees, Binary Search Trees

Sorting. Task Description. Selection Sort. Should we worry about speed?

Binary Trees

( ) 1 B. 1. Suppose f x

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

Chapter 20: Binary Trees

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

ECE242 Data Structures and Algorithms Fall 2008

Data Structures Question Bank Multiple Choice

MLR Institute of Technology

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Computer Science 136 Exam 2

) $ f ( n) " %( g( n)

Final Examination CSE 100 UCSD (Practice)

About this exam review

COS 226 Midterm Exam, Spring 2009

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

COS 226 Algorithms and Data Structures Fall Midterm

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

: Advanced Programming Final Exam Summer 2008 June 27, 2008

Prelim 2, CS2110. SOLUTION

Friday Four Square! 4:15PM, Outside Gates

Section 1: True / False (2 points each, 30 pts total)

Te Whare Wananga o te Upoko o te Ika a Maui EXAMINATIONS 2003 END-YEAR COMP 103. Data Structures and Algorithms

Analysis of Algorithms

Visit ::: Original Website For Placement Papers. ::: Data Structure

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Data Structures and Algorithms Winter term 2016

Programming II (CS300)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

CS/ENGRD2110: Final Exam

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Total Score /1 /20 /41 /15 /23 Grader

INF2220: algorithms and data structures Series 1

CS 171: Introduction to Computer Science II. Binary Search Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Summer Final Exam Review Session August 5, 2009

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

ECE250: Algorithms and Data Structures Midterm Review

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

n 2 ( ) ( ) + n is in Θ n logn

Data Structure Advanced

COMP 103 Introduction to Data Structures and Algorithms

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Cpt S 122 Data Structures. Data Structures Trees

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Lecture 26. Introduction to Trees. Trees

Data Structures - Binary Trees 1

Computer Science 302 Spring 2007 Practice Final Examination: Part I

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

COMP 250 Midterm #2 March 11 th 2013

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

CS 216 Exam 1 Fall SOLUTION

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Trees. Truong Tuan Anh CSE-HCMUT

CS302 - Data Structures using C++

Transcription:

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU Last Name: First Name: Please put student id on last page Instructions: The time for this test is 3 hrs. No references or calculators are allowed. Write all answers in this booklet and do not hand in any other work. The mark total for this exam is 100. Only one answer per question is allowed.

#1 (A) [10 marks ] Consider four sorts: SelectionSort, InsertionSort, MergeSort, and QuickSort(standard version with left element as pivot). For each of the following situations, pick ONE of these four sorts that you think would be best for the situation, assuming that you want the fastest sort possible that will work. Then say why it is best in 20 words or less, and give the exact number of comparisons in the worst case where required. Do not simply say that it is the best because it is the fastest. Indicate why it is the fastest. Assume that you have as much memory as necessary to use any of the sorts, except in those situations where memory availability is specifically mentioned. a)start with a Vector of Strings that has 1,000,000 elements and is already sorted in ascending order. Add 5 more Strings to the end of the Vector. Now sort the elements in ascending order. Which Sort? Why? Exact Number of comparisons: b)start with one Vector of Strings whose 200,000 elements are already sorted in ascending order and a second Vector of Strings whose 200,000 elements are also sorted in ascending order. Start with a third Vector that is empty. Add all elements from the first vector to the third Vector in order. Then append all the elements from the second Vector to the third vector in order. Now sort the elements of the third vector in ascending order. Which Sort? Why? c)start with one Vector of Strings whose 10,000 elements are already sorted in descending order. Now sort the elements of the vector in ascending order. Which Sort? Why? d)start with one Vector of Persons whose N elements, N > 100,000 are in random order. Assume that you have enough memory to store this vector, plus enough memory for as many stack frames as any of the sorts would require. However, assume that you do not have enough memory to store more than 1,000 extra Person elements. Now sort the elements of the vector in ascending order. Which Sort? Why? Number of comparisons in O-notation: #1 (B) [10 marks ] Consider the four search scenarios listed below (a, b, c, and d). For each of the scenarios, circle the word binary if it is possible to do a binary search for that scenario or circle sequential if a binary search is not possible. Then, use a binary search if possible and a sequential search if a binary one is not possible. Count the number of element comparisons that will be performed during the search and write this number in the space provided. a)searching for a String in an unsorted Vector of 512 Strings and not finding it. b)searching for a String in a sorted Vector of 256 Strings and finding it at the last location searched.

c)searching for a String in an unsorted DoublyLinkedList of 1,024 Strings and not finding it. d)searching for a String located at position 64 of a sorted SinglyLinkedList of 1,024 Strings. e)searching for an Integer in a sorted SinglyLinkedList of 2,048 Integers and not finding it. Assume the List contains the Integers: 1, 3, 5, and assume you are looking for the Integer 512. #2 (A) [8 marks ] Consider the following program. What is the output? Ignore any syntax errors that may be in the program. import structure.*; public class StacksAndQueues { public static void main(string args[]) { /* Program statements go here. */ String[] mystrings = { "fred", Wilma, "barney", "pebbles"; Stack stack1; Stack stack2; Queue myqueue; int index; Object element; stack1 = new StackList(); stack2 = new StackList(); myqueue = new QueueVector(); for (index = 0; index < mystrings.length; index++) stack1.add(mystrings[index]); System.out.println(stack1); for (index = 0; index < mystrings.length / 4; index++) { element = stack1.remove(); myqueue.add(element); stack2.add(element); System.out.println("Stack 1: " + stack1); while (!stack1.isempty()) { element = stack1.remove(); myqueue.add(element); element = myqueue.remove(); stack2.push(element); System.out.println("Stack 2: " + stack2); System.out.println("Queue: " + myqueue); while (!myqueue.isempty()) { element = myqueue.remove(); stack2.add(element); System.out.println("Stack 2: " + stack2); OUTPUT:

#2 (B) [12 marks] Consider the following problems; clearly justify in the space provided why you will use a Stack or a Queue in each case. (Your choice should be based on lowering cost where both data structures can be used. No marks for guessing without proper justification.) a) You start at an initial position S in a maze. Then mark all positions that can be reached in 1 step from S by 1,, all positions that be reached with a minimum of K steps from S are marked by K, and so on, until all positions are marked. Circle choice: Stack Queue Justification: b) Starting from the root of a tree find the shortest path to a leaf; where the length of a path equals the number of nodes (ignoring the root itself) from the root to a leaf. Circle choice: Stack Queue Justification: c) Starting for the root of a tree, find a leaf in the tree. Circle choice: Stack Queue Justification: #3 [30 marks] Recall the code segments for defining a binary tree node. public class BinaryTreeNode { protected Object data; protected BinaryTreeNode parent; protected BinaryTreeNode left; protected BinaryTreeNode right; Consider adding the functionality of keeping track the height of a binarytreenode in the node itsel (a) [3] Write the 1 line definition code to define nodeheight to keep track of height of a node. (b) [3] Write the code to initialize nodeheight when a new node is inserted at a leaf.

(c) [8] Write the MOST EFFICIENT code to modify nodeheight at certain intermediate nodes when a new element is inserted at a leaf. (d) [6] Given that H is the height of a binary tree: What is worst case cost of finding the height of a node in an usual binary tree (discussed in class)? What is the worst case cost of updating nodeheight of relevant intermediate nodes in (c)? What is the worst case cost of finding the height of a node in the modified binary tree described in this question? [Express your answer as a constant x (a function of H). Specifically, I need the function of H.] Consider the following code: public static boolean isfull(binarytreenode anode) { //post: returns true iff subtree rooted at the node full. // full iff size == 2^(h+1) - 1 int size; int height; int value; int i; if (anode == null) return true; size = size(anode); height = height(anode); value = 2; for (i = 1; i < height + 1; i++) value = 2 * value; value--; return size == value;

(e) [6] Given that H is the height of the binarytreenode anode: What is worst case cost of the method isfull? In 2-3 lines clearly describe how you can significantly reduce the cost of checking if a subtree is full, following parts (a) to (c). What is the worst case cost of checking if a subtree is full following the 2-3 line description you propose? [Again, express your answer as a constant x (a function of H). Specifically, I need the function of H. Full marks only for most efficient solution.] (f) [2] Write the code segment to modify the BinaryTreeNode for efficient checking if a subtree is full. (g) [2] Write the code segment to efficiently test for isfull given the modified data structure in (a) & (f).

4. (a) [7] Consider a hash function that adds the alphabetical order (1 26) of the first and the last letters of a string and computes the remainder of the sum divided by 7. Show how the strings dog, good god, gold, bold, bald, bad will be stored in the hashtable assuming string are inserted from left to right. (b) [6] Define a new hash function that avoids most of the collisions in part (a) by taking all characters & their relative positions in a string into account, and increases the size of the hash table to 14. (c) [7] Enter the strings in part (a), in left to right order, into the hash table below using the improved hash function in part (b). [No marks for this part if hash function in part (b) does not satisfy given requirements.]

5. [10] Consider the Binary Search Tree (BST) above: (a) List the elements in preorder traversal. (b) List the elements in inorder traversal. (c) List the elements in postorder traversal. (d) How many comparisons are required to find 20 in the BST above? (e) Draw the new BST after 20 is deleted from the tree. Student #: #1 #2 #3 #4 #5 Total /20 /20 /30 /20 /10 /100