Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Similar documents
Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Question 0. Do not turn this page until you have received the signal to start.

Do not turn this page until you have received the signal to start.

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Duration: 90 minutes. Page 1 of 14 Q2: /13 Q3: /13 TOTAL: /38. Bonus /1

Do not turn this page until you have received the signal to start.

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Binary Search Tree. Revised based on textbook author s notes.

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

TREES AND ORDERS OF GROWTH 7

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSI33 Data Structures

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

First Name: Last Name: Q1: /10

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

NONE (in particular, no calculator)

Final Examination CSE 100 UCSD (Practice)

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CSCA48 Term Test 1 Seminar

Question 0. Do not turn this page until you have received the signal to start.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

UNIVERSITY OF TORONTO Faculty of Arts and Science

CSI33 Data Structures

CSC148H Week 8. Sadia Sharmin. July 12, /29

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

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

Structure and Interpretation of Computer Programs

Technical University of Denmark

n 1 i = n + i = n + f(n 1)

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

Section 4 SOLUTION: AVL Trees & B-Trees

Please submit your request directly to Noha Sinno

Sample Exam 1 Questions

UNIVERSITY REGULATIONS

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Trees. Tree Structure Binary Tree Tree Traversals

CSC148-Section:L0301

Name: utorid: U of T

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Short Python function/method descriptions:

Exam Data structures DAT036/DAT037/DIT960

CSC148-Section:L0301

Student Number: UTORid: Question 0. [1 mark] Read and follow all instructions on this page, and fill in all fields.

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Structure and Interpretation of Computer Programs

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

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Last name... First name... Q1: / 5 Q2: / 6 Q3: / 9

INFO1x05 Tutorial 09

Chapter 20: Binary Trees

Practical Questions CSCA48 Winter 2018 Week 11

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

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

Principles of Computer Science

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Data Structures and Algorithms Winter term 2016

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

Algorithms. AVL Tree

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC148 Week 7. Larry Zhang

CSE 373 Spring Midterm. Friday April 21st

Final Exam. Name: Student ID: Section: Signature:

Do not turn this page until you have received the signal to start.

Binary Tree Applications

CSE 332, Spring 2010, Midterm Examination 30 April 2010

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

ECE 250 Algorithms and Data Structures

Student Number: Instructor: Brian Harrington

Computer Science Foundation Exam

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

EECS 311 Data Structures Midterm Exam Don t Panic!

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

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

CS 104 (Spring 2014) Final Exam 05/09/2014

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

CSC148-Section:L0301

8. Binary Search Tree

Dictionaries. Priority Queues

Heaps. Heaps Priority Queue Revisit HeapSort

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

UNIVERSITY OF TORONTO SCARBOROUGH. Fall 2015 EXAMINATIONS. CSC A20H Duration 3 hours. No Aids Allowed

Student Number: Lab day:

Structure and Interpretation of Computer Programs

Second Examination Solution

Binary Search Trees. Analysis of Algorithms

CS 315 Data Structures mid-term 2

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CSC148 Recipe for Designing Classes

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

INF2220: algorithms and data structures Series 1

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Exam Data structures DIT960/DAT036

Short Python function/method descriptions:

MIDTERM EXAM (HONORS SECTION)

CS102 Binary Search Trees

Transcription:

CSC A48 Winter 2014 CSCA48 Final Exam 23 April 2014 Duration: Aids Allowed: 150 minutes None Student Number: UTORid: Last (Family) Name(s): First (Given) Name(s): Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully. Marking Guide # 1: / 1 This term test consists of 8 questions on 16 pages (including this one), printed on both sides of the paper. When you receive the signal to start, please make sure that your copy of the test is complete, fill in the identification section above, and write your name on the back of the last page. Answer each question directly on the test paper, in the space provided, and use one of the blank pages for rough work. If you need more space for one of your solutions, use a blank page and indicate clearly the part of your work that should be marked. # 2: / 7 # 3: / 7 # 4: / 5 # 5: / 4 # 6: / 7 # 7: / 5 # 8: / 6 TOTAL: /42 Good Luck! Page 1 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 2 of 16

CSCA48 Final Exam 23 April 2014 Question 1. [1 mark] Draw a diagram of an empty tree Question 2. [7 marks] Part (a) [3 marks] Let f and g be two functions. Give the formal definition of f = O(g) Part (b) [2 marks] On your answer to part a), underline the part(s) that convey the idea of within a constant factor Part (c) [2 marks] From our class discussion, explain why we needed to include the idea of within a constant factor in the definition of big-oh Page 3 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 4 of 16

CSCA48 Final Exam 23 April 2014 Question 3. [7 marks] Part (a) [4 marks] What is the output of the following program? def weird_version(s): """ (str) -> str Return a weird version of string s. """ if len(s) < 2: return s else: return s[1] + weird_version(s[2:]) + s[0] if name == " main ": print(weird_version("a48weird")) print(weird_version("abc12345def")) Part (b) [3 marks] Suppose we made a new version of the function weird called weirdplus, where we changed the line if len(s) < 2: to if len(s) < 3: What can we say about a string s if we know that weird(s)!= weirdplus(s)? Page 5 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 6 of 16

