Presentation is loading. Please wait.

Presentation is loading. Please wait.

Written Midterm Solutions

Similar presentations


Presentation on theme: "Written Midterm Solutions"— Presentation transcript:

1 Written Midterm Solutions
Yang Zhengwei

2 Time Complexity

3 Time Complexity

4 Stack and Queue

5 Idea For each element, determine the state of the stack or the queue.
According to the state, judge whether an output is feasible. Answer

6 Tree

7 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

8 AVL Tree

9 Idea You could go to this web site to check every step of insertion and deletion. a) d) b) 1 c) 1, 6, 9, 12 e) upper-bound: 2^(h+1) – 1 lower bound:

10 Idea Upper bound: a complete tree of height h Lower bound:
N(h) = N(h-1) + N(h-2) do you know why? The additional 1 is the above equation is for the root node. How to Solving the recurrence equation? Fibonacci Sequence

11

12 Heap

13 Idea You could go to this web site to check every step of insertion and deletion. a) Swap the root with the last element, then delete the last element and maintain the heap from root.

14

15 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

16 Short Questions

17 Short Questions

18 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.


Download ppt "Written Midterm Solutions"

Similar presentations


Ads by Google