Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

1 Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

2 Overview ● Set – A collection of elements in which the same element does not appear more than once. ● 11.1 – Set interface ● 11.2 – Three implementations: ordered lists ● 11.3 – Binary search trees

3 Overview ● 11.4 – Hash tables ● 11.5 – Java collections framework, discussing Java's own Set interface.

4 The Set Interface

5

6

7

8

9

10

11

12

13

14

15 ● A good list of words can be found in the file /usr/share/dict/words on most Unix systems, including Mac OS X ● Efficient Set implementation is important.

16 The Set Interface ● Given a large words.txt file with tens or hundreds of thousands of words, the program as written takes at most a few seconds to start up. ● If we change – Words = new OrderedList<String(); ● to – Words = new BinarySearchTree (); ● Takes unacceptable amount of time.

17 Ordered Lists ● Since a Set changes size as items are added and removed, a linked structure seems in order. ● An OrderedList is like a LinkedList, except that: – The elements of an OrderedList must implement the Comparable interfaces – The elements of an OrderedList are kept in order. – The OrderedList class implements the Set interface. It provides the methods add(), contains(), remove(), and size(). No duplicate elements are allowed.

18 Ordered Lists ● Extending LinkedList – The problem is that the LinkedList class implements the List interface, which conflicts with the Set interface. – The add() method from the List interface should add the argument target to the end of the list, even if it is already present, while the add() method from the Set interface may add target at any position, but not if it is already present.

19 Ordered Lists

20 ● We should extend a class only when an instance of the subclass works in place of an instance of the superclass.

21 Ordered Lists

22

23

24 ● To add something to a Set, we must first find where it belongs, then (if it is not present) put it there. ● To remove something, we must first find where it belongs, then (if it is present) remove it. ● Since the final “put it there” and “remove it” operations take constant time, all three methods have the same order of running time for a given implementation.

25 Ordered Lists ● The contains() method for the OrderedList class is a linear search.

26 Ordered Lists

27

28

29

30 ● The OrderedList data structure is easy to implement, but it requires linear time for search, insertion, and deletion.

31 Binary Search Trees ● Binary search tree – More efficient under some circumstances.

32 Binary Search Trees

33

34 ● Search

35 Binary Search Trees

36 ● Searching a binary search tree. – In a perfect tree, this is Θ (log n) – In the Anagrams program when the word file is in alphabetical order, it produces a worst case. Every new node is a right child.

37 Binary Search Trees ● Insertion

38 Binary Search Trees ● There are two complications to the code: – Once we reach a null node, we have forgotten how we got there. Since we need to modify either the left or right field in the parent of the new leaf, we'll need this information. – We need to deal with the situation in which the binary search tree is empty.

39 Binary Search Trees

40

41

42

43 ● Deletion – The challenge is to make sure the tree is still a binary search tree when we're done with the deletion. – Deleting a leaf, this is easy. – If the node has only one child, we just splice it out much as we would a node in a linked list.

44 Binary Search Trees

45 ● When the node we want to delete has two children. – We must be very careful about which node we choose to delete so that the tree will still be a binary search tree. – It is always safe to choose the inorder successor of the node we originally wanted to delete. – Find a node's inorder successor by going to the right child, then going left until we hit a node with no left child. – It can have a right child.

46 Binary Search Trees ● It is safe to replace the node we want to delete with its inorder successor. – It is therefore larger than anything in the left subtree and smaller than anything else in the right subtree.

47 Binary Search Trees

48

49 ● We don't need special code for the case where node is a leaf, because in this situation parent.setChild(direction, node.getRight()); ● Is equivalent to: – parent.setChild(direction, null)

50 Binary Search Trees

51 ● BinarySearchTrees should not be used in the plain form explained here. – The worst-case, running time is linear – Worst case are not uncommon.

52 Hash Table ● LetterCollection – a collection of letters – Since there are only 26 different letters a data structure as complicated as an ordered list or binary search tree is not required. ● It is also handy to keep track of the total number of letters in the collection.

53 Hash Table ● LetterCollection containing 1 'a', 3 'd's, and 1 'y'.

54 Hash Table

55

56

57

58

59 ● A set can be represented by an array of booleans. – This approach is called direct addressing. ● When we want to look up some element, we go directly to the appropriate place in the array. ● Direct addressing is incredibly fast.

60 Hash Table ● Direct addressing cannot be used for every set. – The set of possible elements might be vastly larger than the set of elements actually stored. – A direct addressing table would be a huge wast of space. ● Hash function – Takes an int as input and returns an array index. – f(x) = x mod 10

61 Hash Table ● f(x) = x mod 10

62 Hash Table ● Hash table – Have all the advantages of direct addressing, even though it works when the number of possible elements is much larger than the number of elements actually stored. – Most of the built-in classes have a method hashCode() which takes no arguments and returns an int which can be passes to a hash function. ● This int is called the hash code.

