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

Post on 18-Jan-2018

245 views 1 download

description

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

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

Chapter 11

Sets

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

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

Overview

● 11.4– Hash tables

● 11.5– Java collections framework, discussing Java's own

Set interface.

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

The Set Interface

● 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.

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<String>();

● Takes unacceptable amount of time.

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.

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.

Ordered Lists

Ordered Lists

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

Ordered Lists

Ordered Lists

Ordered Lists

Ordered Lists

● 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.

Ordered Lists

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

Ordered Lists

Ordered Lists

Ordered Lists

Ordered Lists

Ordered Lists

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

Binary Search Trees

● Binary search tree– More efficient under some circumstances.

Binary Search Trees

Binary Search Trees

Binary Search Trees

● Search

Binary Search Trees

Binary Search Trees

● 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.

Binary Search Trees

● Insertion

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.

Binary Search Trees

Binary Search Trees

Binary Search Trees

Binary Search Trees

Binary Search Trees

● 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.

Binary Search Trees

Binary Search Trees

● 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.

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.

Binary Search Trees

Binary Search Trees

Binary Search Trees

● 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)

Binary Search Trees

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.

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.

Hash Table

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

Hash Table

Hash Table

Hash Table

Hash Table

Hash Table

Hash Table

● 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.

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

Hash Table

● f(x) = x mod 10

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.

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.

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.

Hash Table

● Chaining– To keep a sequence of ListNodes (effectively an

ordered list) at each position in the table.

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).

Hash Table

● Open addressing– Collision resolution techniques that avoid chaines

and store all of the elements directly in the array.

Hash Table

● Linear probing– If there is a collision during insertion, we simply

move on to the next unoccupied position.

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.

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.

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.

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

Hash Tables

Hash Tables

Hash Tables

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

Hash Tables

Hash Tables

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

Hash Tables

Hash Tables

Hash Tables

● 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.

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.

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.

Java Collections Framework Again

Java Collections Framework Again

● 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().

Java Collections Framework Again

● Map<String, Interger> numbers = new TreeMap<String, Integer>();● numbers.put(“five”, 5);● numbers.put(“twelve”, 12);● numbers.put(“a dozen”, 12);

● numbers.get(“twelve”) returns the Integer 12.

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.

Java Collections Framework Again

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.

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.

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.

Summary