CSCI 270 Exam 3: Recursion, Algorithm Efficiency, Trees, Exams of Data Structures and Algorithms

The third exam for csci 270, focusing on recursion, algorithm efficiency, and trees. It includes multiple-choice questions about recursion, binary search, and tree concepts, as well as a recursive algorithm to calculate the sum of integers from 1 to n. Additionally, there are exercises to trace recursive function calls, calculate the total number of operations executed, and determine the complexity of algorithms.

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-a7u
koofers-user-a7u 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 270 Exam 3 Recurse, Alg Eff, Trees (2 Aug) Name _________________________
Summer 2005
60 points total
1) (10 pts, 1pt each) Circle the correct answer, True or False, for each of the following
questions:
a) True False Recursion refers to the act of defining an object in terms of itself.
b) True False A recursive definition must include a base (or anchor) case
c) True False A recursive function can have more than one base or terminating
case.
d) True False Binary search has logarithmic complexity.
e) True False Linear search is more efficient than binary search.
f) True False Some exponential algorithms would take centuries to execute even
with the fastest possible processor.
g) True False In a tree, nodes with no outgoing arcs are called leaves.
h) True False A binary search tree has no special ordering of the nodes in the
tree.
i) True False A binary search tree can degenerate to a list for some sequences of
insertions into the tree.
j) True False A node in a general tree can have only two children at most.
pf3
pf4
pf5

Partial preview of the text

Download CSCI 270 Exam 3: Recursion, Algorithm Efficiency, Trees and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI 270 Exam 3 Recurse, Alg Eff, Trees (2 Aug) Name _________________________ Summer 2005 60 points total

  1. (10 pts, 1pt each) Circle the correct answer, True or False, for each of the following questions:

a) True False Recursion refers to the act of defining an object in terms of itself. b) True False A recursive definition must include a base (or anchor) case c) True False A recursive function can have more than one base or terminating case. d) True False Binary search has logarithmic complexity. e) True False Linear search is more efficient than binary search. f) True False Some exponential algorithms would take centuries to execute even with the fastest possible processor. g) True False In a tree, nodes with no outgoing arcs are called leaves. h) True False A binary search tree has no special ordering of the nodes in the tree. i) True False A binary search tree can degenerate to a list for some sequences of insertions into the tree. j) True False A node in a general tree can have only two children at most.

  1. (8 points) Given the following function F() and assuming ASCII representation of characters, use the method we used in class to trace the sequence of function calls and returns in evaluating calling the function with F(‘a’, ‘e’)

int F(char ch1, char ch2)

{

if (ch1 > ch2)

return 0;

if (ch1 + 1 == ch2)

return 1;

return F(ch1 + 1, ch2 – 1) + 2;

}

a) (1 Point) What is the final value returned by the initial call to F _____ 6 ______

b) (1 Point) How many base cases does the recursive function F have ____ 2 ______

  1. (5 points) Fill in the blanks below to define a recursive algorithm which will calculate the sum of the integers from 1 to n. You can assume that the initial value of n will always be positive. For example, if n is 4, your function would return 10 (4 + 3 + 2 + 1 or 1 + 2 + 3 + 4).

For Sum(n): base case: Sum(n) returns ______ 1 (or 0) _______ for n = _____ 1 (or 0) _______

general case: Sum(n) returns ____ n + Sum(n-1) _____ for n > ___ 1 (or 0) ________

  1. (3 points) Given this recursive function: void Func (int n) // 1 { a. What is the base case for Func? if (n < 10) // 2 { n >= 10 (line #5) Func (n+2); // 3 cout << n << endl; // 4 b. What line(s) carry out the action for } the base case? (identify by comment number) else return; // 5 } line #5 (and line #2 checks for n >= 10)

c. yes no The function call Func (12); results in infinite recursion.

  1. (4 points) In order to determine the complexity of several algorithms, statement counts have been computed. For each of the following statement counts, tell what the Big O of that algorithm would be:

a. O ( __ 2 n __ ) 2 n^ + 10n^2 + 15

b. O ( __ n ___ ) (5284n - 245) / 35

c. O ( __ n ___ ) 14log 2 n + 2n

d. O ( __ n^4 ___ ) 63n + 25n^2 + 2n^3 + n^4 + 91

  1. (6 points) Rank these common computing time functions in order from least efficient (slowest) to most efficient (fastest) for large values of n:

n^2 , log n, 2n, n log n, n^3 , n

least efficient 2 n^ n^3 n^2 n log n n log n most efficient

  1. (12 points) a) Draw the binary search tree (BST) that results when the following letters are inserted in the order given.

H, O, U, S, E

b) Perform inorder, preorder and postorder traversals of the tree that resulted from the insertion of the letters in a). Show the sequence of letters that results in each case:

inorder (L,V,R) : E H O S U

preorder (V, L, R) : H E O U S

postorder (L, R, V) : E S U O H