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.

CSI33 Data Structures

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Student Number: Lab day:

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

Short Python function/method descriptions:

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

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

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

UNIVERSITY OF TORONTO Faculty of Arts and Science

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

CSI33 Data Structures

What is an algorithm?

Short Python function/method descriptions:

Lecture 12 ADTs and Stacks

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

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSCA48 Term Test 1 Seminar

NONE (in particular, no calculator)

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSI33 Data Structures

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

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. In the meantime, please read the instructions below carefully.

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Structure and Interpretation of Computer Programs

CSC A20H3 S 2011 Test 1 Duration 90 minutes Aids allowed: none. Student Number:

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

Student Number: Instructor: Brian Harrington

15-110: Principles of Computing, Spring 2018

CSC 108H1 F 2009 Test 1 Duration 35 minutes Aids allowed: none. Student Number:

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

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

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

First Name: Last Name: Q1: /10

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

Final Examination CSE 100 UCSD (Practice)

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

TREES AND ORDERS OF GROWTH 7

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

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

Computer Science E-22 Practice Final Exam

Lecture 16: Binary Search Trees

Binary Trees. Binary Search Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Structure and Interpretation of Computer Programs

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

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

Data Structures and Algorithms Winter term 2006/2007 Final Exam

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

CS 115 Exam 3, Fall 2011

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

: Intro Programming for Scientists and Engineers Final Exam

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

Introduction to Computer Science and Programming for Astronomers

CSC148 Recipe for Designing Classes

Data Structures CSCI C343, Fall 2014

Structure and Interpretation of Computer Programs

Walk through previous lectures

CSC 108H1 F 2010 Test 1 Duration 45 minutes Aids allowed: none. Student Number:

Name: utorid: U of T

CS 331 Summer 2017 Final Exam

Structure and Interpretation of Computer Programs

1 / 20 2 / 20 3 / 20 4 / 20 5 / 20

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

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

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

Math 15 - Spring Homework 5.2 Solutions

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Extended Introduction to Computer Science CS1001.py

[key, Left subtree, Right subtree]

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

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

CS115 - Module 8 - Binary trees

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

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

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

Structure and Interpretation of Computer Programs

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

CSC148 Week 2. Larry Zhang

Self-Balancing Search Trees. Chapter 11

Welcome to CSC148! Introduction to Computer Science

CS 1110 Prelim 1 March 15th, 2016

Transcription:

CSC 148 H1 / L0101 Term Test # 2 13 March 2013 Duration: Aids Allowed: 50 minutes None Student Number: 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. This term test consists of 4 questions on 10 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. In your answers, you may use any of the Python builtin functions and standard modules listed on the accompanying Python reference sheet. You must write your own code for everything else. Comments and docstrings are not required, except where indicated, although they may help us grade your answers. Also, they may be worth part marks if you cannot write a complete answer. If you are unable to answer a question (or part of a question), remember that you will get 10% of the marks for any solution that you leave entirely blank (or where you cross off everything you wrote to make it clear that it should not be marked). Marking Guide # 1: /12 # 2: /10 # 3: /10 # 4: / 8 TOTAL: /40 Good Luck! Page 1 of 10

CSC 148 H1 / L0101 Term Question 1. [12 marks] Implement each method in class Stack below. Use instances of class LLNode to store the items in a linked list. Do not use any builtin Python container in your solution! (No list, set, dict, file, or even str.) Your code will be marked on its style and design in addition to its correctness. class LLNode: """A node in a linked list.""" def init (self, item, link=none): self.item = item self.link = link class EmptyStackError(Exception): pass class Stack: """Implementation of the Stack ADT.""" def init (self): """Initialize this stack to be empty.""" def push(self, item): """Add item to the top of this stack.""" [... continued on the next page... ] Page 2 of 10

