Hashing:

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

HashTable<K,V> & HashMap<K,V> class

java.util implements map<K,V> interface K-- type paramater for key and v-- type parameter for associated value Operations: lookup, insert, delete. Constructor lets you set init capacity and load factor handles collisions with chained buckets hash map only allows null for keys and values

The more items a table can hold, the () likely a collision will happen.

less

How do you look up a value within the hash table?

return Table[Hash(key)];

What kind of Collection is Hashing?

value-orientated.

Array Bucket

-- a bucket of arrays. -Fixed in size. -size of about 3 work usually well.

Probe Hashing:

-> Hash it, and if it leads to a collision, use a separate equation to determine the step size and use that step size to find a new site.

If we have UW-Madison student ID's, and we wanted the ideal hash functions, how would we do it, and why would there be a problem

-> We'd simply count each one as an index -> Hash table would be huge.

Collision Hashing using Buckets

-Each element can solre than one item. -throw collisions into a bucket. -buckets aren't sorted.

HashCode Method:

-method of OBJECT class -Returns an int -default hash code is BAD-- computed from Object's memory address. --> must override

Steps to resizing:

1. Double table size to nearest prime number 2. Re-hash items from old table into the new table.

Good Hash Function qualities:

1. Must be deterministic: -> Key must ALWAYS generate the same Hash Index (excluding rehashing). 2. Must achieve uniformity -> Keys should be distributed evenly across hash table. 3. FAST/EASY to compute -> only use parts of the key that DISTINGUISH THE ITEMS FROM EACH OTHER 4. Minimize collisions:

How does Hashing work?

1. you have a key for the item. 2. the item's key gets churned within the hash function to form the Hash index. 3. The hash index can be applied to the data array, and so, the specific data is found.

Hash Function

A function that takes in the key to compute a specific Hash Index.

hash Table:

An array that stores a collection of items.

Load Factor

Approximately how it's full... 0.7-0.8.

Extraction:

Breaking keys into parts and using the parts that uniquely identify with the item. 379452 = 394 121267 = 112

Folding:

Combining parts of the key using operations like + and bitwise operations such as exclusive or. Key: 123456789 123 456 789 --- 1368 ( 1 is discarded)

What is the goal of Hashing?

Do faster than O(LogN) time complexity for: lookup, insert, and remove operations. To achieve O(1)

What would the Perfect Hash Function be?

Each Key maps to an unique Hash Index.

HashMap underlying structure:

HashTable with chained buckets

Collesion handeling:

How you handle the collisions so each element in the hittable stores only one item.

Idea of probing:

If you have a collision, search somewhere else on the table.

Key

Information in items that is used to determine where the item goes into the table.

Load Factor (LF)

Number of items/Table size. For instance, a load factor of 1 = 100% of the items are used.

HashMap complexity of basic operations:

O(1)

What is the worst case time complexity for: Insert, lookup, and delete, for hash functions?

O(1)

TreeMap complexity for iterating over associated values:

O(N)

Rehashing Complexity:

O(N)-- costly. Carefully select initial TS to avoid re-hashing.

Complexity for iterating over associated values:

O(T.S + N) --> worst case.

Treemap complexity of basic operations:

O(logN)

Quadratic Probing:

Probe Sequence is (Hk+1)^2. Minimizes clustering better at distinguishing items across table.

Linear Probing:

Step size is 1. Find the index, and keep incrementing by one until you find a free space.

How do you insert a value within the hash table?

Table[Hash(key)]=data;

Table Size(TS)

The Array's Length

Why do we use prime numbers for table size?

We mod often, and prime numbers give us the most unique numbers. (2*ts+1)

Collisions:

When the Hash Function returns the same index for different keys.

How do you delete a value within the hash table?

You just set Table[hash(Key)] = null

Weighting:

Emphasizing some parts of the key over another.

Compressing:

Ensuring the hash code is a valid index for the table size.

Tree Buckets

+--WC = O(logN) +--no wasting space +--dynamically sized -- more complicated than what's needed. --> insert with dups= O(1) --> W/o dups = O(N)

Chained bucket:

+--easy to implement +-- buckets can't overfill +-- buckets won't waste time. +-- buckets are dynamically sized.

TreeMap underlying Structure:

RBT


Ensembles d'études connexes

Radical Reconstruction, Presidential Reconstruction, Andrew johnson, knights of labor

View Set

Forensic Minds Exam 4 - Ch. 11 Terrorism

View Set

Chapter 8: Conducting Causal Research

View Set

Folders and Files - vocabulary & techniques

View Set

Muscle Excitation and Relaxation

View Set