CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Similar documents
INSTITUTE OF AERONAUTICAL ENGINEERING

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

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

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

CPSC 331 Term Test #2 March 26, 2007

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Question Bank Subject: Advanced Data Structures Class: SE Computer

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

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

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

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

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Priority Queues and Binary Heaps

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

CS301 - Data Structures Glossary By

Merge Sort Goodrich, Tamassia Merge Sort 1

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Table of Contents. Chapter 1: Introduction to Data Structures... 1

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

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

CSCE 2014 Final Exam Spring Version A

Summer Final Exam Review Session August 5, 2009

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

Course Review. Cpt S 223 Fall 2009

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

Data Structures Question Bank Multiple Choice

Section 1: True / False (1 point each, 15 pts total)

Analysis of Algorithms

Trees. Eric McCreath

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Department of Computer Science and Technology

Computer Science E-22 Practice Final Exam

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Sorting and Selection

Basic Data Structures (Version 7) Name:

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

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?

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

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

a) State the need of data structure. Write the operations performed using data structures.

Midterm solutions. n f 3 (n) = 3

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

MLR Institute of Technology

ECE250: Algorithms and Data Structures Midterm Review

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

COMP 250 Midterm #2 March 11 th 2013

Course Review for Finals. Cpt S 223 Fall 2008

Stores a collection of elements each with an associated key value

Code No: R Set No. 1

CSL 201 Data Structures Mid-Semester Exam minutes

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

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

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

CH 8. HEAPS AND PRIORITY QUEUES

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

CH. 8 PRIORITY QUEUES AND HEAPS

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

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

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Cpt S 122 Data Structures. Data Structures Trees

Elementary Data Structures 2

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

Programming II (CS300)

1. AVL Trees (10 Points)

Why Do We Need Trees?

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

Binary Trees

CS 315 Data Structures mid-term 2

AP Computer Science 4325

About this exam review

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

UNIT III BALANCED SEARCH TREES AND INDEXING

Final Examination CSE 100 UCSD (Practice)

DATA STRUCTURES AND ALGORITHMS

Final Exam. EECS 2011 Prof. J. Elder - 1 -

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Binary Search Trees. CSE2100 DS & Algorithms 1

DIT960 Datastrukturer

Transcription:

CSE 2100 Data Structures and Introduction to Algorithms...! In Java!! Instructor: Fei Wang! Mid-Term Exam CSE2100 DS & Algorithms 1

1. True or False (20%=2%x10)! (1) O(n) is O(n^2) (2) The height h of a binary search tree of size n is O(log n) (3) In an AVL tree, the depths of any two external nodes differ by at most 1 (4) Splay tree is balanced (5) A stack follows the first-in-first-out rule (6) A heap is a binary search tree (7) An AVL tree is a heap (8) In a circular doubly linked list with 10 nodes, we will need to change 4 links if we want to delete a node other than the head node (9) Inserting an element to the beginning of an array (that is A[0] element) is more difficult than inserting an element to the beginning of a linked list (10) The worst-case height of a binary search tree is O(log n) CSE2100 DS & Algorithms 2

15 9 20 5 12 15 30 4 7 16 40 6 6 CSE2100 DS & Algorithms 3

2. (10%) Draw a binary tree with 10 nodes numbered from 1 to 10 for which the preorder and post- order traversals are 5, 2, 1, 3, 4, 7, 6, 8, 9, 10, respectively 1, 4, 3, 2, 6, 10, 9, 8, 7, 5. Is there more than one tree satisfying these properties? CSE2100 DS & Algorithms 4

3. (10%) Describe a linear-time algorithm that lists the nodes of a binary tree in level-order: first list the root, then the nodes of depth 1, followed by nodes of depth 2, and so on. Nodes at the same depth should be listed in left-to-right order. This can be achieved by using a FIFO queue Q. We start by inserting the root of the tree in Q, then, while Q is not empty we extract the node at the front of the queue and insert its children (in left-to-right order) at the end of the queue. CSE2100 DS & Algorithms 5

4. (10%) Draw the 11-entry hash table that results from using the hash function (3i+1) mod 11, to hash the keys 12,44,13,88,23,94,11,39,20,16 and 5, assuming collisions are handled by (1) chaining and (2) linear probing. 0 1 2 3 4 5 6 7 8 9 10 44 12 16 20 13 94 88 23 39 11 0 1 2 3 4 5 6 7 8 9 10 44 88 11 12 23 20 13 94 39 16 CSE2100 DS & Algorithms 6

