Download presentation
Presentation is loading. Please wait.
Published byPreston Merritt Modified over 9 years ago
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. – Three implementations of sets: ordered lists (11.2), Binary search trees (11.3) and Hash tables (11.4) ● 11.1:Set interface ● 11.5:Java collections framework, discussing Java's own Set interface.
3
The Set Interface
7
11.1 The Set Interface
8
The Set Interface
14
● 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.
15
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 (); ● to – Words = new BinarySearchTree (); ● Takes unacceptable amount of time.
16
11.2 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.
17
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. – But the add() method from the Set interface may add target at any position, but not if it is already present.
18
Ordered Lists
19
● We should extend a class only when an instance of the subclass works in place of an instance of the superclass.
20
Ordered Lists
22
● The contains() method for the OrderedList class is a linear search (sequential search).
23
Ordered Lists ● To add something to a Set, we must first find where it belongs, then (if it is not present) put it there.
24
Ordered Lists
25
● To remove something, we must first find where it belongs, then (if it is present) remove it.
26
● Since the final “put it there” and “remove it” operations take constant time, all three methods (search, insertion and deletion) have the same order of running time Θ (n) for a given implementation. ● The OrderedList data structure is easy to implement, but it requires linear time for search, insertion, and deletion. Ordered Lists
27
11.3 Binary Search Trees ● Binary search tree – More efficient under some circumstances.
28
Binary Search Trees
29
● Search – Successful search for node 16. – Shaded nodes are never examined
30
Binary Search Trees ● Searching a binary search tree. – In a perfect tree, time complexity 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, Θ (n).
31
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 (e.g. node 16) of the new leaf, we'll need this information. – We need to deal with the situation in which the binary search tree is empty. ● Insertion (node 15) ● The search fails when 16 has no child, so we add a new leaf there.
32
Binary Search Trees Fig. 11-20 (p300) Both the BinaryNode and BinarySearchTree classes must be modified to implement the Parent interface.
33
Binary Search Trees Fig. 11-21 (p300) The getChild() and setChild() methods for the BinaryNode class.
34
Binary Search Trees Fig. 11-22 (p301) The getChild() and setChild() methods for the BinarySearchTree class. Since there is only one child (the root), the argument direction is ignored.
35
Binary Search Trees
36
● 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.
37
Binary Search Trees ● Deletion (to delete node 2): The deleted node’s child 8 becomes a child of the deleted node’s parent 13.
38
Binary Search Trees ● When the node we want to delete has two children (see Figure 11-25 on page 302). – 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.
39
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.
40
Binary Search Trees
42
● 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); – Because node.getRight() returns null.
43
Binary Search Trees ● BinarySearchTrees should not be used in the plain form explained here. – The worst-case, running time is linear – Worst case are not uncommon.
44
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.
45
Hash Table ● LetterCollection containing 1 'a', 3 'd's, and 1 'y'.
46
Hash Table
51
● 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.
52
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, where x is the integer input and f(x) is the array index.
53
Hash Table (p308) ● f(x) = x mod 10 Fig. 11-31: In a hash table, a hash function maps each potential element to an array index. The shaded positions do not contain set elements; in practice, some invalid value such as -1 or null is stored there.
54
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.
55
Hash Table ● hashCode() method must return the same value for two objects which are equals() – That is if a.equals(b), then a.hashCode() == b.hashCode(). – But if a.hashCode() == b.hashCode(), it does not follow that a.equals(b) – Sometimes two different, non-identical objects have the same hash code. – 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.
56
Hash Table ● The hash function might occasionally map two different hash codes to the same index. ● For example, 37 mod 10 = 87 mod 10. This is called a collision. – We try to choose a hash function which makes collisions rare. – No matter how good our hash 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.
57
Hash Table (collision resolution) 1. Chaining – To keep a sequence of ListNodes (effectively an ordered list) at each position in the table.
58
Hash Table (collision resolution) ● 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 (e.g. 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) (constant time).
59
Hash Table (collision resolution) 2. Open addressing – Collision resolution techniques that avoid chains and store all of the elements directly in the array. – 2.1 Linear probing ● If there is a collision during insertion, we simply move on to the next unoccupied position.
60
Hash Table ● Three problems with linear probing: – 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. – 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.
61
Hash Tables ● 2.2 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 if 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. – See Fig. 11-35 (p311): Double hashing ● f(x) = x mod 10 ● g(x) = floor(x / 100) //floor function ● Examples: to insert 256 and 386 into the hash table.
62
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
63
Hash Tables
65
● If this position is occupied by something other than target, we use hash2() to decide how many positions to skip ahead.
66
Hash Tables
67
● If the table is at least half full (including deleteds), we rehash into a larger table before inserting.
68
Hash Tables
70
● 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.
71
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.
72
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.
73
Java Collections Framework Again
74
● 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().
75
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.
76
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.
77
Java Collections Framework Again
78
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.
79
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.
80
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.
81
Summary
82
Chapter 11 Self-Study Homework ● Pages: 296-318 ● Exercises: 11.5, 11.7, 11.8, 11.9, 11.17, 11.22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.