Written Midterm Solutions Yang Zhengwei
Time Complexity
Time Complexity
Stack and Queue
Idea For each element, determine the state of the stack or the queue. According to the state, judge whether an output is feasible. Answer
Tree
Idea Traverse the tree, at the same time label it Answer (b) Pre-order:8,4,2,1,3,6,5,7,12,10,9,11,14,13,15 Post-order:1,3,2,5,7,6,4,9,11,10,13,15,14,12,8 (c) 2^n (d) find the root of every subtree when rebuild the tree
AVL Tree
Idea You could go to this web site to check every step of insertion and deletion. https://www.cs.usfca.edu/~galles/visualization/AVLtree.html a) d) b) 1 c) 1, 6, 9, 12 e) upper-bound: 2^(h+1) – 1 lower bound:
Idea Upper bound: a complete tree of height h Lower bound: N(h) = N(h-1) + N(h-2) + 1 do you know why? The additional 1 is the above equation is for the root node. How to Solving the recurrence equation? Fibonacci Sequence
Heap
Idea You could go to this web site to check every step of insertion and deletion. https://www.cs.usfca.edu/~galles/visualization/Heap.html a) Swap the root with the last element, then delete the last element and maintain the heap from root.
Short Questions The operators are associated right-to-left, so we calculate right first. 10-(20 * 8 / (5 – 2 – 1)) – 20 = 10 – (20 * 8 / (5 – (2 – 1)) – 20 …= - 10
Short Questions
Short Questions
Short Questions d) No, not search tree e) f) pointer: worst case of push/pop always O(1) array: only need to store data itself, no pointers Delete a node / Reverse the list A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.