Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
-
Upload
silas-roland-warner -
Category
Documents
-
view
214 -
download
0
Transcript of Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Department of Computer Science 3
So far Arrays Arraylists LinkedList
Stacks Queues
Items are all placed at a position in the data structure LinkedList has an iterator with an “add” method
Department of Computer Science 4
Sets A set is a collection of items that does not permit
duplicates Adding an existing item has no effect Removing an item not in the set has no effect
Java has a Set interface with two implementations HashSet (uses hashing to store items) TreeSet (uses a tree to store items – Sorted)
Department of Computer Science 5
Exampleimport java.util.HashSet;import java.util.Iterator;import java.util.Set;
public class SetTest {public static void main(String[] args){
Set<String> names = new HashSet<String> ();
names.add(“Harry”); // add lots more
Department of Computer Science 6
Continued
names.add("Jane");
names.add("Tom");
names.add("Richard");
names.add("Tom");
names.add("Barbara");
print(names);
}
Department of Computer Science 7
Printingprivate static void print(Set<String> s){
System.out.print("{ ");
for (String x : s){
System.out.print(x);System.out.print(" ");
}
System.out.println("}");}
Department of Computer Science 8
TreeSet
Changing only two lines of code lets us have a Tree behind the scenes
Note the order of items!
Department of Computer Science 9
Maps Similar idea can be developed to make
associations Two sets are kept in association Set of keys -> Set of Values Two keys may refer to same value Names and phone numbers Java has a Map Interface Two maps are implemented
HashMap TreeMap
Department of Computer Science 10
Test program for maps
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
public class maps {
public static void main(String[] args) {
Department of Computer Science 11
Continued
Map<String, String> yellowpages = new HashMap<String, String> ();
yellowpages.put("Harry", "123456");
yellowpages.put("Jane", "012345");
yellowpages.put("Tom", "123458");
yellowpages.put("Richard", "123457");
yellowpages.put("Barbara", "123454");
yellowpages.put("Barbara", "987654"); // assignment
Department of Computer Science 12
Printing out maps
Set<String> keySet = yellowpages.keySet();for (String key : keySet){String value = yellowpages.get(key); // value assoc with keySystem.out.println(key + "->" + value);}}
}
Department of Computer Science 13
What is hashing? Technique for finding elements without making a
linear search through all elements Uses a hash function is a function that computes
an integer value (called a hash code) from an object (different objects should have different hash codes)
Object class has a hashCode method int h = x.hashCode();
Department of Computer Science 14
Hash functions Should avoid collisions (two or more different
objects with the same hash code) If you have your own objects you write:
public int hashCode( ) When adding x.equals(y) => x.hashCode() ==
y.hashCode (avoid duplicates) Eg for a circle so that circles of different radii are
stored separately. Forgetting to implement hashCode means the one
assoc with Object is used – not a good idea.
Department of Computer Science 15
Example Consider
Set<Circle> circles = new HashSet<Circle>();
circles.add(new Circle(5));
if (circles.contains(new Circle(5))
System.out.println(“Circle of radius 5 exists”);
Department of Computer Science 16
Binary Search Trees Finding things is O(log N) with binary search With arrays insertion and deletion are O(N) Binary search trees are fast at everything Nodes of a binary search tree have two children Data values of all descendants to the left of any
node are less than the data value stored at that node (similarly right and greater)
Department of Computer Science 17
Tree of names
For the list {Adam, Eve, Harry, Jane, Richard, Tom}
Jane (root) – children Eve and Richard Eve – children Adam and Harry Richard – children Tom and null Adam, Harry and Tom are leaf nodes (both
children null)
Department of Computer Science 18
Implementation overviewpublic class BinarySearchTree {// need reference to root (like first in LL)private node root;// inner class for nodeprivate class Node { public void addNode(Node newNode) { } public Comparable data; public Node left; public Node right;}}
Department of Computer Science 19
Tree Operations Tree Traversal (sorted order)
Print left subtree Print data Print right subtree
Called Inorder traversal Preorder traversal (root, left, right) Postorder traversal (left, right, root) (3+4*5)
Department of Computer Science 20
Trees and Recursion// Note this is a method of the Node inner classpublic void printNodes( ){
if (left != null) left.printNodes( );System.out.println(data);if (right != null) right.printNodes( );
}
// Method of BinarySearchTreepublic void print( ){
if (root != null) root.printNodes( );}
Department of Computer Science 21
HashSet or TreeSet
If you have a good hash function for your objects then performance will be better than trees
Hash sets are at the mercy of the hash function Iterators visit TreeSet data in sorted order (hash
order is random) TreeSet requires objects to implement
Comparable (or Comparator)
Department of Computer Science 22
How to choose – ask questions How are elements accessed?
Doesn’t matter By key (eg a/c #) (MAP) Integer index (ARRAYLIST)
Does data order matter? Doesn’t matter Must be sorted (TREESET) Store in insert order (LL,
ARRAYLIST)