Test # 2 13 March 2013 Question 1. (continued) #...class Stack, continued... def pop(self): """Remove the top item from this stack and return that item. Raise EmptyStackError if this stack is empty.""" def is_empty(self): """Return True iff this stack is empty.""" Page 3 of 10

CSC 148 H1 / L0101 Term Question 2. [10 marks] Consider the following node class (for linked lists). class LLNode: """A node in a linked list.""" def init (self, item, link=none): self.item = item self.link = link Write the body of the function below to satisfy its docstring. This will require a bit of thinking, so it would be a good idea to provide some internal comments to describe what you are trying to do. Your code will be marked on its style and design in addition to its correctness. def split_even(head): """(LLNode) -> NoneType Modify the linked list starting at node head so that every even integer in the list becomes two separate values, each equal to half of the original (this will require adding new nodes to the list). Precondition: every item in the linked list is an integer. For example, if head refers to a linked list as follows: head: 1 -> 2 -> 3 -> 4 then after the call split_even(head), the linked list should look like this (the 2 has been split into two 1 s and the 4 into two 2 s): head: 1 -> 1 -> 1 -> 3 -> 2 -> 2 (Note: this function does NOT return anything.) """ Page 4 of 10

Test # 2 13 March 2013 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 5 of 10

CSC 148 H1 / L0101 Term Question 3. [10 marks] Consider the following node class (for binary trees). class BTNode: """A node in a binary tree.""" def init (self, item, left=none, right=none): self.item = item self.left = left self.right = right Part (a) [4 marks] Write the body of the method below to satisfy its docstring. (Note: This is a method inside class BTNode.) Your code will be marked on its style and design in addition to its correctness. def reverse(self): """(BTNode) -> NoneType Replace every item in the subtree rooted at this node with its reverse. Precondition: every item in the subtree rooted at this node is a string. For example, if self is a reference to the root of the tree pictured on the left below, then after this method finishes executing, the tree s items have changed as pictured on the right. before reverse() after reverse() "this" "siht" / \ / \ "test" "is" "tset" "si" / / "fun" "nuf" Reminder: if s refers to a string, then s[::-1] is the reverse of s. """ Page 6 of 10

Test # 2 13 March 2013 Question 3. (continued) Part (b) [6 marks] Write the body of the function below to satisfy its docstring. (Note: This is a function outside class BTNode.) Your code will be marked on its style and design, in addition to its correctness. def leaf_list(root): """(BTNode) -> [object] A Return a list of every item stored in a leaf of the binary tree / \ rooted at root. For example, if root refers to the root of the B C binary tree pictured on the right, then the return value should / be [B, D] (or [D, B] -- order does not matter). D """ Page 7 of 10

CSC 148 H1 / L0101 Term Question 4. [8 marks] Part (a) [4 marks] Some of the trees pictured below are Binary Search Trees; others are not. Circle every tree that is a Binary Search Tree. 10 10 5 5 5 15 5 10 15 10 15 15 Part (b) [4 marks] Let t be the binary search tree pictured below. Use the rest of the space on this page to draw a picture of the tree after performing the operation t.remove(10). (For your reference, we provide parts of the BST code from class on the next page.) t.root: 5 3 10 7 12 6 9 8 Page 8 of 10

Test # 2 13 March 2013 Question 4. (continued) Part (b) (continued) class BST: #...other methods omitted... def remove(self, item): self.root = _remove(self.root, item) def _remove(root, item): if root is None: pass elif item < root.item: root.left = _remove(root.left, item) elif item > root.item: root.right = _remove(root.right, item) else: # item == root.item if root.left is None or root.right is None: root = root.left or root.right else: root.item, root.left = _remove_max(root.left) return root def _remove_max(root): if root.right is None: return root.item, root.left else: largest, root.right = _remove_max(root.right) return largest, root Page 9 of 10

CSC 148 H1 / L0101 Term Test # 2 13 March 2013 On this page, please write nothing except your name. Last (Family) Name(s): First (Given) Name(s): Total Marks = 40 Page 10 of 10