Exam 2 Solutions 1. First solution: void swtch() { if (cnt == 0) return; if (curr == tail) { prev(); } else { E item = remove(); prev(); insert(item); next(); } } Second solution: void swtch() { if (cnt == 0) return; Link* temp = curr->next; if (temp == NULL) { prev(); } else if (curr == head) { curr = temp; } else { E item = curr->element; curr->element = temp->element; temp->element = item; } } 2. Operand eval(VarBinNode *expr) { if (expr->isLeaf()) return ((LeafNode *)expr)->value(); Operand val; Operand op1 = eval(((IntlNode *)expr)->leftchild()); Operand op2 = eval(((IntlNode *)expr)->rightchild()); switch(((IntlNode *)expr)->value()) { case '+': val = op1 + op2; break; case '-': val = op1 - op2; break; case '*': val = op1 * op2; break; case '/': val = op1 / op2; break; } return val; } 3. a. Make 25 right child of 24. b. Delete 24 from tree, make 21 right child of 19. c. Replace 9 with 11, make 14 left child of 15. d. 3, 4, 9, 11, 13, 14, 15, 17, 18, 19, 21, 22, 24, 26, 29. 4. a. The tree can be reconstructed from the Huffman codes shown in part b. b. 0 01 1 0001 2 1010 3 00000 4 100 5 110 6 001 7 1011 8 111 9 00001 c. 3, 4 d. The largest integer less than or equal to log_2 n, the smallest integer greater than or equal to log_2 n. Or, using the notation of p. 30, the floor and ceiling of log_2 n.