1 / 32

Hash Tables

Hash Tables. Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the storing/searching problem. Data Structures and Other Objects Using C++. Serial Search.

grantsmith
Download Presentation

Hash Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hash Tables • Chapter 12 discusses several ways of storing information in an array, and later searching for the information. • Hash tablesare a common approach to the storing/searching problem. Data Structures and Other Objects Using C++

  2. Serial Search • Algorithm: step through an array, check one item at a time looking for a desired item

  3. Binary Search

  4. What is a Hash Table ? • The simplest kind of hash table is an array of records. • This example has 701 records. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] . . . An array of records

  5. What is a Hash Table ? [ 4 ] • Each record has a special field --- unique identification value, called its key. • In this example, the key is a long integer field called Number. Number 506643548 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] . . .

  6. What is a Hash Table ? [ 4 ] Number 506643548 • The number might be a person's identification number, and the rest of the record has information about the person. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] . . .

  7. Number 281942902 Number 233667136 Number 506643548 Number 155778322 . . . What is a Hash Table ? • When a hash table is in use, some spots contain valid records, and other spots are "empty". [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  8. Number 281942902 Number 233667136 Number 506643548 Number 155778322 . . . Inserting a New Record Number 580625685 • In order to insert a new record, the key must somehow be converted toan array index. • The index is called the hash valueof the key. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  9. Number 281942902 Number 233667136 Number 506643548 Number 155778322 . . . Inserting a New Record Number 580625685 • Typical way create a hash value through hash function: (Number % 701) What is (580625685 % 701) ? [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  10. Number 281942902 Number 233667136 Number 506643548 Number 155778322 . . . Inserting a New Record Number 580625685 • Typical way to create a hash value: (Number mod 701) 3 What is (580625685 mod 701) ? [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  11. [3] Number 281942902 Number 233667136 Number 506643548 Number 155778322 . . . Inserting a New Record Number 580625685 • The hash value is used for the location of the new record. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  12. Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322 . . . Inserting a New Record • The hash value is used for the location of the new record. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  13. My hash value is [2]. Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322 . . . Collisions Number 701466868 • Here is another new record to insert, with a hash value of 2. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  14. When a collision occurs, move forward until you find an empty spot --- open-address hashing. Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322 . . . Collisions Number 701466868 • This is called a collision, because there is already another valid record at [2]. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  15. When a collision occurs, move forward until you find an empty spot. Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322 . . . Collisions Number 701466868 • This is called a collision, because there is already another valid record at [2]. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  16. When a collision occurs, move forward until you find an empty spot. Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322 . . . Collisions Number 701466868 • This is called a collision, because there is already another valid record at [2]. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  17. The new record goes in the empty spot. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Collisions • This is called a collision, because there is already another valid record at [2]. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  18. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . A Quiz Where would you be placed in this table, if there is no collision? Use your social security number or some other favorite number. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  19. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • The data that's attached to a key can be found fairly quickly. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  20. My hash value is [2]. Not me. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • Calculate the hash value. • Check that location of the array for the key. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  21. My hash value is [2]. Not me. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • Keep moving forward until you find the key, or you reach an empty spot. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  22. My hash value is [2]. Not me. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • Keep moving forward until you find the key, or you reach an empty spot. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  23. My hash value is [2]. Yes! Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • Keep moving forward until you find the key, or you reach an empty spot. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  24. My hash value is [2]. Yes! Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Searching for a Key Number 701466868 • When the item is found, the information can be copied to the necessary location. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  25. Please delete me. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 506643548 Number 155778322 . . . Deleting a Record • Records may also be deleted from a hash table. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  26. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 155778322 . . . Deleting a Record • Records may also be deleted from a hash table. • But the location must not be left as an ordinary "empty spot" since that could interfere with searches. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  27. Number 281942902 Number 233667136 Number 701466868 Number 580625685 Number 155778322 . . . Deleting a Record • Records may also be deleted from a hash table. • But the location must not be left as an ordinary "empty spot" since that could interfere with searches. • The location must be marked in some special way so that a search can tell that the spot used to have something in it. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

  28. Double Hashing • Linear probing: the insertion function move forward from the calculated index until a vacant spot is found in the hash table • Double hashing: uses a second hash function to determine how to resolve a collision • Example: hash1(key) = 330; hash2(key) = 7 • If location 330 is occupied, then move forward 7 spots and try location 337. If 337 is also occupied, then move forward 7 spots to location 344, and so on. • If the calculation of the next spot beyond the end of the array, then “wrap around” to the front of the array

  29. Quadratic Probing • If there is a collision, then move to the next array index. If there is a second collision, then move forward two more spots through the array. After a third collision, move forward three more spots, and so on. • If the calculation of the next spot beyond the end of the array, then “wrap around” to the front of the array

  30. Chained Hashing • Open-address hashing: a collision is handled by probing the array for an unused position. Each array location can hold just one entry. • Chained hashing (Chaining): each location of the hash table’s array can hold more than one entry

  31. Time Analysis

  32. Summary • Hash tables store a collection of records with keys. • The location of a record depends on the hash value of the record's key. • When a collision occurs, the next available location is used. • Searching for a particular key is generally quick. • When an item is deleted, the location must be marked in a special way, so that the searches know that the spot used to be used.

More Related