Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hashing, Hashing Tables Chapter 8. Class Hierarchy.

Similar presentations


Presentation on theme: "Hashing, Hashing Tables Chapter 8. Class Hierarchy."— Presentation transcript:

1 Hashing, Hashing Tables Chapter 8

2 Class Hierarchy

3 Introduction Definition: –Key: a key is a field or composite of fields that uniquely identifies an entry in a table.

4 Example Table of students in a course sorted by name -------------------------------------------------------------- NameYearMark -------------------------------------------------------------- Adams, Keith394 Davis, Susan175 Jordan, Ann186 Patterson, Lynn473 Williams, George165

5 Insert Function of ListAsArray

6 Find Function of ListAsArray

7 Insert Function of ListAsLinkedList

8 Find Function of ListAsLinkedList

9 Insert Function of SortedListAsArray

10 Binary Search

11 Hashing The implementation of hash tables is called Hashing. Hashing is a technique used for performing insertions and finds in constant average time. Efficient removal of items not required

12 The General Idea –Array of some fixed size, containing items.

13 Example

14 Keys and Hash Functions Each key is mapped into some number in the range 0 to TableSize-1 and placed in the appropriate cell. The mapping is called a hash function

15 Keys and Hash Functions Characteristics of a good hash function –Avoids collisions –Spread keys evenly in the array –Easy to compute

16 Avoid Collisions Ideal situation –Given a set of n<=M distinct keys {k1,k2,…,kn}, the set of hash values {h(k1),h(k2),…,h(kn)} contains no duplicates We can only try to reduce the likelihood of a collision using knowledge about the keys E.g. if we know the telephone numbers are all from the same district, so the district number will have little use in our hash function

17 Spreading Keys Evenly We need to know the distribution of the keys An equal number of keys should map into each array position

18 Ease of Computation The running time of the hash function should be O(1) (Jumping immediately to the desired record is a direct access approach, much like direct access of data on a disk)

19 Hashing Methods We are dealing with integer values first, K=Z The value of the hash function falls between 0 and M-1

20 Division Method The simplest method of hashing an integer The division method of hashing h(x) = x mod M.

21 Choice of M Generally, any M is good –we often choose M to be a prime number

22 Implementation Unsigned int const M = 1031; // a prime Unsigned int h(unsigned int x) { return x%M; }

23 Middle Square Method Avoid division Making use of the fact that computer does finite- precision integer arithmetic –All arithmetic is done modulo W, where W=2 w, w is the word size of the computer M=2 k, W=2 w Meaning: –Multiply x by itself, then shift to the right k bits.

24 Implementation unsigned int const k = 10; // M==1024 unsigned int const w = bitsizeof (unsigned int); unsigned int h (unsigned int x) { return (x * x) >> (w - k); }

25 Multiplication Method We multiply the key by a

26 Implementation unsigned int const k = 10; // M==1024 unsigned int const w = bitsizeof (unsigned int); unsigned int const a = 2654435769U; unsigned int h (unsigned int x) { return (x * a) >> (w - k); } }

27 Hash Tables

28 HashTable Class Definition

29 Separate Chaining


Download ppt "Hashing, Hashing Tables Chapter 8. Class Hierarchy."

Similar presentations


Ads by Google