{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

21_hash_tables

# 21_hash_tables - CSCI-1200 Data Structures Fall 2010...

This preview shows pages 1–3. Sign up to view the full content.

CSCI-1200 Data Structures — Fall 2010 Lecture 21 – Hash Tables Review from Lecture 20 A Priority Queue as a Heap, percolate_up and percolate_down A Heap as a Vector, Building a Heap, Heap Sort Merging heaps are the motivation for leftist heaps Today’s Lecture Hash Tables, Hash Functions, and Collision Resolution Hash Table Performance, Binary Search Trees vs. Hash Tables Collision resolution: separate chaining vs open addressing Using a hash table to implement a set , Function objects, Iterators, find, insert, and erase Ford & Topp Sections 12.1-12.5. 19.1 Definition: What’s a Hash Table? A table implementation with constant time access . Like a set, we can store elements in a collection. Or like a map, we can store key-value pair associations in the hash table. But it’s even faster to do find, insert, and erase with a hash table! However, hash tables do not store the data in sorted order. A hash table is implemented with a array at the top level. Each element or key is mapped to a slot in the array by a hash function . 19.2 Definition: What’s a Hash Function? A simple function of one argument (the key) which returns an index (a bucket or slot in the array). Ideally the function will “uniformly” distribute the keys throughout the range of legal index values (0 k-1). What’s a collision? When the hash function maps multiple (different) keys to the same index. How do we deal with collisions? One way to resolve this is by storing a linked list of values at each slot in the array. 19.3 Example: Caller ID We are given a phonebook with 50,000 name/number pairings. Each number is a 10 digit number. We need to create a data structure to lookup the name matching a particular phone number. Ideally, name lookup should be O(1) time expected, and the caller ID system should use O(n) memory (n = 50,000). Note: In the toy implementations that follow we use small datasets, but we should evaluate the system scaled up to handle the large dataset. The basic interface: // add several names to the phonebook add(phonebook, 1111, "fred"); add(phonebook, 2222, "sally"); add(phonebook, 3333, "george"); // test the phonebook std::cout << identify(phonebook, 2222) << " is calling!" << std::endl; std::cout << identify(phonebook, 4444) << " is calling!" << std::endl; We’ll review how we solved this problem in Lab 9 with an STL vector then an STL map . Finally, we’ll implement the system with a hash table.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
19.4 Caller ID with an STL Vector // create an empty phonebook std::vector<std::string> phonebook(10000, "UNKNOWN CALLER"); void add(std::vector<std::string> &phonebook, int number, std::string name) { phonebook[number] = name; } std::string identify(const std::vector<std::string> &phonebook, int number) { return phonebook[number]; } Exercise: What’s the memory usage for the vector-based Caller ID system?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

21_hash_tables - CSCI-1200 Data Structures Fall 2010...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online