5. (20%) In the following questions, consider the list of numbers: 62, 31, 70, 91, 25, 11, 9, 61, 73, 6. a). Show the result of inserting the numbers in the list in the same order specified above into an initially empty minimum heap. Note that you need to show how the heap looks like after each number is inserted. (5%) 25 9 6 31 70 31 11 9 11 61 62 70 25 61 31 70 25 82 62 91 91 73 62 CSE2100 DS & Algorithms 7

b) Show the result of inserting the numbers in the list in the same order specified above into an initially empty binary search tree. Note that you need to show how the binary search tree looks like after each number is inserted. (5%) 62 62 62 31 70 31 70 31 70 61 25 91 61 25 91 25 91 11 11 73 9 9 6 CSE2100 DS & Algorithms 8

c) Use the binary search tree you created in question 2. What are the two possible binary search trees after 62 is deleted? (5%) 61 70 31 70 31 91 25 91 25 61 73 11 73 11 9 9 6 6 CSE2100 DS & Algorithms 9

d) Explain how you can utilize a heap to sort the list of numbers in descending order. (3%) Let n be the number of elements in the list. What is the time complexity of your sorting algorithm? (2%) (1) step1: Delete the root from the min heap and put it into stacka. O(1) step2: Restructure the min heap. O(log n) step3: Go step1 until the min heap is empty. ntimes step4: Pop items from stacka.(the result is in descending order.) O(n) (2) O(n log n) CSE2100 DS & Algorithms 10

6. (10%) Given an AVL tree shown below, please draw the process on how such AVL tree will adjust after we insert 40 and 10. 15 9 20 12 30 CSE2100 DS & Algorithms 11

15 9 20 12 30 40 CSE2100 DS & Algorithms 12

15 9 30 12 20 40 CSE2100 DS & Algorithms 13

15 9 30 12 20 40 10 CSE2100 DS & Algorithms 14

15 9 30 12 20 40 10 CSE2100 DS & Algorithms 15

15 9 30 10 20 40 12 CSE2100 DS & Algorithms 16

15 10 30 9 20 12 40 CSE2100 DS & Algorithms 17

7. (10%) For a tree T, let ni denote the number of its internal nodes, ne denote the number of its external nodes. Show that if every internal node in T has exactly C children, then ne=(c-1)ni+1 CSE2100 DS & Algorithms 18

For ni = 0, then ne = 1. For ni = 1, then ne = C-1 +1 = C. Assume the ne equation holds true for k < k, i.e., for any ni = k < k, ne = (C-1)nI +1. Now consider ni = k. Then, ne = (C-1)(k 1) + 1 + C-1. That is, the number of external nodes is equal to the number of external nodes for a tree with k 1 internal nodes plus C (we added an internal node which must have C children) minus 1 (in creating the new internal node, we made an external node into an internal node). Thus, ne = (C-1)k+1. CSE2100 DS & Algorithms 19

8. (10%) The Tower of Hanoi is a mathematical game. The game consists of 3 rods, and n disks of different sizes which can slide onto any rod. The puzzle starts with the disks stack in ascending order of size on rod number 1 (the smallest disk is on the top). The objective of the game is to move the entire stack to rod number 3, obeying the following rules: a). Only one disk can be moved at a time. b). Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. c). No disk may be placed on top of a smaller disk. In this problem, please write an algorithm to output a series of moves which solve the Tower of Hanoi problem. The input and output are as follows: Input: one single number which represents the number of disks on the rod Output: Each line of output consists of a pair (i, j), which represents a move from rod number i to rod number j. The sequence of moves should move the entire stack from rod number 1 to rod number 3. Example: Input: 3; Output: (1,3) (1,2) (3,2) (1,3) (2,1) (2,3) (1,3) CSE2100 DS & Algorithms 20

hanoi(from, tmp, to, n){ if(n==1){ move(from,to); return; } hanoi(from, to, tmp, n-1); System.out.println("("+from.toString()+","+to.toString()+")") hanoi(tmp, from, to, n-1); return; }! CSE2100 DS & Algorithms 21

