Download presentation
Presentation is loading. Please wait.
1
Hash tables in C
2
Outline Data storage problems Hash Table Design
Hash function terminology “Good” hash functions Example in C Acknowledgements
3
Data Storage Limitations with data structures we have learned so far:
Suppose we have two entities e.g. word and definition {we are probably making a dictionary} Limitations with data structures we have learned so far: Arrays: Have to form two ‘parallel’ arrays-> search one containing the words-> retrieve same key from second array Linked list: Go through list elements one by one ->find cell containing word -> retrieve definition
4
Problems: Ar[..] Arrays : Have to follow two structures Double operations Linked Lists: Too slow, dangling pointers
5
Solution: Combine fast retrieval power of arrays and linear characteristic of linked lists. Symtab[NHASH] NULL Name 1 Value 1 NULL Name 2 Value 2 NULL Name 3 Value 3
6
Design Analyze data Storing: Come up with “good” hash function
Hash key to find “bucket” Store Retrieving: Hash key Lookup table Get data Other possible facilitators: Deletion Replacement
7
Terminology Using our dictionary program application example: Key => word Hash function (convert key to hash) Hash => encoded word Hash table => ‘table’ of hashed values Bucket => ‘cell’ in a hash table Value => word definitions Chaining => technique of linking definitions
8
Overcoming Same Hash Problems
Two main ways: Overwriting Chaining Picked according to programming challenge being tackled
9
Chaining! Symtab[NHASH] NULL Name 1 Value 1 Name 3 Value 3
10
Good hash functions Good function:
Perfect function: One that randomly spreads out all keys into buckets perfectly randomly Symtab[NHASH] NULL -+=-+=-+=-+=-+=-+= Symtab[NHASH] -+=-+=-+=-+=-+=-+= Good function: Very close to perfect if not perfect
11
Bad hash functions Multiple chains
Doesn’t consider data e.g. common multiples Different hashes for the same key Simply: bad algorithm Symtab[NHASH] NULL -+=-+=-+=-+=-+=-+=
12
C example C-ing is believing
15
for words in our dictionary
Hash Function for words in our dictionary
16
Bad hash function example:
Bad modulation Not accounting for multiples Not accounting for constants in names e.g. java using constants in names .class In urls (^ *.htm$, *.html$) Using a random number generator to spread out data
17
Uses of hash tables in real world
IP and DNS tables Memory allocation Almost any two-part data More parts with custom data types
18
? ? Questions
19
Acknowledgements
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.