{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

35S-CS106X-Final-Solution

# 35S-CS106X-Final-Solution - CS106X Autumn 2009 Handout 35S...

This preview shows pages 1–4. Sign up to view the full content.

CS106X Handout 35S Autumn 2009 December 11 th , 2009 CS106X Final Examination Solution Problem 1: Encoding General Trees [8 points] struct genTreeNode { int value; Vector<genTreeNode *> children; }; struct binTreeNode { int value; binTreeNode *left; binTreeNode *right; }; binTreeNode *encode(Vector<genTreeNode *>& siblings, int start) { if (start == siblings.size()) return NULL; binTreeNode *root = new binTreeNode; root->value = siblings[start]->value; root->left = encode(siblings[start]->children, 0); root->right = encode(siblings, start + 1); return root; } binTreeNode *encode(genTreeNode *root) { Vector<genTreeNode *> rootAsVector; rootAsVector.add(root); return encode(rootAsVector, 0); } Criteria for Problem 1 [8 points] o Clearly understands that NULL s are relevant to the binary tree, but that NULL s never appear anywhere in the general tree: 1 point o Properly catches the base case above (or whatever base case(s) they need for their recursive decomposition. It’s possible the second recursive call is written iteratively instead, in which case the base case might come in the form of a for loop of length 0: 1 point o Understands the need to implement the primary function as a wrapper, and implements it as so: 2 points o Properly allocates one node for every node in the original tree, and copies the value over: 1 point o Properly constructs the tree that should stem from the left pointer: 1 point o Properly constructs the tree that should stem from the right pointer: 1 point o Properly returns the root of the overall tree: 1 point

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

View Full Document
3 Set<int> computeAllPossibilities(string digits) { Set<int> possibilities; computeAllPossibilities("", digits, possibilities); return possibilities; } Interestingly enough, there are two recursive layers to the above solution.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

35S-CS106X-Final-Solution - CS106X Autumn 2009 Handout 35S...

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

View Full Document
Ask a homework question - tutors are online