Está en la página 1de 9

University of Ontario Institute of Technology Faculty of Business and Information Technology

Algorithms and Data Structure (INFR 2820)

Name: ____________________________________ Student number: ______________

Please read the following instructions carefully:

1. This exam consists of 12 questions plus one bonus question, in a total of 10 pages (including this page). Please make sure that you have all the pages. 2. Be careful with your handwriting. If I cannot read your answer, it means I cannot correct it, and I will not correct it. 3. Make sure you switch off your cell phone, walkman, ipod, 4. This is a closed book exam. Only non-electronic writing utensils are allowed in the exam. 5. Print your name and student ID on this exam page. Do not detach the pages. 6. You must sign on the attendance sheet when you hand in your exam. Be prepared to show your student ID. Academic Integrity is a central value of this university. Please read and sign the following statement: I am the sole author of all the work and solutions for this exam, as submitted in my name in this paper, and I have followed the instructions stated in this page. ______________________ Students signature

Good Luck
I II III IV V VI VII VIII IX X XI XII B Total

Page 1/10

Ex I. (9 points) Select the correct answer: 1. The function nlog(n)+4 has the same growth rate as the function: (a) n (b) nlog(n4)-1 (c) n(log(n))2 (d) n2 (e) log(n)+ 4

2. A binary tree must be nonempty (a) True (b) False

3. The level of the root node in any tree is 0 (a) True (b) False

4. If a tree has only one node, the height of this tree is 0 because the number of the levels in the tree is 0 (a) True (b) False

5. The inorder traversal of a binary tree always output the data in ascending order (a) True (b) False

6. Given a singly link list implementation of a stack with n elements, what would be the complexity to perform the following operations on the stack: (you dont have to justify your answer, just write the complexity using the big-O notation) (a) (b) (c) (d) Push an element on the stack Checking if the stack is empty Destroy the stack Make a copy of the stack O( _1_) O( _1_) O( _n_) O( _n_)

Page 2/10

Ex II. (4 points) If the efficiency of the algorithm doIt() can be expressed as O(n) = n2, calculate the efficiency of the following program segment: for (i =1; i<=n ; i++) for (j =1; i<n ; i++) doIt(); k(n)=n^2 f(n)=n g(n)=n f(n)*g(n)*k(n)

f(n)*g(n)*k(n) = (n*(n)*(n^2)) = (n^4) = O(n^4)

Ex III. (7 points) a. Write the pseudocode for a recursive algorithm that receives an integer and then prints the number of digits in the integer and the sum of the digits. For example, given 12345, the algorithm would print that there are 5 digits with a sum of 15. b. Set up and solve a recurrence relation for the number of basic operations executed in the algorithm (Hint: use the addition or division as the basic operation.) (a) sum_of_digits(int x, int num) if (x == 0) return 0; else return (x % 10) + sum_of_digits(x / 10); ---------------------------------------------------------------------------------------------(b) c(n) = c(n/10) + f(n) c(n) =c(n/10) +2 f(n) = 2 => O(1) => a=1,b=10,k=0 a = b^k -> Case #2 => O(lgn) Page 3/10

// Since there are 2 BO (+,%)

O(n^k*logn)

Page 4/10

(6 points) Write the pseudocode for an algorithm to reverse the order of elements on a stack S using two addition stacks S1 and S2. You should only the basic operations on the stacks.

Ex IV. (6 points) Write the pseudocode of the function moveNthFront(Q,n) that takes as parameters, a queue Q and a positive integer n. The function moves the nth element of the queue to the front. The order of the remaining elements remains unchanged. For example, suppose: Q={5,11,34,67,43,55} and n=3 After a call to the function moveNthFront(Q,n): Q={34,5,11, ,67,43,55}

Page 5/10

Ex V. (3 points) What is the order of nodes visited in a postorder traversal (i.e. postorder sequence) of the following Binary Search Tree?

Answer: 10 28 29 26 25 34 33 35 30 50 65 40

Ex VI. (3 points) What is the order of nodes visited in a Breadth First traversal of the following Binary Search Tree?

Answer: 40 30 65 25 35 50 10 26 33 29 34 28 Page 6/10

Ex VII. (3 points) Show how the BST would look like after deleting node 35. Before After

Ex VIII. (4 points) Show how the BST would look like after deleting node 30 . Before After

* Substituting the Successor

Page 7/10

Practice Question:

List the orders of the following traversals: Pre-order, In-order, Post-order and BreadthFirst Travel (Level-Order) * Solution at end

Bonus Exercise: (2 points) Fill in the blank:

Great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. Francis Sullivan

Page 8/10

Practice Question Solutions:

Pre-order Traversal:

3784797876016459717 8234919954419366138
Post-order Traversal:

7678079974582171684 5499934191386163437
In-order Traversal:

7876790849745168217 3754999141933463861
Breadth-First Traversal:

378434791691678045 17994161769782549338

Page 9/10