33S-Section-Solution

# 33S-Section-Solution - CS106X Winter 2008 Handout 33S...

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

CS106X Handout 33S Winter 2008 February 27-29, 2008 Section Solution Problem 1: Binary Tree Recursion a.) void IdentifyEndpoints(node *arbitraryNode, node *& front, node *& back) { front = back = arbitraryNode; if (front == NULL) return; while (front->left ! NULL) { front = front->left; } while (back->right != NULL) { back = back->right; } } b.) node *FlattenTree(node *root) { if (root == NULL) return NULL; node *leftList = FlattenTree(root->left); node *rightList = FlattenTree(root->right); node *leftFront, *leftBack, *rightFront, *rightBack; IdentifyEndpoints(leftList, leftFront, leftBack); IdentifyEndpoints(rightList, rightFront, rightBack); root->left = leftBack; if (leftBack != NULL) leftBack->right = root; else leftFront = root; root->right = rightFront; if (rightFront != NULL) rightFront->left = root; return leftFront; } Problem 2: Tree Rotations a) void RotateLeft(node **parentp) { node *parent = *parentp; node *rightChild = parent->right; parent->right = rightChild->left; rightChild->left = parent;

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 / 3

33S-Section-Solution - CS106X Winter 2008 Handout 33S...

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

View Full Document
Ask a homework question - tutors are online