The Hash Table Data Structure Mugurel Ionu Andreica Spring 2012.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Comp 122, Spring 2004 Hash Tables – 1. hashtables - 2 Lin / Devi Comp 122, Fall 2003 Dictionary Dictionary: »Dynamic-set data structure for storing items.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
The Linux Kernel: Memory Management
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
CHAPTER 7 HASHING What is hashing for? For searching But we already have binary search in O( ln n ) time after sorting. And we already have algorithms.
The Stack Data Structure Mugurel Ionu Andreica Spring 2012.
© 2004 Goodrich, Tamassia Hash Tables1  
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
Data Structures Mugurel Ionu Andreica Spring 2012.
Theory I Algorithm Design and Analysis (7 Hashing: Open Addressing)
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Hash Tables1 Part E Hash Tables  
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
C++ Classes and Data Structures Jeffrey S. Childs
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Hash Tables1   © 2010 Goodrich, Tamassia.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
© 2004 Goodrich, Tamassia Hash Tables1  
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Robbie CSCI2100A Data Structures Tutorial
The Map ADT and Hash Tables. 2 The Map ADT  Map: An abstract data type where a value is "mapped" to a unique key  Need a key and a value to insert new.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
The Graph Data Structure Mugurel Ionu Andreica Spring 2012.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
LinkedList Class.
The Binary Tree Data Structure
The Binary Search Tree Data Structure
Programmazione I a.a. 2017/2018.
Chapter 16-2 Linked Structures
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Review & Lab assignments
Presentation transcript:

The Hash Table Data Structure Mugurel Ionu Andreica Spring 2012

Operations put(key, value) –Inserts the pair (key, value) in the hash table –If a pair (key, value’) (with the same key) already exists, then value’ is replaced by value –We say that the value value is associated to the key key get(key) –Returns the value associated to the key key –If no value is associated to key, then an error occurs hasKey(key) –Returns 1 if the key key exists in the hash table, and 0 otherwise

Example put(3, 7.9) put(2, 8.3) get(3) -> returns 7.9 put(3, 10.2) get(3) -> returns 10.2 get(2) -> returns 8.3 hasKey(5) -> returns 0 hasKey(2) -> returns 1 get(5) -> generates an error

Possible implementation Maintain an array H[HMAX] of linked lists –The info field of each element of a list consists of a struct containing a key and a value Each key is mapped to a value hkey=hash(key), such that 0≤hkey≤HMAX-1 –hash(key) is called the hash function put(k, v) –Searches for the key k in the list H[hkey=hash(k)] –If the key is found, then we replace the value by v –If the key is not found, then we insert the pair (k,v) in H[hkey] get(k) –Search for the key k in H[hkey=hash(k)] –If it finds the key, then it returns its associated value; otherwise, an error occurs hasKey(k) –Search for the key k in H[hkey=hash(k)] –If it finds the key, then it returns 1; otherwise, it returns 0

Possible implementation (cont.) Class Hashtable HMAX and hash => arguments in the constructor –The function hash will be passed as an argument (actually, a pointer to the function will be passed in fact) –Obviously, hash must be defined differently according to the data type of the keys (see later some examples for int and char*) –The array H: allocated dynamically in the constructor & deallocated in the destructor

Hash Table – Implementation (hash_table.h) #include "linked_list.h" template struct elem_info { Tkey key; Tvalue value; }; template class Hashtable { private: LinkedList > *H; int HMAX; int (*hash) (Tkey); public: Hashtable(int hmax, int (*h) (Tkey)) { HMAX = hmax; hash = h; H = new LinkedList<struct elem_info<Tkey, Tvalue> > [HMAX]; } ~Hashtable() { for (int i = 0; i < HMAX; i++) { while (!H[i].isEmpty()) H[i].removeFirst(); } delete H; } void put(Tkey key, Tvalue value) { struct list_elem > *p; struct elem_info info; int hkey = hash(key); p = H[hkey].pfirst; while (p != NULL) { /* the == operator must be meaningful when comparing values of the type Tkey ; otherwise, an equality testing function should be passed as an argument to the constructor */ if (p->info.key == key) break; p = p->next; } if (p != NULL) p->info.value = value; else { info.key = key; info.value = value; H[hkey].addLast(info); }

Hash Table – Implementation (hash_table.h) (cont.) Tvalue get(Tkey key) { struct list_elem > *p; int hkey = hash(key); p = H[hkey].pfirst; while (p != NULL) { if (p->info.key == key) break; p = p->next; } if (p != NULL) return p->info.value; else { fprintf(stderr, "Error The key does not exist in the hashtable\n"); Tvalue x; return x; } int hasKey(Tkey key) { struct list_elem > *p; int hkey = hash(key); p = H[hkey].pfirst; while (p != NULL) { if (p->info.key == key) break; p = p->next; } if (p != NULL) return 1; else return 0; } };

Using the Hash Table - example #include #include “hash_table.h” #define VMAX 17 #define P 13 int hfunc(int key) { return (P * key) % VMAX; } Hashtable hid(VMAX, hfunc); int hfunc2(char* key) { int hkey = 0; for (int i = 0; i < strlen(key); i++) hkey = (hkey * P + key[i]) % VMAX; return hkey; } Hashtable hci(VMAX, hfunc2); char *k1 = "abc"; char *k2 = "xyze"; char *k3 = "Abc"; char *k4 = "abcD"; int main() { hid.put(3, 7.9); hid.put(2, 8.3); printf("%.3lf\n", hid.get(3)); hid.put(3, 10.2); printf("%.3lf\n", hid.get(3)); printf("%.3lf\n", hid.get(2)); printf("%d\n", hid.hasKey(5)); printf("%d\n", hid.hasKey(2)); printf("%.3lf\n", hid.get(5)); hci.put(k1, 10); hci.put(k2, 20); printf("%d\n", hci.get(k1)); hci.put(k1, 30); printf("%d\n", hci.get(k1)); printf("%d\n", hci.get(k2)); printf("%d\n", hci.hasKey(k3)); printf("%d\n", hci.hasKey(k2)); printf("%d\n", hci.get(k4)); char *k5 = new char[4]; k5[0] = ‘a’; k5[1] = ‘b’; k5[2] = ‘c’; k5[3] = 0; printf("%d\n", hci.get(k5)); // what happens ? return 0; }

Final remarks The Hash table is a fundamental data structure in many situations –Packet routing in the Internet –Session management in web servers –Web search (e.g. Google) –etc. Many other implementations exist, besides using an array of linked lists –For example: Linear probing, Cuckoo hashing, etc. –Many of them are more efficient, but more difficult to understand