0% found this document useful (0 votes)
156 views7 pages

Heap and Hashtable

A priority queue is a data structure that supports inserting objects and deleting the smallest object. It can be implemented using a min heap, which is a complete binary tree where the smallest element is at the root and child nodes are larger than their parents. A min heap is represented using an array where the root is at index 0 and children indices are calculated based on the parent index. The priority queue implementation uses a min heap to delete the smallest object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views7 pages

Heap and Hashtable

A priority queue is a data structure that supports inserting objects and deleting the smallest object. It can be implemented using a min heap, which is a complete binary tree where the smallest element is at the root and child nodes are larger than their parents. A min heap is represented using an array where the root is at index 0 and children indices are calculated based on the parent index. The priority queue implementation uses a min heap to delete the smallest object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lecture Notes on Data Structures using Java Page 1

Priority Queue (Heaps)

 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

 an almost complete binary tree with the following property


 the smallest element is at the root
 for every node x, the value of the parent of x is less than or equal to the value
of x (with the exception of the root)
 An almost complete binary tree can be numbered such that the root is assigned
number 1, a left child is assigned twice the number assigned to its parent and a
right child is assigned 1 more than twice the number assigned to its parent.
 Because of this property, a heap can be represented by an array. The root will
occupy position 0, and for any node x at position i, its left child is at position 2*i and
its right child is at position 2*i+1.

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.

MinHeap Implementation of the Priority Queue ADT

public class MinHeap implements PriorityQueue {

private Object items[];


private int count;

public MinHeap(int n) {
items = new Object[n];
count = 0;
}
Lecture Notes on Data Structures using Java Page 2

public void insert(Object item) {


int i=0;
count++;
Comparable c = (Comparable) item;
i = count;
while (i > 1) {
if (c.compareTo(items[i/2]) < 0) {
items[i] = items[i/2];
i = i / 2;
}
else
break;
}
items[i] = item;
}

public void deleteMin() {


Object min = items[1];
Object last = items[count--];
int i, child=0;
for (i=1; i*2 <= count; i = child) {
child = i * 2;
if (child != count) {
Comparable c = (Comparable) items[child+1];
if (c.compareTo(items[child]) < 0)
child++;
}
Comparable c = (Comparable) last;
if (c.compareTo(items[child]) > 0)
items[i] = items[child];
else
break;
}
items[i] = last;
}

public Object findMin() {


return count == 0? null : items[1];
}

public boolean isEmpty() {


return count == 0;
}

public void clear() {


for (int i=0; i< count; i++)
items[i] = null;
}
Lecture Notes on Data Structures using Java Page 3

public String toString() {


StringBuffer sb = new StringBuffer();
sb.append("[");
for (int i=1; i <= count; i++) {
sb.append(items[i]);
if (i < (count)) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
Lecture Notes on Data Structures using Java Page 4

HASHING
 implementation of the hash table ADT
 used for performing insertions, deletions and finds in constant average time.

Hash Table data structure


 array of some fixed size containing the keys
 typically a key is a string with some associated value
 let size denote the size of the hash table
 each key is mapped into some number in the range 0 to size-1 and placed in the
appropriate cell
 the mapping is called a hash function
 the hash function should be simple enough to compute and should ensure than
any two distinct keys get different cells.

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

key 10 will be stored in slot 3


key 7 will be stored in slot 0
key 14 will be stored in a lot 0 causing a collision.

Strategies for solving collisions


 use open hashing technique
 use a list to store all items that hash to the same value
 use closed hashing
 try alternate cells in the array during collision

The Hash Table Interface

public interface HashTable {

public boolean add(Object item);


public boolean remove(Object item);
public boolean contains(Object item);
public void clear();
}

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

Open Hashing Implementation of the Hash Table ADT

 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

Three common collision resolution strategies

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

hi(x) = (hash(x) + i) % size

i=0 ((14 % 7) + 0) % 7 = 0 okey, 14 is placed at slot 0


i=0 ((16 % 7) + 0) % 7 = 2 okey, 16 is placed at slot 2
i=0 ((21 % 7) + 0) % 7 = 0 collision
i=1 ((21 % 7) + 1) % 7 = 1 okey, 21 is placed at slot 1
i=0 (( 9 % 7) + 0) % 7 = 2 collision
i=1 (( 9 % 7)+ 1) % 7 = 3 okey, 9 is placed at slot 3
i=0 (( 7 % 7) + 0) % 7 = 0 collision
i=1 (( 7 % 7) + 1) % 7 = 1 collision
i=2 (( 7 % 7) + 2) % 7 = 2 collision
i=3 (( 7 % 7) + 3) % 7 = 3 collision
i=4 (( 7 % 7) + 4) % 7 = 4 okey, 7 is placed in slot 4
i=0 (( 2 % 7) + 0) % 7 = 2 collision
i=1 (( 2 % 7) + 1) % 7 = 3 collision
i=2 (( 2 % 7) + 2) % 7 = 4 collision
i=3 (( 2 % 7) + 3) % 7 = 5 okey, 2 is placed in slot 5

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

hi(x) = (hash(x) + i*i) % size

i=0 ((14 % 7) + 0) % 7 = 0 okey, 14 is placed at slot 0


i=0 ((16 % 7) + 0) % 7 = 2 okey, 16 is placed at slot 2
i=0 ((21 % 7) + 0) % 7 = 0 collision
i=1 ((21 % 7) + 1) % 7 = 1 okey, 21 is placed at slot 1
i=0 (( 9 % 7) + 0) % 7 = 2 collision
i=1 (( 9 % 7)+ 1) % 7 = 3 okey, 9 is placed at slot 3
i=0 (( 7 % 7) + 0) % 7 = 0 collision
i=1 (( 7 % 7) + 1) % 7 = 1 collision
i=2 (( 7 % 7) + 4) % 7 = 4 okey, 7 is placed in slot 4
i=0 (( 2 % 7) + 0) % 7 = 2 collision
i=1 (( 2 % 7) + 1) % 7 = 3 collision
i=2 (( 2 % 7) + 4) % 7 = 6 okey, 2 is placed in slot 6

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

You might also like