hwsol

hwsol - COP 3503 Final Exam Practice Problems - SOLUTIONS...

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

COP 3503 – Final Exam Practice Problems - SOLUTIONS 1. For the binary tree shown below, show the output of a preorder traversal of the tree. For additional practice do: inorder, postorder, and level order traversals . Preorder – A, B, C, D, E, F, G, H, I Additional Practice: Inorder – B, D, C, A, F, H, G, I, E Postorder – D, C, B, H, I, G, F, E, A Levelorder - A, B, E, C, F, D, G, H, I 2. Define and explain the following terms: (1) a greedy algorithm – a locally optimal decision is made by the algorithm without regards to the future consequences or overall optimality of the decision made at that point. (2) divide and conquer algorithm a recursive algorithm that consists of two parts: (1) recursively dividing the problem into smaller and smaller subproblems ultimately arriving at the base case, and (2) the solution of the original problem is formed from the solutions to the subproblems. 3. Show the contents of a stack after each of the following operations. What is the final value of the stack? (1) push (20) (2) push (15) (3) push (pop( ) - pop( )) (4) push (10) (5) push( pop( ) + pop( )) (6) top( ) 1 A B E C F G D H I

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

View Full Document
stack = [20] stack = [15, 20] stack = [ - 5] stack = [10, - 5] stack = [5] return 5 4. Shown below is an expression tree that represents a valid infix expression. Convert the expression to a prefix expression. What technique did you use to do this conversion? For additional practice assume that you want the postfix form of the expression from the tree. prefix expression is: / * c + a b * e – f g technique is a prefix traversal of the tree Additional practice : postfix traversal of the tree give the postfix expression: c a b + * e f g - * / 5. Write a pseudocode algorithm that will delete an arbitrary node identified by the pointer del from a doubly linked list. function delete (list, del) { // list is the doubly-linked list, del points to the node to delete // list node contains data field and two pointer fields: prev and next behind : pointer; behind = del.prev; del.next.prev = behind; behind.next = del.next; free(del); //return node to the heap (optional) } 2 / c * + e - a f g * b
6. Shown below is a conjecture. Complete an induction proof that proves the conjecture is true for all integer numbers greater than or equal to 1. Conjecture: = - + = - n 1 i 3 1 ) 1 n 4 )( 1 n ( n ) 1 i 2 ( i 2 Proof by Induction base case: n = 1 LHS: 2 ) 1 ( 2 ) 1 ) 1 ( 2 )( 1 ( 2 = = - RHS: 2 3 6 ) 3 )( 2 )( 1 ( 3 1 ) 1 ) 1 ( 4 )( 1 1 )( 1 ( 3 1 = = = - + inductive hypothesis: n = k = - + = - n 1 i 3 1 ) 1 k 4 )( 1 k ( k ) 1 i 2 ( i 2 induction step: n = k+1 = - + + + + = - n 1 i 3 1 ) 1 ) 1 k ( 4 )( 1 ) 1 k )(( 1 k ( ) 1 i 2 ( i 2 = - + + + + = - n 1 i 3 1 ) 1 ) 1 k ( 4 )( 1 ) 1 k )(( 1 k ( ) 1 i 2 ( i 2 = ) 3 k 4 )( 2 k )( 1 k ( 3 1 + + + LHS: ) 1 k 2 )( 2 k 2 ( ) 1 k 4 )( 1 k ( k ) 1 ) 1 k ( 2 )( 1 k ( 2 ) 1 k 2 ( k 2 ) 1 i 2 ( i 2 1 k n 1 i 3 1 k n 1 i + + + - + = - + + + - = - + = = = = = ) 6 k 18 k 12 k k 3 k 4 ( 3 1 ) 2 k 6 k 4 ( ) 1 k 4 )( k k ( 3 1 2 2 3 2 2 + + + - + = + + + - + = ) 6 k 17 k 15 k 4 ( 3 1 2 3 + + + RHS: [ ] [ ] 6 K 8 k 9 k 12 k 3 k 4 3 1 ) 3 k 4 )( 2 k 3 k ( 3 1 ) 3 k 4 )( 2 k )( 1 k ( 3 1 2 2 3 2 + + + + = + + + = + + + = [ ] 6 k 17 15 k 4 3 1 2 3 + + + Since the LHS = RHS for the inductive step, the conjecture is proven true.

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.

hwsol - COP 3503 Final Exam Practice Problems - SOLUTIONS...

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

View Full Document
Ask a homework question - tutors are online