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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Problem 2: Generating Expressions [14 points] This problem was worth a good number of points, because you needed to build a larger algorithm to solve a more complicated problem. My solution is straightforward, but it’s more straightforward than I’d expect from students trying to solve the problem under time constraints. It’s certainly the case that you needed to generate all permutations of the digit string, and for each determine all of the different ways you can construct an expression tree. There’s a lot of code to be written here. void addToPossibilities(string digitOrder, int start, int end, Set<int>& possibilities) { if (start == end) { possibilities.add(digitOrder[start] - '0'); return; } for (int mid = start; mid < end; mid++) { Set<int> left, right; addToPossibilities(digitOrder, start, mid, left); addToPossibilities(digitOrder, mid + 1, end, right); foreach (int l in left) { foreach (int r in right) { possibilities.add(l + r); possibilities.add(l - r); possibilities.add(l * r); if (r != 0) possibilities.add(l / r); } } } } void addToPossibilities(string digitOrder, Set<int>& possibilities) { addToPossibilities(digitOrder, 0, digitOrder.size() - 1, possibilities); } void computeAllPossibilities(string fixedPrefix, string remaining, Set<int>& possibilities) { if (remaining.empty()) { // fixedPrefix is a full ordering addToPossibilities(fixedPrefix, possibilities); return; } for (int i = 0; i < remaining.size(); i++) {
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.

This note was uploaded on 03/07/2010 for the course CS 201 taught by Professor Selimaksoy during the Spring '09 term at Bilkent University.

Page1 / 7

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

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