Hash Tables in C James Goerke.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Programming and Data Structure
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Algorithms and Data Structures* Objective: To review the fundamental algorithms and data structures that are commonly used in programs. To see how to use.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
HASH TABLES -Paritosh Gupta. Problem. Required Search for The Precious One way would be to map all the data. And get key-value pairs. This means providing.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Hashing.
Higher Order Tries Key = Social Security Number.
Hashing CSE 2011 Winter July 2018.
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Data Structures and Algorithms for Information Processing
Hashing Alexandra Stefan.
Review Graph Directed Graph Undirected Graph Sub-Graph
Bitwise Hashing.
Chapter 8 Arrays Objectives
Introduction to Hashing - Hash Functions
Efficiency add remove find unsorted array O(1) O(n) sorted array
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Hash Tables Part II: Using Buckets
Hash table another data structure for implementing a map or a set
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
CS223 Advanced Data Structures and Algorithms
Hash Tables in C Louis Manco.
Tries A trie is another type of tree structure. The word “trie” comes from the word “retrieval,” but is usually pronounced like “try.” For our purposes,
Teach A level Computing: Algorithms and Data Structures
Growing Arrays in C Varoon Wadhwa.
Chapter 8 Arrays Objectives
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE 373: Data Structures and Algorithms
Hash Tables and Associative Containers
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Hash Tables: A basic O(1)verview
Hashing Alexandra Stefan.
Hash tables in C.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
How to use hash tables to solve olympiad problems
Chapter 8 Arrays Objectives
CS223 Advanced Data Structures and Algorithms
Data Structures & Algorithms
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Podcast Ch21b Title: Collision Resolution
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Data Structures and Algorithm Analysis Hashing
Chapter 5: Hashing Hash Tables
Lecture-Hashing.
Presentation transcript:

Hash Tables in C James Goerke

Basic Overview of Hash Tables Combines use of arrays and lists Creates array of lists that chains together the items that share a hash value Creates an efficient structure for storing and retrieving dynamic data Often used in compilers to store variables, web browsers to track history, and in internet connections to cache recently used domain names/ip addresses Created based off use of pre-defined hash function

Hash Chains Average list length is n/(array size) (where n is the amount of items) Points to next item or is marked null if at end of list  NULL name 1 name 2 value 1 value 2 name 3 value 3

Hash Type Element type same as lists typedef struct Nameval Nameval; char *name; int value; nameval *next; /* in chain */ }; Nameval *symtab[NHASH]; /* a symbol table */

Hash Function/Calculation Passes key through in order to generate a “hash value” Hash value would be evenly distributed through a modest-sized integer range Hash value then used to index a table where information is stored One of the most common hashing algorithms uses the key string and builds a hash value by adding each byte of the string to a multiple of the current hash

Calculation Example enum { MULTIPLIER = 31 }; /* hash: compute hash value of string */ Unsigned int hash(char *str) { unsigned int h; unsigned char *p; h = 0; for (p = (unsigned char *) str; *p != ‘\0’; p++) h = MULTIPLIER * h + *p; return h % NHASH; //returns (result)mod(size of array)

Hash Table Lookup/Insert /* lookup: find name in symtab, with optional */ Nameval = lookup(char *name, int create, int value) { int h = hash; Nameval *sym; h = hash(name); for (sym = symtab[h]; sym != NULL; sym = sym->next) if (strcmp(name, sym->name) == 0) return sym; if (create) { sym = (Nameval *) emalloc(sizeof(Nameval)); sym->name = name; /* assumed allocated elsewhere */ sym->value = value; sym->next = symtab[h]; symtab[h] = sym; }

Possible Limitations A poor hash function or table size that is too small can lead to lists that grow long If average list size is too large this can lead to O(n) behavior instead of O(1) behavior. If used properly though, the constant –time lookup, insertion and deletion properties are unmatched by other sorting techniques.

Questions?