CMSI 281 DATA STRUCTURES AND ALGORITHMS I FINAL EXAM PREPARATION The preparation is in two parts: 1. An outline of course topics (to help you remember things and see how everything fits together) 2. A previous year's exam, with solutions! You should really study. Course Outline ============== SOFTWARE ENGINEERING Abstraction, Classification, Hierarchy UML - we really only studied class diagrams Java - you learned a lot this semester Classes, Objects, Interfaces Abstract Data Types (ADTs) Generics Inheritance and Polymorphism Unit Testing Good and Bad Design ALGORITHMS Analysis Big-O, Big-Omega, Big-Theta The major complexity classes and how they are related Effects of increasing input size Effects of a faster computer Recursion Inductive/Recursive definitions, proofs, functions, data types When to use / When not to use Memoization Recursive types lead to recursive functions (e.g. linked list nodes) Sorting (will be covered in CMSI 282) Properties of sorts (stable, in-place) Methodologies (comparison, merging, distribution...) Memorize at least insertion, quick, merge, and radix Time and space complexities Limitations of different algorithms DATA TYPES AND DATA STRUCTURES Data types generally specified with interfaces Two building blocks of data structures: Arrays and Links When to use arrays and when to use links How linked structures work -- PLEASE UNDERSTAND THIS!!!!!!!! COLLECTIONS The types of collections, in general The main collection interfaces and classes in Java Java Iterators Java Comparators and Comparables Why you need to make collection classes yourself CLASSIC DATA TYPES AND THEIR IMPLEMENTATIONS Stacks and Queues (using arrays and singly-linked lists) Lists (using doubly-linked nodes) Hierarchies Oriented Trees Terminology Various representations Breadth-first and depth-first traversal Ordered Trees k-ary Trees and some of their properties Binary Trees Sets and Dictionaries Basic array implementations (sorted and unsorted) Bitsets Search Trees Binary Search Trees Red-black Trees Splay Trees Tries Hashtables An exam from a couple years ago - ENJOY!! ========================================= 1. Give the time complexity (Theta notation is fine) of the following: for (int i = n; n >= 1; n /= 2) { for (int j = 0; j < i; j++) { System.out.print(i + j); } } ANSWER: Note that i never changes -- it's always equal to the initial value of n. So the inner loop executes log n times, each time printing n items. So T(n) = n * log(n) (which is clearly in Theta(n * log(n))) An interesting variation on the problem is if the outer loop were written as for (int i = n; i >= 1; i /= 2) In this case T(n) = n + (n/2) + (n/4) + (n/8) + ... = 2 * n, which is in Theta(n). ------------------------------------------------------------------------------ 2. Give the best and worst case complexity (Theta-notation is fine) of the following: // Assume some global variable t is defined out here x = 1; for (int i = 1; i <= n; i++) { if (new Date().getTime() > t) { for (int j = 1; j <= x; j++) { System.out.print(i + j); } } x += x; } ANSWER: In the best case, we never execute the inner loop. In the worst case we always do. Clearly B(n) = n, which is in Theta(n). W(n) = 1 + 2 + 4 + 8 + 16 + 32 + ... + 2^n = 2^(n+1) - 1 = 2(2^n) - 1 which is in Theta(2^n). ------------------------------------------------------------------------------ 3. An algorithm of with time complexity function T, where T(n) = 4 log n, can process a 64 element list in 72 seconds on our PC. (Logs are base 2, remember?) a) How long would it take to process a 512 element list? b) If we ran the algorithm on a machine that was 20 times faster than our PC, how large of a list could we process in one hour? ANSWER: a) T(64) = 4 log 64 ops = 24 ops T(64) = 72 secs So rate = 24 ops / 72 secs = (1/3) ops/sec T(512) = 4 log 512 ops = 36 ops 36 ops / (1/3 ops/sec) = 108 seconds b) Rate of faster computer = (20/3) ops/sec In 3600 seconds we do (20 / 3 * 3600) = 24000 ops T(n) = 4 log n = 24000 log n = 6000 n = 2^6000 A 2^6000 element list can be processed in an hour ------------------------------------------------------------------------------ 4. Draw a UML class diagram for basketball teams, their players (and their positions), their coaches, their schedules and their won-loss records. ANSWER: I don't wamt to do any character art for this, so here are some class fragments. It's not too hard to extend to UML. By the way, there are many acceptable answers to this problem. class Team { private Person[] players; private Person[] coaches; private Person headCoach; public Game[] getGames() {...} public Record getRecord(Date startDate, Date endDate) {...} // ... } class Person { private String name; // ... } class Game { private Date date; private Team team1; private int score1; private Team team2; private int score2; // ... } ------------------------------------------------------------------------------ 5. Write a code fragment to swap two adjacent nodes in the middle of a doubly-linked ring. Don't just swap the data elements from one node to another; instead redirect all the links this way and that to get the desired effect. ANSWER: The trick is to crete temporary variables for the nodes that will be messed with. Assume current is a reference to the first of the two nodes to be swapped. Then you write down the code with virtually no effort: Node* p = current.prev; Node* n = current.next; Node* after = n.next; p.next = n; n.next = current; current.next = after; after.prev = current; current.prev = next; next.prev = p; This fragment would be embedded in an "if size of list is bigger than 1" clause. ------------------------------------------------------------------------------ 6. a) Draw the binary tree with preorder A B C E F D and inorder A E F C D B or prove that no such tree exists. b) If the preorder and level order of a binary tree are the same, what does the tree look like? c) If the preorder and post order of a binary tree are the same, what does the tree look like? ANSWER: a) A \ B / C / \ E D \ F b) It looks like a chain of nodes starting at the root with at most two leaf nodes. c) A single node ------------------------------------------------------------------------------ 7. a) Draw the red-black tree that results from inserting in order, the integers 70, 90, 80, 60, 50, 75. 77. 65. b) Draw the tallest possible red-black tree with 17 nodes. ANSWER: a) 75(B) / \ 60(R) 80(R) / \ / \ 50(B) 70(B) 77(B) 90(B) / 65(R) b) B / \ R B / \ \ B B B / \ / \ R R B B / \ / \ B B B B / / \ R R R ------------------------------------------------------------------------------ 8. Write a utility method that interleaves two java.util.List objects. That is, interleave([A,Z,G,Q], [P, X, R]) would return a new list [A, P, Z, X, G, R, Q]. ANSWER: public static void interleave(List a, List b) { List result = new ArrayList(); Iterator i = a.iterator(); Iterator j = b.iterator(); while (i.hasNext() || j.hasNext()) { if (i.hasNext()) result.add(i.next()); if (j.hasNext()) result.add(j.next()); } return result; } ------------------------------------------------------------------------------ 9. Write an instance method for the BinaryTree class in Homework #4 to return a list of the data elements in the leaves of a binary tree. For example, in the binary tree A / \ B C / \ \ D E G / F your method needs to return the list [D, F, G] ANSWER: public List frontier() { if (isLeaf()) return Collections.singletonList(this); List result = new ArrayList(); if (left != null) result.addAll(left.frontier); if (right != null) result.addAll(right.frontier); return result; } ------------------------------------------------------------------------------ 10. Is every binary tree also an (a,b)-tree? If so, give the values for a and b. If not, disprove. ANSWER: The easiest way to disprove this is to note that in an (a,b)-tree all leaves have to be on the same level, and there exist binary trees for which this is not true.