13 Sequential Hash Tables

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Guidelines for the method hashCode

- If a class overrides the method equals, it should override hashCode. - If the method equals considers two objects equal, hashCode must return the same value for both objects. - If you call an object's hashCode more than once during the execution of a program, and if the object's data remains the same during this time, hashCode must return the same hash code. - An object's hash code during one execution of a program can differ from its hash code during another execution of the same program.

How can we ensure the correct value is retrieved when using Open Addressing with Linear Probing?

A successful search for an entry that corresponds to a given search key follows the same probe sequence used to add the entry to the hash table.

A typical hash function will perform two steps:

Convert the search key to an integer called the hash code. Compress the hash code into the range of indices for the hash table.

An array that utilizes special indexes potentially derived from a calculation performed on the value to be referenced

Hash table

A technique that determines the array index using only an entry's search key, without searching

Hashing

A way of evaluating a polynomial to produce an algorithm optimized for computation

Horner's method

When compressing a hash table index using "c % n" why should "n" be an odd number? c = hash, n = size of index

If n is even, c % n has the same parity as c—that is, if c is even, c % n is even; if c is odd, c % n is odd. Having "n" odd will eliminate this bias.

Why should the size of a hash table be a prime number n greater than 2?

If you compress a positive hash code c into an index for the table by using c % n, the indices will be distributed uniformly between 0 and n - 1

A good hash function should

Minimize collisions Be fast to compute

Why is "c % n" ideal for compressing the index of a hash table? c = hash, n = size of index

The c%n remainder lies between 0 and n - 1. * If c is negative, c % n lies between 1 −n and 0. A zero result is fine, but if c % n is negative, add n to it so that it lies between 1 and n - 1

True or False: Hash functions are generally selected using a criterion of minimizing collisions.

True

True or False: Hashing is an efficient method to implement a Dictionary ADT when it is not necessary to maintain the entries in sorted order.

True

True or False: Linear probing involves a linear search through the hash table starting from the location with index corresponding to a hash value of a data item being inserted or retrieved.

True

True or False: Linear probing would not be required for sequential hashing if a perfect hash function were available.

True

T/F If the search key's data type is int, you can use the key itself as the hash code.

True *If the search key is an instance of either byte, short, or char, you can cast it to an int to get a hash code. Thus, casting to an int is one way to generate a hash code.

Open Addressing

When a collision occurs during the addition of an entry to a hash table, an open addressing scheme locates an alternate location in the hash table that is available, or open. You then use this location to reference the new entry.

Why are typical hash functions not perfect?

because they can allow more than one search key to map into a single index, causing a collision in the hash table

When values map into the same hash index

collision

Handeling a scenario where two entries produce the same hash value (produce a collision)

collision resolution

To reduce the chance of a collision choose a hash function that

distributes entries uniformly throughout the hash table

A ___________ takes a search key and produces the integer index of an element in the hash table

hash function

Code for computing a hash value by multiplying the Unicode value of each character by a factor based on the character's position within the string

int hash = 0; int n = s.length(); for (int i = 0; i < n; i++) hash = g * hash + s.charAt(i); *The ith character of the string is s.charAt(i). Adding this character to the product g * hash actually adds the character's Unicode value. An explicit cast of s.charAt(i) to int is not necessary and would not affect the result. "g" is a constant intelligently determined by you.

Finding an unused, or open, location in the hash table is called

open addressing

A ____________ maps each search key into a different integer that is suitable as an index to the hash table

perfect hash function

Method that computes the hash index for a given search key whose data type is the generic object type K using using "c % n" hash table index compression (c=hash, n=table length) for array "hashTable".

private int getHashIndex(K key) { int hashIndex = key.hashCode() % hashTable.length; if (hashIndex < 0) hashIndex = hashIndex + hashTable.length; return hashIndex; } // end getHashIndex

Locating an open location in the hash table is called

probing

Linear probing

resolves a collision during hashing by examining consecutive locations in the hash table—beginning at the original hash index—to find the next available one. With linear probing, if a collision occurs at hashTable[k], we see whether hashTable[k + 1] is available. If not, we look at hashTable[k + 2], and so on.

When a function is applied to a search key associated with some information to be stored to map the key to slots in an array to use when inserting or retrieving data with the key is called

sequential hashing

hashCode

will return an int value based on the invoking object's memory address * Java's base class Object has a method hashCode that returns an integer hash code. Since every class is a subclass of Object, all classes inherit this method


Kaugnay na mga set ng pag-aaral

FINC Sirmans Final Quizzes and Menti Questions

View Set

Principles of Personal Health Unit 1

View Set

Chapter 5 TCI World History 9/29/19

View Set

BIO 110 EXAM 4 CLICKER QUESTIONS

View Set

M9 - Chapter 6 (Participating Life Insurance Policies)

View Set

General Psychology Chapters 11 and 14

View Set