SYSC2002_2007_Winter

SYSC2002_2007_Winter - SYSC 2002 D Winter 2007 Final Exam...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
SYSC 2002 D Winter 2007 Final Exam Page 2 of 9 Question 1 (3 Marks) Catalan numbers are defined for non-negative integers as follows: Catalan(0) = 1 Catalan(n) = Catalan(n-1)*(4n+2)/(n+1), for n>0 Write a recursive function that calculates Catalan(n) . Make sure that your function does something reasonable for all integer arguments. Question 2 (4 Marks) Write a function, getMean , which given the head pointer to a linked list of integers and the Node class defined below, returns the mean of the values stored in the list. Note that the mean (sometimes called the average) is defined to be the sum of the integers divided by the number of integers in the list. If the list is empty, your function should throw an exception. Your function may be iterative or recursive. If you choose to call any other functions, you must also write their implementations. class Node { public: int value; Node *next; } Question 3 (15 Marks) a) Assuming that we start with an empty stack of integers, s , give the output of the following code fragment: (2 marks) s.push(4); s.push(5); cout << s.pop() << endl; s.push(12); s.push(3); s.pop(); cout << s.pop() << endl; b) i) Assuming that we start with an empty sorted binary tree, t . Draw the resulting tree after the following statements are executed. Note that this is a regular binary tree, not a self-balancing tree (i.e. no rotations will be performed). (3 marks) t.insert(10); t.insert(5); t.insert(8); t.insert(12); ii) Redraw your tree after the following statement is executed. (2 marks) t.delete(10);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SYSC 2002 D Winter 2007 Final Exam Page 3 of 9 iii) Redo part i) assuming that the sorted binary tree is self-balancing (i.e. rotations will be performed during insert, if necessary). Draw a tree for each step, i.e. one tree for every insertion that doesn’t involve a rotation, and two for each insertion that does involve a rotation (one showing the tree before the insertion, and the other showing the tree after the insertion). For each tree, give the balance factor of every node. For each rotation, indicate the type of rotation performed (i.e. LL, LR, RL, RR). (5 marks) c) Draw the expression tree for the following: (((10*5)+(3-2))/7) (3 marks) Question 4 (14 Marks) An English Professor wishes to study the books written by a famous author to see how often various words appear in the text. The Professor is going to store this information in a data structure and as she needs to be able to find each word quickly, hashing is appropriate. For this application we have chosen
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

SYSC2002_2007_Winter - SYSC 2002 D Winter 2007 Final Exam...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online