63 Hash Table ● hashCode() method must return the same value for two objects which are equals() – If a.hashCode() == b.hashCode(), it does not follow that a.equals(b) – When storing our own classes in a hash table, we must define both equals() and hashCode(). ● Hash table set methods use hashCode() to find the right position in the table, then use equals() to verify that the item at the position is the one we're looking for.

64 Hash Table ● The hash function might occasionally map two different hash codes to the same index. – We try to choose a hash function which makes collisions rare. – No matter how good our has function is, collisions cannot be completely avoided. – Since there are more potential elements than positions in the table, some elements must hash to the same location.

65 Hash Table ● Chaining – To keep a sequence of ListNodes (effectively an ordered list) at each position in the table.

66 Hash Table ● To search, insert, or delete, we simply use the hash function to find the right list and then perform the appropriate ordered list operation there. ● If we know in advance how many elements the Set contains (n), we can choose the array length to be proportional to this. – This limits the average number of elements per list to a constant. ● Average time for search, insertion, and deletion is Θ (1).

67 Hash Table ● Open addressing – Collision resolution techniques that avoid chaines and store all of the elements directly in the array.

68 Hash Table ● Linear probing – If there is a collision during insertion, we simply move on to the next unoccupied position.

69 Hash Table ● Tables can fill up – Fails catastrophically when there's no more room. ● Solve this problem by making the new table larger and rehashing when the table gets too full.

70 Hash Tables ● We can't simply remove an item to delete it. – This affects future searches – By replacing a deleted item with a special value. ● Neither null nor equals() to any target, so searches continue past it. – The table may become full of deleted values. ● Solve by occasionally rehashing. ● Clusters tend to occur and grow. – Insertion into any position in a cluster expands the cluster.

71 Hash Tables ● Double hashing – We use two hash functions ● The first function tells us where to look first, while the second one tells us how many positions to skip each time we find an occupied one. ● Two elements which originally has to the same position are unlikely to follow the same sequence of position through the table.

72 Hash Tables – Reduce the risk of clustering – Linear probing is a special case of double hashing where the second has function always returns 1. – The size of the table and the number returned by the second hash function should be relatively prime. ● Have no factor in common other than 1. ● Easiest way to ensure relative primality is to make the table size a power of two, require that the second hash function always returns an odd number

73 Hash Tables

74

75 ● If this position is occupied by something other than target, we use hash2() to decide how many positions to skip ahead.

76 Hash Tables

77 ● If the table is at least half full (including deleteds), we rehash into a larger table before inserting.

78 Hash Tables

79

80 ● There are only two drawbacks to hash tables: – Traversal is not efficient. ● Visiting all of the elements requires a search through the entire table, which is presumably mostly empty. – This takes time proportional to the capacity of the table, not to the number of elements. – With open addressing, deletion is a bit awkward ● We have to leave behind a special value deleted and occasionally rehash. – A hash table may not be the best Set implementation to use in an application where many deletions are expected.

81 Java Collections Framework Again ● Java collections framework – In the java.util package, the interface Collection is extended by an interface Set, which is similar to the one we defined in Section 11.1. – The same element may appear more than once in a collection, but not in a set. – The Set interface is extended by SortedSet.

82 Java Collections Framework Again ● HashSet and TreeSet – HashSet ● Is very much like the HashTable we defined in Section 11.4 – TreeSet ● Is similar to the BinarySearchTree, but it uses some advanced techniques.

83 Java Collections Framework Again

84 ● Maps – Each of a set of keys is associated with a value. – Each key may appear only once in a Map, although it is possible for several keys to be associated with the same value. – The Map interface requires the methods put() and get().

85 Java Collections Framework Again ● Map numbers = new TreeMap (); ● numbers.put(“five”, 5); ● numbers.put(“twelve”, 12); ● numbers.put(“a dozen”, 12); ● numbers.get(“twelve”) returns the Integer 12.

86 Java Collections Framework Again ● Data structures for Maps are the same as those used for Sets, with a small amount of extra work needed to keep track of the values in addition to the keys.

87 Java Collections Framework Again

88 Summary ● Set interface – OrderedList ● An ordered list is a linked list in which the elements are in order. – It should be be used only for very small sets. – BinarySerachTree ● A binary tree where all of the elements in the left subtree are less than the root and all the elements in the right subtree are greater than the root. – Average performance is good, it performs very poorly when the data are already sorted—a common situation.

89 Summary – HashTable ● Stores elements in an array, using a hash function to determine where in the array to place each element. – Since there are more potential elements than positions in the array, collisions can occur. – Three approaches to collision resolution are: ● Chaining ● Linear probing ● Double hashing – Hash tables have extremely good performance on average.

90 Summary ● The java collections framework defines a Set interface, implemented by a TreeSet and HashSet. ● Map interface for associating keys with values, implemented by TreeMap and HashMap.

91 Summary


Download ppt "Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."

Similar presentations


Ads by Google