CSCA48 Final Exam 23 April 2014 Question 4. [5 marks] Consider the following class for a binary tree node. class BTNode: def init (self, left=none, right=none): """ (BTNode, BTNode, BTNode) -> NoneType Initialize a new BTNode with pointers to subtrees left and right. """ self.left = left self.right = right Using the above class, write a function called sum_depths, which takes the root of a binary tree, and returns the sum of the depths of all the nodes in that tree. For example, if r is the root of a tree that has 1 node at depth 0, 2 at depth 1, 3 at depth 2, and 5 at depth 3, then sum_depths(r) should return 23 (because 1 0 + 2 1 + 3 2 + 5 3 equals 23). Page 7 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 8 of 16

CSCA48 Final Exam 23 April 2014 Question 5. Part (a) [2 marks] [4 marks] Suppose the numbers 1,2,3,4,5,6,7,8,9 were found in a min heap which is shaped like the following tree. The numbers could have been inserted in any order. 1 1 1 1 1 1 1 1 1 Mark with an X all the nodes where it is possible to find the number 3. Part (b) [2 marks] Instead of using a tree, we can hold the same heap in the list below. 1 1 1 1 1 1 1 1 1 Mark with an X all the places in the list where it is possible to find the number 7. Page 9 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 10 of 16

CSCA48 Final Exam 23 April 2014 Question 6. [7 marks] The following classes were defined in the starter code of your regular expression assignment. UnaryTree, BarTree, BinaryTree, DotTree, Leaf, RegexTree, StarTree. Part (a) [3 marks] For each of these classes, give its child classes. Write none if a class has no subclasses. To help you get started, we completed the question for the first class. Children of UnaryTree: StarTree. Children of BarTree: Children of BinaryTree: Children of DotTree: Children of Leaf: Children of RegexTree: Children of StarTree: Part (b) [4 marks] StarTree is the only subclass of UnaryTree. So by defining StarTree as a child class of the parent of UnaryTree, we could have avoided the need for the UnaryTree class altogether. Give two reasons why our design included the UnaryTree class. Reason #1: Reason #2: Page 11 of 16

CSC A48 Winter 2014 def del_index(head, tail, index2del): """ (DLLNode, DLLNode, int) -> (DLLNode, DLLNode) Remove the item at index index2del from the doubly linked list with head head and tail tail (both None if list empty). Return the head and tail of the updated list. If the items were stored in a python list L in the same order, then the effect of executing del_index(head, tail, index2del) should be the same as that of executing del L[index2del]. Note how index2del can be negative. REQ: -N <= index2del < N, where N is the number of items in the list. """ # find the node to delete # if deleting head # if deleting tail # start at back of list # start at front of list # unlink node to delete curr = curr.next_link curr = curr.prev_link curr = head curr = tail curr.next_link.prev_link = curr.prev_link curr.prev_link.next_link = curr.next_link else: else: else: head = curr.next_link if (curr.next_link == None): if (curr.prev_link == None): if index2del >= 0: index2del += 1 index2del -= 1 return (head, tail) tail = curr.prev_link while (index2del < -1): while (index2del > 0): Page 12 of 16

CSCA48 Final Exam 23 April 2014 Question 7. [5 marks] For the doubly linked list exercise, Nick had written a well documented and correct helper function. Then the CODE MANGLER struck. Dun dun dunnn... He (she?) did the following to all of Nick s code after the docstring, including comments. Deleted all blank lines. Removed all leading spaces (i.e., removed all indentation). Permuted, or shuffled, all the lines. As a result, Nick s code wound up as it appears on the opposite page. In the space below, reconstruct Nick s original code (you don t need to re-write the header or docstring). Page 13 of 16

CSC A48 Winter 2014 Use the space on this blank page for scratch work, or for any solution that did not fit elsewhere. Clearly label each such solution with the appropriate question and part number. Page 14 of 16

CSCA48 Final Exam 23 April 2014 Question 8. [6 marks] Part (a) [2 marks] Draw the binary search tree (BST) we would get if we were to insert the letters M A N G L E R in the order listed (i.e., M is the first letter added to the tree). In case you need it, here are all the upper case letters in alphabetical order. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Advice: Do this part very carefully. Any error here will likely lead to wrong answers in part (b) also. Part (b) [4 marks] Perform two rotations on the BST from part (a) so to have G at the root. Draw the tree after each rotation. Page 15 of 16

CSC A48 Winter 2014 CSCA48 Final Exam 23 April 2014 On this page, please write nothing except your name. Last (Family) Name(s): First (Given) Name(s): Total Marks = 42 Page 16 of 16