- learned Java! - look at: http://www.cs.middlebury.edu/~dkauchak/classes/cs201/problems/set1 - 33 lectures - over 3602 lines of notes - assuming 34 lines per page that's 106 pages of notes - over 3375 lines of Java code looked at in examples - 119 classes - over 3000 lines of java Code written! - - extendable arrays - linked lists (many different types) - Material covered: - general coding - commenting - JavaDoc - coding style - what is meant by: - public/private - static - final - super - inheritance interfaces - equals vs. == - OO programming - design and functionality vs. implementation - method signature - generics - general concepts - binary search - complexity analysis - sorting - mergesort - merge method - quicksort - partition method - treesort - data structures - Know - how they're represented - how they're used - what they're good at/bad at - different implementations - run-times - arrays and extendable arrays (ArrayLists) - linked lists - singly linked - circularly linked - doubly linked - linear structures - stacks - queues - binary trees - tree terminology - tree traversal - bfs - dfs - binary search trees - heaps and priority queues - hash tables - hash functions - hashCode() - collision resolution by chaining - open addressing - maps - graphs - terminology - representation - adjacency list - adjacency matrix - trade-offs - algorithms - dfs - bfs - cycles The questions below are some sample problems to help you prepare for the final. While they are representative of the types of questions that will be answered they are not necessarily comprehensive in covering all of the material that could be covered (for example, there are no graph questions below). T/F: - A binary tree with n nodes has at most height O(log_2 n) False. A twig has height O(n) - searching for a value in balanced binary tree is O(log_2 n) False. Binary trees don't have any order (binary *search* trees to impose an ordering), so searching is O(n). - balanced binary trees make sure that the height of the tree is O(log_2 n). True. - If we have an undirected connected graph with three nodes it has to have a cycle. False. Consider the graph A<->B<->C - searching for a value in a binary search tree is O(log_2 n) False. Binary search trees do not impose any height restriction, e.g. insert 1, 2, 3, ... n, into a binary search tree. Search is O(n) without imposing a balance. - We can insert no more than n things into a hashtable with n entries without resizing. Fasle. If we use collision resolution by chaining we can overfill the hashtable. - Assume we have some method that has checked that two binary trees have the same tree structure. Write a method called equalValues that takes two binary trees that are structurally the same and see if they contain the same values, i.e. are identical. You may assume the standard binary tree methods are available. public static boolean equalValues(BinaryTree t, BinaryTree t2) // solution public static boolean equalValues(BinaryTree t1, BinaryTree t2){ if( t1 == null ) return true; }else if( t1.data.equals(t2.data) ){ return equalValues(t1.left(), t2.left()) && equalValues(t1.right(), t2.right()); }else{ return false; } } - write a method for a binary search tree that takes a value and an ArrayList and populates the ArrayList with all data items in the tree that are less than the value passed in. Assume that the data item stored in the tree is a string and that the strings are unique. // SOLUTION // Simple solution (most credit, but not as efficient as could be) public findLessThan(ArrayList list, String value){ if( data.compareTo(value) < 0 ){ list.add(data); } if( left != null ){ left.findLessThan(list, value); } if( right != null ){ right.findLessThan(list, value); } } // Faster solution public findLessThan(ArrayList list, String value){ if( data.compareTo(value) < 0 ){ list.add(data); if( left != null ){ left.findLessThan(list, value); } if( right != null ){ right.findLessThan(list, value); } } else { if( left != null ){ left.findLessThan(list, value); } } } - Write a method for the LinkedLists class (implemented with both a head and tail reference) that takes as input another linked list and adds all of the values of the input linked list to the current one. // SOLUTION public void merge(LinkedList other){ tail.setNext(other.head); if( other.tail != null ){ tail = other.tail; } } Note this solution isn't ideal since the latter part of this linked list will be shared between the two linked lists. A better solution would be to copy them over, e.g. public void merge(LinkedList other){ Node current = other.head; while( current != null ){ tail.setNext(new Node(current.data())); tail = tail.next(); current = current.next(); } } On an exam I would specify if the earlier solution would be acceptable. - Assuming that BinaryTree t is a full binary search tree and that t has a right child, what does the method below do? public static String mystery(BinaryTree t){ BinaryTree temp = t.right(); while( temp.left() != null ){ temp = temp.left(); } return temp.data(); } //SOLUTION Finds the successor, i.e. the left-most node in the right subtree. - Write a static method that takes two arrays of ints as a parameter and returns a new array that has the contents of the input arrays concatenated. // SOLUTION public static int[] concat(int[] a, int[] b){ int[] returnMe = new int[a.length+b.length]; // copy over a for( int i = 0; i < a.length; i++ ){ returnMe[i] = a[i]; } // copy over b for( int i = 0; i < b.length; i++ ){ // have to offset for the a things already copied over returnMe[a.length + i] = b[i]; } return returnMe; }