Download presentation
Presentation is loading. Please wait.
Published bySilas Roland Warner Modified over 9 years ago
1
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2
2
Department of Computer Science2 Lectures - Today - Collections Continued - Sets - Trees - Hashing
3
Department of Computer Science3 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
4
Department of Computer Science4 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)
5
Department of Computer Science5 Example import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetTest { public static void main(String[] args) { Set names = new HashSet (); names.add(“Harry”); // add lots more
6
Department of Computer Science6 Continued names.add("Jane"); names.add("Tom"); names.add("Richard"); names.add("Tom"); names.add("Barbara"); print(names); }
7
Department of Computer Science7 Printing private static void print(Set s) { System.out.print("{ "); for (String x : s) { System.out.print(x); System.out.print(" "); } System.out.println("}"); }
8
Department of Computer Science8 TreeSet Changing only two lines of code lets us have a Tree behind the scenes Note the order of items!
9
Department of Computer Science9 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
10
Department of Computer Science10 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) {
11
Department of Computer Science11 Continued Map yellowpages = new HashMap (); 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
12
Department of Computer Science12 Printing out maps Set keySet = yellowpages.keySet(); for (String key : keySet) { String value = yellowpages.get(key); // value assoc with key System.out.println(key + "->" + value); }
13
Department of Computer Science13 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();
14
Department of Computer Science14 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.
15
Department of Computer Science15 Example Consider Set circles = new HashSet (); circles.add(new Circle(5)); if (circles.contains(new Circle(5)) System.out.println(“Circle of radius 5 exists”);
16
Department of Computer Science16 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)
17
Department of Computer Science17 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)
18
Department of Computer Science18 Implementation overview public class BinarySearchTree { // need reference to root (like first in LL) private node root; // inner class for node private class Node { public void addNode(Node newNode) { } public Comparable data; public Node left; public Node right; }
19
Department of Computer Science19 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)
20
Department of Computer Science20 Trees and Recursion // Note this is a method of the Node inner class public void printNodes( ) { if (left != null) left.printNodes( ); System.out.println(data); if (right != null) right.printNodes( ); } // Method of BinarySearchTree public void print( ) { if (root != null) root.printNodes( ); }
21
Department of Computer Science21 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)
22
Department of Computer Science22 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)
23
Department of Computer Science23 More questions What operations need to be fast? Doesn’t matter (small dataset) Add/remove(LL) Search(SET) (DM = ARRAYLIST) Hash or Tree? Strings should be hashed Own class need to check hashCode and equals Trees may need a Comparator
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.