Sample Questions for Midterm Exam 2

Similar documents
CS Introduction to Data Structures Week 5, 2017

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Data Structures Brett Bernstein

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

CS171 Midterm Exam. October 29, Name:

CS24 Week 8 Lecture 1

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

ECE250: Algorithms and Data Structures Midterm Review

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Recursion, Binary Trees, and Heaps February 18

CMPT 225. Binary Search Trees

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

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

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

Principles of Computer Science

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

If you took your exam home last time, I will still regrade it if you want.

INF2220: algorithms and data structures Series 1

CmpSci 187: Programming with Data Structures Spring 2015

Binary Trees. Examples:

CS171 Final Practice Exam

Topic 14. The BinaryTree ADT

TREES Lecture 12 CS2110 Fall 2016

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

Q1 Q2 Q3 Q4 Q5 Q6 Total

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 20: Binary Trees

TREES Lecture 10 CS2110 Spring2014

CS171 Final Practice Exam

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

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

ECE242 Data Structures and Algorithms Fall 2008

COMP 250 Midterm #2 March 11 th 2013

TREES Lecture 12 CS2110 Spring 2018

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

tree nonlinear Examples

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

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

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

STUDENT LESSON AB30 Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

ITI Introduction to Computing II

CS 201 Exam 3 Spring 2005 Grading Key

Multi-Way Search Tree

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

ITI Introduction to Computing II

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

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

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CS 314 Final Fall 2011

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

An Introduction to Trees

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

CSI33 Data Structures

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

CS350: Data Structures Tree Traversal

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Tree Travsersals and BST Iterators

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

CSI33 Data Structures

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

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

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

TREES. Trees - Introduction

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Programming II (CS300)

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

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Binary Search Trees 1

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Binary Trees: Practice Problems

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

Section I B COMPUTER SCIENCE SOLUTION

Binary Trees. Height 1

CSC 321: Data Structures. Fall 2012

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

CSE373 Winter 2014, Midterm Examination January 29, 2014

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

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

Tree traversals and binary trees

CS302 - Data Structures using C++

CP2 Revision. theme: dynamic datatypes & data structures

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data Structures Question Bank Multiple Choice

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?

Computer Science Foundation Exam

Computer Science Foundation Exam

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

Java 1996 AP Computer Science Question 3

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

Computer Science Foundation Exam

Transcription:

Sample Questions for Midterm Exam 2 The following are meant to give you some examples of questions that might be asked on the second midterm exam. The sample exam questions do not represent the length or difficulty of the actual exam. The actual exam will have more questions and cover a broader range of topics than what is presented here. For a list of topics and more information about midterm exam 2, see the Exams page of the course web site. Multiple Choice Choose the one best answer after reading all of the choices. 1) Given the following binary search tree: 20 11 Y X 33 70 Which of the following best represents the possible values for X and Y if duplicates are not allowed? A. X < 11 and 20 < Y < 70 B. 11 < X < 20 and 33 < Y < 70 C. X < 11 and 33 < Y < 70 D. 11 < X < 20 and Y < 20 E. X < 11 and Y > 33 2) You are given an array of size 100 where the value at each index k is k, e.g., A[0] = 0, A[1] = 1, etc. You perform a search for every value in the array, e.g., first you find 0, then 1, etc. The search uses general techniques and not techniques specific for these values. Which of the statements below is true? A. Binary search is not applicable to this problem. B. Sequential search is not applicable to this problem. C. Sequential search is always faster than binary search for every one of the 100 values searched. D. Sequential search is always slower than binary search for every one of the 100 values searched. E. Sequential search is sometimes faster, sometimes slower than binary search depending on which of the 100 values is being searched. Page: 1

The next two questions refer to the following method: public static int compute(int x, int y) { if (x == y) return x; else return compute(x + 1, y - 1); 3) What is returned by the call compute(1, 5)? A. 1 B. 2 C. 3 D. 4 E. No value is returned because an infinite recursion occurs. 4) Which of the following calls leads to an infinite recursion? i. compute (2, 8) ii. compute (8, 2) iii. compute (2, 5) A. i only B. ii only C. iii only D. i and ii E. ii and iii 5) Assume that general trees are implemented using a Treenode class that includes the following fields and methods: private List<Treenode<T>> children; public List<Treenode<T>> getchildren() { return children; and that the following class is used to represent general trees: public class Tree<T> { private Treenode<T> root; // other methods will come here Consider the following methods (assume they are in the Tree class): public int count() { return count(root); private int count(treenode<t> n) { if ((n == null) n.getchildren().isempty()) return 0; int total = 1; Iterator<Treenode<T>> iter = n.getchildren().iterator(); while (iter.hasnext()) total += count(iter.next()); return total; Which of the following best describes what the method count does? A. Always returns 0. B. Returns the number of nodes in the tree. C. Returns the number of leaves in the tree. D. Returns the number of non-leaves in the tree. E. Returns the height of the tree. Page: 2

Written Questions 1) Assume that binary trees are implemented using a BinaryTreenode class that includes the following fields and methods: // fields private T data; private BinaryTreenode<T> left, right; // methods public T getdata() { return data; public BinaryTreenode<T> getleft() { return left; public BinaryTreenode<T> getright() { return right; public void setleft(binarytreenode<t> newl) { left = newl; public void setright(binarytreenode<t> newr) { right = newr; Write the counttwochildren method whose header is given below. The method should return the number of nodes in the tree that have exactly two children. To receive full credit you must use recursion in your solution. You may not add any additional data members or methods to the BinaryTreenode class. public int counttwochildren(binarytreenode<t> N) { Page: 3

2) Given the following binary search tree: 52 33 60 21 57 75 29 64 81 72 Part a) Give the order in which the nodes would be printed using the given traversal. Note: children should be visited from left to right. Pre-order traversal: Post-order traversal: In-order traversal: Level-order traversal: Part b) Show the tree that results from inserting the value 27, deleting the value 33, and deleting the value 60 (in that order). Deletion should use the inorder successor (where appropriate). Page: 4

3) Assume that s is a non-empty string containing only digits. Consider the following recursive implementation to convert the string to the corresponding integer value (e.g., converttoint("1234") returns the integer 1234): public static int converttoint(string s) { // determine the integer value of the last character in s int value = s.charat(s.length() - 1) - '0'; // if s only has one character, return its integer value if (s.length() == 1) return value; // otherwise the integer value of s is // (integer value of s without its last character)*10 // + integer value of the last character in s return converttoint(s.substring(0, s.length() - 1))*10 + value; Analyze the time complexity for the recursive converttoint method above: Part a) Identify the problem size (what affects the method's runtime), N = Part b) Write the recurrence equations for base and recursive cases, T(1) = T(N) = Part c) State your solution that you've guessed by looking for a pattern, and Part d) Verify your solution. Part e) What is the complexity (in big-o notation) of the converttoint method? Page: 5