CP114 Winter 2007 Midterm Solutions - CP114 Winter 2007 Midterm Solutions 1 of 6 file/E/ToUploadToResourcia/CP114 Midterm W2007 Ha Version 12:23 CP114

CP114 Winter 2007 Midterm Solutions - CP114 Winter 2007...

This preview shows page 1 - 3 out of 6 pages.

CP114 Winter 2007 Midterm Solutions ... 1 of 6 3/15/2007 8:06 PM Version: March 12, 2007, 12:23 CP114 Winter 2007 Midterm Solutions Written Wednesday, March 7th, 2007 The following are sample solutions and comments on the midterm. Please review this material before bringing any questions about your midterm to your instructors. Question 1 [12 marks] auxiliary function A function that accompanies another function and does a task for it. We often used it in the context of recursion. Example: Sum(n) can have an auxiliary function Sum(m,n). f is O(g) Suppose f(n) and g(n) are functions of n . Then F(n) is O(g(n)) if there exist two positive constants K and c such that |f(n)| <= K|g(n)| for all n >= c . Example: 3n^2 + 4n is O(n^2) . This question asked for a definition. Many students gave an explanation of what this means intuitively but did not define it. Many students did answer it correctly, however, and gave a definition. binary tree Either the empty tree or a node that has left and right subtrees that are binary trees. Example: x / \ a b a. [2 marks] Draw an expression tree corresponding to the following arithmetic expression: (x - y) * (a + d + e) . * / \ - + / \ / \ x y + e / \ a d b. [1 mark] Show the results of a postorder traversal of the tree in part (b). xy-ad+e+* If you got the wrong tree in b) but correctly read the postorder off the wrong tree, you still got the marks. c. [1 mark] True or false: A stack, a queue, and a priority queue are all linear structures. Explain your answer (no marks will be awarded without an explanation). False. A priority queue is not a linear structure. Its elements are arranged according to priority, not in a line. d. [2 marks] In Python lists are not of predetermined size and the programmer may grow a list as required. However, in other languages, arrays are of fixed size and this size must be specified in the program by the programmer. This can pose a problem in implementing a queue using an array since the program will delete from the front of the queue and insert in the rear of the queue. Describe the method discussed in class to deal with this situation. What structure is used? How are the pointers Front and Rear handled? A circular array is used. Front points to the first occupied position in the queue. Rear points to the first empty position after the last element in the queue. To remove an item, set Front = (Front+1) % N. To add an item, set Rear = (Rear+1)%N, where N is the size of the array. e. [1 bonus mark] Describe Cupid's job with trees (as discussed in class). He walks around the outside of the tree with his left shoulder always in contact with the tree. Depending ont he order in which he calls out nodes as he passes, he completes different traversals of the tree. f.
Image of page 1

Subscribe to view the full document.

CP114 Winter 2007 Midterm Solutions ...
Image of page 2
Image of page 3
  • Fall '13
  • sta

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes