Heap and Hashtable
Heap and Hashtable
A priority queue is collection of items that supports at least the following operations:
insert(Object item)
deleteSmallest()
A priority queue can be implemented using a minheap
MinHeap
Example:
2
8 4
15 20 13 17
23 25
The same as a binary search tree, we will be using the Comparable interface since we will
be comparing the elements.
public MinHeap(int n) {
items = new Object[n];
count = 0;
}
Lecture Notes on Data Structures using Java Page 2
HASHING
implementation of the hash table ADT
used for performing insertions, deletions and finds in constant average time.
Problems:
choosing a function most of the time hash(key) = key % size
what to do when two keys hash to the same value collision
how to choose the correct table size (we normally choose a prime number.
Example: Suppose that size = 7 and our hash function is hash(key) = key % size
Before we go into the actual implementation of our hash table, we have to understand first how
to go about getting the values that will serve as the key that will be used by the hash function.
Well, since our hash table will contain Objects, we have no idea what kind of objects will be
stored in our table. Our assumption in this case is that the class definition of the objects has
overridden the hashCode() method of the Object class. The hashCode() method returns an
integer value that is supposed to represent the state of the object. We assume that the
hashCode() will give us distinct integers for distinct objects.
Lecture Notes on Data Structures using Java Page 5
in the implementation of open hashing, keys that map to the same slot are stored in
a linked-list.
To illustrate:
Suppose that the hash table size is 7 and we use the function hash(key) = key %
size.
Suppose that we want to insert the following values in sequence
14 16 21 9 12 10 7 24
14 21 7
0
16 9
2
10 24
3
4
12
5
6
Lecture Notes on Data Structures using Java Page 6
Closed Hashing
if a collision occurs, alternate cells are tried until an empty cell is found
formally, cells h0(x), h1(x), h2(x) are tried where
hi(x) = (hash(x) + f(i)) % size with f(0) = 0
Linear probing
f is a linear function of i typically f(i) = i
this amounts to trying cells sequentially with wrap-around in search of an empty
cell.
suffers from primary clustering
To illustrate:
Suppose that the hash table size is 7 and we use the function hash(key) = key %
size.
Suppose that we want to insert the following values in sequence
Suppose further that we want to use linear probing
14 16 21 9 7 13
Slot# 14 16 21 9 7 2
0 14 14 14 14 14 14
1 21 21 21 21
2 16 16 16 16 16
3 9 9 9
4 4 4
5 2
6
Lecture Notes on Data Structures using Java Page 7
Quadratic probing
f is a quadratic function of i typically f(i) = i2
suffers from secondary clustering
To illustrate:
Suppose that the hash table size is 7 and we use the function hash(key) = key %
size.
Suppose that we want to insert the following values in sequence
Suppose further that we want to use linear probing
14 16 21 9 7 2
Slot# 14 16 21 9 7 2
0 14 14 14 14 14 14
1 21 21 21 21
2 16 16 16 16 16
3 9 9 9
4 7 7
5
6 2