1 / 91

CS 61b: Final Review

CS 61b: Final Review. Data Structures Steve Sinha and Winston Liaw. DISCLAIMER. We have NOT seen the exam. We do NOT know the format of the exam What we are presenting is what we “ think is important ” for the exam. Inheritance, Method Calls Asymptotic Analysis Data Structures

armina
Download Presentation

CS 61b: Final Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 61b: Final Review Data Structures Steve Sinha and Winston Liaw

  2. DISCLAIMER We have NOTseen the exam. We do NOTknow the format of the exam What we are presenting is what we “think is important” for the exam Final Review

  3. Inheritance, Method Calls Asymptotic Analysis Data Structures Binary Search Trees B-Trees Heaps Hash Tables Graphs DFS, BFS Topological Sort Dijkstra Kruskal Sorting Skip Lists Threading, Synchronization Review Topics Final Review

  4. Inheritance/Method Calls • Given the class definitions on the next slide, which lines in class foobarbaz are illegal? Final Review

  5. package foo; import bar.bar; public class foobarbaz { static void main(String[] args) { foo f = new foo(); bar r = new bar(); baz z; r.f3(3); f.f2(3); z = (baz) f; f = new baz(); f.f2(3); z = (baz) f; z.f1(); r.f1(); ((foo) r).f1(); } } Inheritance package foo; public class foo { static void f1() {…} protected boolean f2(int x) {…} private String f3(String s) {…} } package foo; public class baz extends foo { private String f3(String s) {…} } package bar; import foo.foo; public class bar extends foo { protected boolean f3(int x) {…} } Final Review

  6. Inheritance/Method Calls • Access table: • Static methods called according to static type • Child type can be assigned to parent variable without a cast, but the reverse requires one, and the dynamic types must match Final Review

  7. package foo; import bar.bar; public class foobarbaz { static void main(String[] args) { foo f = new foo(); bar r = new bar(); baz z; r.f3(3); // ILLEGAL f.f2(3); z = (baz) f; // ILLEGAL f = new baz(); f.f2(3); z = (baz) f; z.f1(); r.f1(); // ILLEGAL ((foo) r).f1(); } } Inheritance package foo; public class foo { static void f1() {…} protected boolean f2(int x) {…} private String f3(String s) {…} } package foo; public class baz extends foo { private String f3(String s) {…} } package bar; import foo.foo; public class bar extends foo { protected boolean f3(int x) {…} } Final Review

  8. Asymptotic Analysis • O – Upper bound/Worst case • Ω – Lower bound • θ – both • o – strictly Upper bound More detail… Final Review

  9. Asymptotic Analysis T(n) is O( f(n) ) if and only if there exists positive constants C and N such that T(n) <= C f(n) for all n >= N 5 f(n) T(n) = 4n f(n) = n 4n is O( n ) T(n) f(n) n Final Review

  10. Asymptotic Analysis T(n) is O( f(n) ) if and only if there exists positive constants C and N such that T(n) <= C f(n) for all n >= N N C f(n) T(n) n Final Review

  11. Asymptotic Analysis T(n) is O( f(n) ) if and only if there exists positive constants C and N such that T(n) <= C f(n) for all n >= N T(n) is Ω( f(n) ) if and only if there exists positive constants C and N such that T(n) >= C f(n) for all n >= N Final Review

  12. Asymptotic Analysis T(n) is θ( f(n) ) if and only if • T(n) is O( f(n) ) and • T(n) is Ω( f(n) ) Examples 5n2+1 is θ(n2) 3n is O(n2), but 3n is NOT θ(n2)because 3n is not Ω(n2) Final Review

  13. Asymptotic Analysis Problem • Find the running time of the following code: int foo(int x) { int ans = 1; for (int i = 0; i < x; i++) { for (int j = 0; j < i; j++) { ans *= (i + j); } } return ans; } Final Review

  14. Asymptotic Analysis Solution • The nested loops give away the answer: the outer loop executes x times, the inner loop an average of x/2 times, for a running time of O(x2). int foo(int x) { int ans = 1; for (int i = 0; i < x; i++) { for (int j = 0; j < i; j++) { ans *= (i + j); } } return ans; } Final Review

  15. Trees: Binary Tree Tree: A B G C F E D Preorder : ABCEGFD Inorder : CEBAGDF Postorder: ECBDFGA What are the Pre-, In-, and Post- order traversals of this tree? Final Review

  16. Trees: BST Problem • Remove 8 from: 6 8 3 1 5 7 11 9 13 Final Review

  17. Trees: BST Problem • Remove 8 from: 6 8 3 1 5 7 11 9 13 Replace with successor (left-most node in right subtree) Final Review

  18. Trees: BST Solution • Final tree: 6 9 3 1 5 7 11 13 Final Review

  19. Trees: B-Tree of Order 4 / 2-3-4 Tree • Suggest a sequence of operations that would create the 2-3-4 tree.You can use removal as well as insertion. 5, 7 2, 4 6 8 Final Review

  20. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9 4, 5, 6 Final Review

  21. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9 4, 5, 6 Touch node size = 3 Final Review

  22. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7, 8, 9, 2 Remove: 9 5 4 6, 7 Final Review

  23. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7,8, 9, 2 Remove: 9 5 4 6, 7, 8 Final Review

  24. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7,8,9, 2 Remove: 9 5 4 6, 7, 8 Touch node size = 3 Final Review

  25. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7,8,9, 2 Remove: 9 5, 7 4 6 8, 9 Final Review

  26. Trees: 2-3-4 Tree Solution • Insert: 4, 5, 6, 7,8,9,2 Remove: 9 5, 7 2, 4 6 8 Final Review

  27. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Problem Final Review

  28. Priority Queues – Insertion • Insert at the last position in the heap • Reheapify up: if the element is greater than its parent, swap them and repeat • For an element at position n, its children are at 2n+1 and 2n+2 • For an element at position n, its parent is at floor[(n-1)/2] Final Review

  29. Priority Queues – Solution • Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Final Review

  30. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Solution Final Review

  31. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Solution Final Review

  32. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Solution Final Review

  33. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Solution Final Review

  34. Add 9, 76, 54, 3, 33, 21 to a max heap, using only the array based representation Priority Queues – Solution 76 Tree Representation 54 33 3 9 21 Final Review

  35. Remove the max from the heap Priority Queues – Problem Final Review

  36. Priority Queues – Removal • Replace the max element with the last element in the heap • Reheapify down: if one or both of its children is larger than it, swap with the larger of the children and repeat • For an element at position n, its children are at 2n+1 and 2n+2 • For an element at position n, its parent is at floor[(n-1)/2] Final Review

  37. Remove the max from the heap Priority Queues – Solution Final Review

  38. Remove the max from the heap Priority Queues – Solution Final Review

  39. Remove the max from the heap Priority Queues – Solution 54 Tree Representation 21 33 3 9 Final Review

  40. Hash Table Problem • Draw the structure of a size 7 hash table after insertion of keys with the following hash codes: 0, 95, 21, 6, 64, 74, 3, 54, 34, 75, 10. Final Review

  41. Hash Tables • High-level idea – 2 components • Big array called hash table of size M • Function h which maps keys to integer values • For (key, item), use h(key) % M to find location of item in table • Linked list in each entry that stores all items that map to that location (chaining) Final Review

  42. Hash Table Solution • Draw the structure of a size 7 hash table after insertion of keys with the following hash codes: 0, 95, 21, 6, 64, 74, 3, 54, 34, 75, 10. Final Review

  43. Searches (BFS and DFS) • BFS uses a queue, DFS uses a stack public void BFS/DFS(Node start) { Queue/Stack s = new Queue/Stack(); s.enqueue/push(start); while (!s.empty()) { Node n = s.dequeue/pop(); mark(n); for (all children that are not yet marked) { s.enqueue/push(child); } } } Final Review

  44. Searches (BFS and DFS) Problem • Perform BFS and DFS on the graph, starting at node 1 5 1 6 2 3 7 4 Final Review

  45. Searches (BFS and DFS) Solution • Perform BFS and DFS on the graph, starting at node 1 BFS 1 2 5 3 4 6 7 DFS 1 2 3 4 5 6 7 5 1 6 2 3 7 4 Final Review

  46. Example graph Introduction Flirt Date Hug Personal Ad Give me an ordering that I can do these in so that I don’t violate the dependencies (or my date). I can’t say what goes in this box Kiss Final Review

  47. Topological Sort • Topological sorting gives us an ordering which won’t violate these dependencies. • Perform a DFS from each source (root), marking start and finish times. • Now, our ordering is simply the nodes we visited in decreasing finishing time. Final Review

  48. Example graph 1/12 2/11 3/10 Introduction Flirt Date 4/9 13/14 Hug Personal Ad 6/7 • Personal Ad • Introduction • Flirt • Date • Hug • Kiss • Umm… 5/8 I can’t say what goes in this box Kiss Final Review

  49. Dijkstra’s Algorithm Problem • Find the shortest distances to each node from node 1 8 13 5 1 9 12 1 3 6 6 5 2 3 16 7 2 7 10 3 4 4 Final Review

  50. Dijkstra’s Algorithm • Set all distances initially to ∞, except the start node, which should be set to 0 • Construct a min priority queue of the nodes, with their distances as keys • Repeatedly remove the minimum element, updating each of its adjacent node’s distances if they are still in the queue and if the updated distance is less than the current distance Final Review

More Related