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

Similar documents
CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CSE 373 Spring Midterm. Friday April 21st

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

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

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

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

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

TREES AND ORDERS OF GROWTH 7

CSC148 Week 6. Larry Zhang

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

CSL 201 Data Structures Mid-Semester Exam minutes

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

Data Structures in Java

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

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

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

CSI33 Data Structures

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

Structure and Interpretation of Computer Programs

We have the pointers reference the next node in an inorder traversal; called threads

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

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

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

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

TREES Lecture 12 CS2110 Fall 2016

CS350: Data Structures Tree Traversal

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Data Structure (CS301)

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

UNIVERSITY OF TORONTO Faculty of Arts and Science

TREES Lecture 10 CS2110 Spring2014

Exam Informatik D-MATH/D-PHYS :00 11:00

Chapter 20: Binary Trees

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

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

Computer Science Foundation Exam

CS150 Sample Final. Name: Section: A / B

Computer Science E-22 Practice Final Exam

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Red-Black trees are usually described as obeying the following rules :

Structure and Interpretation of Computer Programs

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 10, Fall 2015, Professor Prasad Jayanti

Binary Search Trees. See Section 11.1 of the text.

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

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

CSI33 Data Structures

Csci 102: Sample Exam

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Lecture 23: Binary Search Trees

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

Final Exam Version A

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

ECE264 Spring 2013 Final Exam, April 30, 2013

Suppose that the following is from a correct C++ program:

CSE 2123 Recursion. Jeremy Morris

CMSC 341. Binary Search Trees CMSC 341 BST

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

CISC-235* Test #1 January 31, 2018

Data Structures and Algorithms

15-110: Principles of Computing, Spring 2018

15-110: Principles of Computing Sample Exam #1

CS 115 Exam 3, Spring 2014

CS150 Sample Final Solution

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Tree Iterators and Properties Displayable Binary Trees

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

CSC148-Section:L0301

CSC148-Section:L0301

TREES AND ORDERS OF GROWTH 7

CIS 121. Binary Search Trees

Practical Questions CSCA48 Winter 2018 Week 11

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

DNHI Homework 3 Solutions List, Stacs and Queues

CS 331 Midterm Exam 2

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS150 - Sample Final

CS 331 Midterm Exam 2

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

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

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

TREES Lecture 12 CS2110 Spring 2018

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Data Structures and Algorithms Winter term 2016

3137 Data Structures and Algorithms in C++

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Transcription:

In addition to the correct answer, you MUST show all your work in order to receive full credit. Questions Mark: Question1) Multiple Choice Questions /10 Question 2) Binary Trees /15 Question 3) Linked List /20 Total: /45 THIS EXAM CONTAINS A TOTAL OF 10 PAGES. PAGE 9/10 IS THE APPENDIX OF YOUR EXAM 1 of 11

Question 1: Multiple Choice Questions [10] 1) Refer to the following tree when answering 1.a) and 1.b) of the multiple choice question. 1.a) What would the result of a pre-order traversal of the above tree? [2] a) 60 20 10 40 30 50 70 b)10 20 30 40 50 60 70 c)10 30 50 40 20 70 60 d) 70 60 50 40 30 20 10 e) None of the above Explain your answer (in 1 or 2 sentences): 1.b) What would the result of a post-order traversal of the above tree? [2] a) 60 20 10 40 30 50 70 b)10 20 30 40 50 60 70 c)10 30 50 40 20 70 60 d) 70 60 50 40 30 20 10 e) None of the above Explain your answer (in 1 or 2 sentences): 2 of 11

2) How many times is the string foo printed by the function call foo(4)? [3] def foo(i): if i>2: foo(i/2) foo(i/2) foo(i/2) print ( foo ) a) 3 b) 4 c) 7 d) 8 e) Something else Explain your answer: 3 of 11

3) What is the return value for the function call mysteryfunction([1,3,5,2,4,6]) [3] def mysteryfunction(x): if not x: return [] if x[0] % 2 == 0: #even return [x[0]] + mysteryfunction(x[1:]) else: return mysteryfunction(x[1:]) + [x[0]] a) The function has no base case and will result in stack overflow b) Something else c) [1,2,3,4,5,6] d) [1,3,5,2,4,6] e) [1,3,5,2,6,4] f) [1,3,5,4,6,2] g) [2,4,6,1,3,5] h) [2,4,6,5,3,1] Explain your answer: 4 of 11