9. (10% Extra Credit) Let A and B be two sets of integer keys stored at the internal nodes of two AVL trees, T A and T B, respectively. Suppose that all keys are distinct. We say that A separates B if there is a triplet of keys x<y<z,such that x and z are in B and y is in A. Design an algorithm that takes as input T A andt B and determines whether or not A separates B. Describe the algorithm in English or pseudo-code, including any additional data structures. For full credit the algorithm must have worst-case time complexity of O(log n), where n denotes the total number of keys in A and B. CSE2100 DS & Algorithms 22

Without loss of generality we can assume that x and z are the minimum, respectively the maximum keys of B, and we have to decide in O(log n) time if A contains a key y between them. We can do so by searching x=min(b) in T A. Let u the node at which the search stops (u is either an internal node storing key x or an external node). Find the internal node v following u in inorder using (possibly twice) the inordernext() method from problem 3. If the key stored at v is smaller than z=max(b), then it can be used as y, proving that A separates B. Otherwise no key of A is between x and z, and A does not separate B. The overall running time is O(log n) since the heights of AVL trees T A and T B are O(log n), and thus computing x=min(b) and z=max(b), searching for x in T A, and the calls to inordernext() take O(log n) time each. CSE2100 DS & Algorithms 23

CSE 2100 Data Structures and Introduction to Algorithms...! In Java!! Instructor: Fei Wang! Sorting CSE2100 DS & Algorithms 24

Merging The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x 1 x 2 x m ) and Y(y 1 y 2 y n ) the resulting list is Z(z 1 z 2 z m+n ) Example: L 1 = { 3 8 9 } L 2 = { 1 5 7 } merge(l 1, L 2 ) = { 1 3 5 7 8 9 } CSE2100 DS & Algorithms 25

Merging X: 3 10 23 54 Y: 1 5 25 75 Result: CSE2100 DS & Algorithms 26

Merging 3 10 23 54 5 25 75 X: Y: Result: 1 CSE2100 DS & Algorithms 27

Merging 10 23 54 5 25 75 X: Y: Result: 1 3 CSE2100 DS & Algorithms 28

Merging 10 23 54 25 75 X: Y: Result: 1 3 5 CSE2100 DS & Algorithms 29

Merging X: Y: 23 54 25 75 Result: 1 3 5 10 CSE2100 DS & Algorithms 30

Merging X: Y: 54 25 75 Result: 1 3 5 10 23 CSE2100 DS & Algorithms 31

Merging X: Y: 54 75 Result: 1 3 5 10 23 25 CSE2100 DS & Algorithms 32

Merging X: Y: 75 Result: 1 3 5 10 23 25 54 CSE2100 DS & Algorithms 33

Merging X: Y: Result: 1 3 5 10 23 25 54 75 CSE2100 DS & Algorithms 34

Divide and Conquer Merging a two lists of one element each is the same as sorting them. Merge sort divides up an unsorted list until the above condition is met and then sorts the divided parts back together in pairs. Specifically this can be done by recursively dividing the unsorted list in half, merge sorting the right side then the left side and then merging the right and left back together. CSE2100 DS & Algorithms 35

Merge Sort Given a list L with a length k: If k == 1 à the list is sorted Else: Merge Sort the left side (1 thru k/2) Merge Sort the right side (k/2+1 thru k) Merge the right side with the left side CSE2100 DS & Algorithms 36

Example 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 37

Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 38

Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 39

Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 40

Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 4 0 CSE2100 DS & Algorithms 41

Example 99 6 86 15 58 35 86 0 4 Merge 4 0 CSE2100 DS & Algorithms 42

Example 6 99 15 86 58 35 0 4 86 99 6 86 15 58 35 86 0 4 Merge CSE2100 DS & Algorithms 43

Example 6 15 86 99 0 4 35 58 86 6 99 15 86 58 35 0 4 86 CSE2100 DS & Algorithms 44

Example 0 4 6 15 35 58 86 86 99 6 15 86 99 0 4 35 58 86 CSE2100 DS & Algorithms 45

Example 0 4 6 15 35 58 86 86 99 CSE2100 DS & Algorithms 46

Implementation There are two basic ways to implement merge sort: In Place: Merging is done with only the input array Pro: Requires only the space needed to hold the array Con: Takes longer to merge because if the next element is in the right side then all of the elements must be moved down. Double Storage: Merging is done with a temporary array of the same size as the input array. Pro: Faster than In Place since the temp array holds the resulting array until both left and right sides are merged into the temp array, then the temp array is appended over the input array. Con: The memory requirement is doubled. CSE2100 DS & Algorithms 47

CSE2100 DS & Algorithms 48