(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

Similar documents
Chapter 20: Binary Trees

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

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

Binary Trees, Binary Search Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Q1 Q2 Q3 Q4 Q5 Q6 Total

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

TREES. Trees - Introduction

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

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

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

Trees, Binary Trees, and Binary Search Trees

CSCI2100B Data Structures Trees

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

INF2220: algorithms and data structures Series 1

Programming II (CS300)

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

Advanced Set Representation Methods

CS171 Final Practice Exam

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

Trees: examples (Family trees)

Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

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

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Midterm solutions. n f 3 (n) = 3

"sort A" "sort B" "sort C" time (seconds) # of elements

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

CMSC351 - Fall 2014, Homework #2

Binary Trees. Height 1

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

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

Computer Science Foundation Exam. May 6, Computer Science. Section 1A. No Calculators! KEY. Score: 50

Chapter 4: Trees. 4.2 For node B :

Algorithms. AVL Tree

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

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

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Exercise 1 : B-Trees [ =17pts]

CMSC 341 Lecture 10 Binary Search Trees

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

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

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

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Binary Trees. Examples:

8. Binary Search Tree

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS350: Data Structures Binary Search Trees

March 20/2003 Jayakanth Srinivasan,

CMSC 341. Binary Search Trees CMSC 341 BST

Data Structures Question Bank Multiple Choice

Tree traversals and binary trees

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Part 2: Balanced Trees

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CS171 Final Practice Exam

// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

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

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

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

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

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

CE 221 Data Structures and Algorithms

CS 350 : Data Structures Binary Search Trees

Problem Set 4 Solutions

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

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

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

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Data Structures in Java

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

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Data Structures and Algorithms

Friday Four Square! 4:15PM, Outside Gates

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Lecture 6: Analysis of Algorithms (CS )

Trees. CSE 373 Data Structures

Trees, Part 1: Unbalanced Trees

Computer Science E-22 Practice Final Exam

Course Review for Finals. Cpt S 223 Fall 2008

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

ECE250: Algorithms and Data Structures Midterm Review

Transcription:

CPS 100 Exam 2 Solutions Spring 199 Dr Rodger 1 (3 pts) A virtual function in C++ is bound dynamically or statically? dynamic 2 (3 pts) When does one use a class template in C++? Templates are used to represent classes of dierent data types 3 (3 pts) Algorithm 1 takes N 2 +1steps Algorithm 2 takes N +10steps For what values of N, an integer > 0, is Algorithm 1 faster than Algorithm 2 0 < N < 7 4 (6 pts) Consider Quicksort and Mergesort (a) Which one does most of its work before its recursive calls? Quicksort (b) Explain why one of these is more reliable (in running time) than the other? Mergesort is more reliable because its worst case running time is O(n log n) Quicksort's usually runs in O(n log n) time, but its worst case running time is O(n 2 ) (10 pts) Consider the operation FINDMIN(D) where D is a data structure containing N items and FINDMIN returns the value of the minimum key in the data structure What is the worst case running time of this operation on the following data structures? Data Structure D array - not in sorted order O(n) array - in sorted order O(1) binary search tree O(n) 2-3 tree O(log n) mintree O(1) running time of FINDMIN 6 (9 pts) What is the worst case running time (big-o) of the following? (a) int count = 0; for (int i=1; i <= N; i+=4) { count = count + 1; O(N) 1

(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; i <= M; i++) { for (j=1; j<=n; j=j*2) { for (k=1; k<=p; k++) { count = count + 2; O(MP log N) 7 (10 pts) A trinary tree is a tree in which each node has at most three children A strictly trinary tree is a trinary tree in which each nonleaf node has exactly three children Prove by induction that a strictly trinary tree with n leaves has (3n, 1)=2 nodes Show the basis, induction hypothesis (IH) and induction step (IS) Proof: Basis:(N=1) ((3*1-1)/2 = 1) IH: Assume a strictly trinary tree with k leaves has exactly (3k, 1)=2 nodes for k<n IS: Show a strictly trinary tree with N leaves has exactly (3N, 1)=2 nodes Given a strictly trinary tree with N leaves Remove 3 leaves that have the same parent The tree is still a strictly trinary tree with N-2 leaves (removed 3 leaves, but the parent is now a leaf) By IH this tree has (3 (N, 2), 1)=2 nodes = (3N-7)/2 nodes If you add the three leaves back where they belonged, then the original tree has (3N-7)/2+3 = (3N-1)/2 nodes 8 (10 pts) Solve the following recurrence relation T(n) = O(?) Show all steps T (1)=1 T(n) = 4 T(n/2) +n 2 T(n) = 4 T(n/2) +n 2 =4[4T(n/4) + (n/2) 2 ]+n 2 =4 2 T(n/4) + 2n 2 2

= 4 2 [4T(n/8) + (n/4) 2 ]+2n 2 = 4 3 T(n/8) + 3n 2 = 4 k T(n/2 k )+kn 2 (Let n/2 k =1,! n =2 k and k = log n and n 2 =4 k ) =n 2 T(1) + log nn 2 = O(n 2 log n) 9 (10 pts) Insert the following integers (in the order given) into an empty binary search tree 8,1,3,7,9,4,2, The resulting tree is: 8 a 1 a 9 3 2 7 @ @ 4 10 (10 pts) Given the following traversals of a binary tree, draw the tree PREORDER: DQBAFNG INORDER: QBDNFAG D b b Q A B F G N 3

11 (12 pts) A node in a binary tree is described below Write a function (in C++) called height that returns the height of a binary tree (an integer) (For example, the height of a binary tree with just one node is 1) struct node { int data; node * lc; node * rc; ; // left child // right child int height(node * t) { if (t== NULL) return 0; else { int left = height(t->lc); int right = height(t->rc); if (left > right) return left+1; else return right+1; If there are n nodes in the binary tree, what is the running time of your function? O(n) 12 (14 pts) Write a function (in C++) to print out a 2-3 tree sideways in the following manner: starting with the rightmost (largest) leaf, printing one node per line, and using indentation for dierent levels (Hint: use a counter to determine which level of the tree you are on, and use that for indenting) For the tree: A node for a 23-tree is dened as: struct node23 { int type; // 1 = leaf node, 2 = nonleaf node // following fields used for nonleaf nodes int sleft; // largest value in left subtree int smid; // largest value in middle subtree node23 * lc; // pointer to left child node23 * mc; // pointer to middle child node23 * rc; // pointer to right child // following field used for leaf nodes int data; // data item 4

10 : 32 HHHH?H Hj :8 21 : 32 36 : 63 =? J J^ J J^? @ @R 8 10 21 32 36 63 69 The output is: 10 : 32 36 : 63 21 : 32 : 8 69 63 36 32 21 10 8 void print(node23 * t, int level) { if (t==null) return; if (t->type == 1) { indent(level); cout << t->data << endl; else { print(t->rc, level+1); print(t->mc, level+1); indent(level); cout << t->sleft << ":" << t->smid << endl; print(t->lc, level+1); void indent(int count) { int j; for (j=1; j<count; j++) cout << "\t"; // print a tab