Ch 8: Hash Tables Flashcards

1
Q

what is a hash table?

A

a data structure that stores unordered items by mapping (hashing) each item to a location in an array (or vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the main advantage of hash tables?

A

an item may require only O(1) insert, remove and search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is a key?

A

a value used to map to an index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a bucket?

A

each hash table array element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a hash function?

A

computes a bucket index from the item’s key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is collision?

A

occurs when an item is inserted into a hash table maps to the same bucket as an existing item in the hash table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is chaining?

A

a collision resolution technique where each bucket has a list of items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is open addressing?

A

a collision resolution technique where collisions are resolved by looking for an empty bucket elsewhere in the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is linear probing?

A

handles a collision by starting at the key’s mapped bucket, then linearly searches subsequent buckets until an empty bucket is found

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are two types of buckets? define them

A

empty-since-start: has been empty since the hash table was created
empty-after-removal: had an item removed that caused the bucket to now be empty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

why does search stop at an empty-since-start but not an empty-after-removal?

A

stops because if previously inserting the item had reached that bucket, then it would have put it there. but since it has been empty since the start, it must not have been inserted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

explain removal with linear probing

A
  • determine the initial bucket
  • probe each bucket until a matching item is found, an empty-since-start bucket is found, or all buckets have been probed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is quadratic probing?

A

starting at the key’s mapped bucket, and then quadratically searches subsequent buckets until an empty bucket is found

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the hash table function for quadratic probing

A

(H + c1 *i + c2&(i^2)) mod (tablesize)
H: item’s initial mapped bucket
c1 & c2: programmer definined constants for quadratic probing
i: increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

explain insert for quadratic probing

A
  • use formula, starting with i = 0 to find bucket
  • insert if bucket is empty
  • otherwise, increment i and use the formula to find a new bucket, repeat
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is a probing sequence?

A

iterating through sequential i values to obtain the desired table values

17
Q

when is a hash table fast?

A

if it minimizes collision

18
Q

what are perfect hash functions?

A

maps items to buckets with no collisions

19
Q

when can a perfect hash function be created?

A

if the number of items and all possible key item keys are known before hand

20
Q

what do good hash functions do?

A

uniformly distribute items into buckets

21
Q

chaining and a good hash result in what?

A

short bucket lists so faster inserts, searches and removes

22
Q

what is the average & worst case operation run times on good hash functions?

A

average: O(1)
worst: O(n)

23
Q

what is a modulo hash?

A

uses remainder from division of key by hash table size

24
Q

what is a midsquare hash?

A

squares the key, extracts R digits from the results middle and returns the remainder of the middle digits divide by hash table size

25
Q

what is a multiplicative string hash?

A

repeatedly multiplies the hash value and adds the ASCII value of each character in the string

26
Q

what is a direct hash function?

A

uses the item’s keys as the bucket index

27
Q

what is a direct access table?

A

a hash table with a direct hash function

28
Q

what is the advantage of a direct access table?

A

no collision

29
Q

what are the two main limitations of a direct access table?

A
  1. all keys must be non-negative integers, but for some applications, keys may be negative
  2. the hash table’s size equals the largest key values plus 1, which may be very large
30
Q

what is double hashing?

A

an open-addressing collision resolution technique that uses 2 different hash functions to compute bucket indices

31
Q

what is the key index for double hashing?

A

(h1(key) + i*h2(key))mod(tablesize)

32
Q

what size is a table resized to?

A

the next prime number >= N*2

33
Q

what is the run time complexity of resizing and why?

A

O(n) because the old array is reindexed to new array

34
Q

what is a load factor?

A

if of items in the hash table divided by the # of buckets

35
Q

when does resizing often occur?

A

when 1+ values exceed a certain threshold:

  1. load factor
  2. when using open-addressing, the # of collisions during an insertion
  3. when using chaining, the size of bucket’s linked list