Question 2) Binary Trees (See Appendix for this question) [15] a) Consider the following binary tree: [5] This binary tree has a total of 9 nodes. Write a recursive Python function such that it takes in as input a binary tree and returns back an integer that represents the count of all nodes in the binary tree. No helper functions are allowed. YOU MUST CLEARLY MARK YOUR BASE CASE(S) AND YOUR RECURSIVE CASE(S). You can safely assume that t is a binary tree using the nodes and references version. def count_nodes_in_binarytree(t): '''Given binary tree, t return an integer that represents the total count of all nodes in binary tree '' 5 of 11

b) Consider the following binary tree: [10] The path from the 5 to the 7 includes the values 5,4,11, and 7, whose sum is 27. The path from the 5 to the 2 includes the values 5,4,11 and 2, whose sum is 22. The path from the 5 to the 13 includes the values 5,8 and 13, whose sum is 26. The path from the 5 to the 1 includes the values 5,8,4, and 1, whose sum is 18. Write the following function using recursion. No helper functions are allowed. YOU MUST CLEARLY MARK YOUR BASE CASE(S) AND YOUR RECURSIVE CASE(S). You can safely assume that t is a binary tree using the nodes and references version. Hint: You need to recurse on the left and the right subtrees, what will you pass into these calls for the total parameter? def has_path_sum(t,total): '''Given binary tree, t return a boolean value indicating whether there is a path in t to any leaf whose values add up to integer total. '' 6 of 11

Question 3) Singly Linked List. (See Appendix for this question) [20] a) Write a function called removeduplicates(head) that takes in a singly linked list in increasing order and deletes any duplicate nodes from the singly linked list. You can assume that the linked list passed in will ALWAYS be in increasing order. [12] You can only traverse the list once Your function MUST BE NON-RECURSIVE. As an example if the following linked list is inputted in your function: Your function MUST return the head of the same linked list with the duplicate nodes removed: def removeduplicates(head): '''head is the head of the singly linked list. This function returns back the head of same linked list with duplicate nodes removed''' 7 of 11

b) A circular linked list is a linked list where the next of the last node refers back to the head node. Write a recursive algorithm in Python that determines if the linked list is circular or not. You MUST write two functions to implement this algorithm. The function is_circular takes in as input the head of a singly linked list and returns back True if the linked list is circular, otherwise returns False. The helper method is_circular is recursive function and MUST be called from the method is_circular. YOU MUST DECIDE CAREFULLY WHAT THE FUNCTIONAL TYPE OF THE HELPER METHOD is. Following is an example of a linked list that is circular in nature. You do not have to consider input case(s) of linked list that have circular loops not ending at the head node. [8] def is_circular(head): '''head is the head of the linked list. This function returns back True if the linked list is circular or returns back False is not. The type of head is Node def is_circular( ): ''THIS IS A RECURSIVE FUNCTION. THE FUNCTIONAL TYPE OF THIS FUNCTION MUST BE DECIDED BY YOU 8 of 11

Appendix: For Question 2 class BinaryTree: '''You can safely assume that this class has been correctly and fully implemented as per the above definition of Binary Tree.''' def init (self,rootvalue): ''Creates a new BinaryTree where, rootvalue refers to the integer value at root node. The left child is set to None, and the right child is set to None'''.. self.root=rootvalue self.left=none self.right=none From the above code of BinaryTree: t.root refers to the integer value at the root node of the tree t. t.left is a reference to the left subtree of tree t. t.right is a reference to the right subtree of tree t. You can directly refer to the instance variables of the binary search tree when writing your code. For Question3 class Node: Node is a class that represents a single Node in a Linked List def init (self,element,next=none): ''Creates a Node instance. The _element of this instance is set to element and the _next of this instance is set to next self._element=element self._next=next An example of creating a linked list using the Node class is as follows: head=node(1,node(2,node(3))) 9 of 11

Extra WorkSheet THIS SHEET WILL ONLY BE MARKED BY US IF YOU HAVE YOUR NAME AND STUDENT # AT THE VERY TOP 10 of 11

Extra WorkSheet THIS SHEET WILL ONLY BE MARKED BY US IF YOU HAVE YOUR NAME AND STUDENT # AT THE VERY TOP 11 of 11