Upcoming SlideShare
×

# Computer notes - Hashing

1,628 views
1,516 views

Published on

An AVL tree, ordered by key insert: a standard insert; (log n) find: a standard find (without removing, of course); (log n) remove: a standard remove; (log n)

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
•
Are you sure you want to  Yes  No
• Be the first to like this

Views
Total views
1,628
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
44
1
Likes
0
Embeds 0
No embeds

No notes for slide
• Start of 41.
• Start lecture 41
• End of lecture 41. Start of lecture 42.
• ### Computer notes - Hashing

1. 1. Class No.35 Data Structures http://ecomputernotes.com
2. 2. Skip List: Implementation   S 0 S 1 S 2 S 3   45 12 23 34   34   23 34 http://ecomputernotes.com
3. 3. Implementation: TowerNode <ul><li>TowerNode will have array of next pointers. </li></ul><ul><li>Actual number of next pointers will be decided by the random procedure. </li></ul><ul><li>Define MAXLEVEL as an upper limit on number of levels in a node. </li></ul>http://ecomputernotes.com 40 50 60 head tail 20 30 26 57 Tower Node
4. 4. Implementation: QuadNode <ul><li>A quad-node stores: </li></ul><ul><ul><li>item </li></ul></ul><ul><ul><li>link to the node before </li></ul></ul><ul><ul><li>link to the node after </li></ul></ul><ul><ul><li>link to the node below </li></ul></ul><ul><ul><li>link to the node above </li></ul></ul><ul><li>This will require copying the key (item) at different levels </li></ul>x quad-node http://ecomputernotes.com
5. 5. Skip Lists with Quad Nodes 56 64 78  31 34 44  12 23 26    31  64  31 34  23 S 0 S 1 S 2 S 3 http://ecomputernotes.com
6. 6. Performance of Skip Lists <ul><li>In a skip list with n items </li></ul><ul><ul><li>The expected space used is proportional to n . </li></ul></ul><ul><ul><li>The expected search, insertion and deletion time is proportional to log n . </li></ul></ul><ul><li>Skip lists are fast and simple to implement in practice </li></ul>http://ecomputernotes.com
7. 7. Implementation 5: AVL tree <ul><li>An AVL tree, ordered by key </li></ul><ul><li>insert : a standard insert; (log n ) </li></ul><ul><li>find : a standard find (without removing, of course); (log n ) </li></ul><ul><li>remove : a standard remove; (log n ) </li></ul>and so on http://ecomputernotes.com key entry key entry key entry key entry
8. 8. Anything better? <ul><li>So far we have find, remove and insert where time varies between constant log n . </li></ul><ul><li>It would be nice to have all three as constant time operations! </li></ul>http://ecomputernotes.com
9. 9. <ul><li>An array in which TableNodes are not stored consecutively </li></ul><ul><li>Their place of storage is calculated using the key and a hash function </li></ul><ul><li>Keys and entries are scattered throughout the array. </li></ul>Implementation 6: Hashing key entry Key hash function array index 4 10 123 http://ecomputernotes.com
10. 10. <ul><li>insert : calculate place of storage, insert TableNode; (1) </li></ul><ul><li>find : calculate place of storage, retrieve entry; (1) </li></ul><ul><li>remove : calculate place of storage, set it to null; (1) </li></ul>Hashing key entry 4 10 123 All are constant time (1) ! http://ecomputernotes.com
11. 11. Hashing <ul><li>We use an array of some fixed size T to hold the data. T is typically prime. </li></ul><ul><li>Each key is mapped into some number in the range 0 to T-1 using a hash function , which ideally should be efficient to compute. </li></ul>http://ecomputernotes.com
12. 12. Example: fruits <ul><li>Suppose our hash function gave us the following values: </li></ul><ul><ul><li>hashCode(&quot;apple&quot;) = 5 hashCode(&quot;watermelon&quot;) = 3 hashCode(&quot;grapes&quot;) = 8 hashCode(&quot;cantaloupe&quot;) = 7 hashCode(&quot;kiwi&quot;) = 0 hashCode(&quot;strawberry&quot;) = 9 hashCode(&quot;mango&quot;) = 6 hashCode(&quot;banana&quot;) = 2 </li></ul></ul>http://ecomputernotes.com kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9
13. 13. Example <ul><li>Store data in a table array: </li></ul><ul><ul><li>table[5] = &quot;apple&quot; table[3] = &quot;watermelon&quot; table[8] = &quot;grapes&quot; table[7] = &quot;cantaloupe&quot; table[0] = &quot;kiwi&quot; table[9] = &quot;strawberry&quot; table[6] = &quot;mango&quot; table[2] = &quot;banana&quot; </li></ul></ul>http://ecomputernotes.com kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9
14. 14. Example <ul><li>Associative array: </li></ul><ul><ul><li>table[&quot;apple&quot;] table[&quot;watermelon&quot;] table[&quot;grapes&quot;] table[&quot;cantaloupe&quot;] table[&quot;kiwi&quot;] table[&quot;strawberry&quot;] table[&quot;mango&quot;] table[&quot;banana&quot;] </li></ul></ul>http://ecomputernotes.com kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9
15. 15. Example Hash Functions <ul><li>If the keys are strings the hash function is some function of the characters in the strings. </li></ul><ul><li>One possibility is to simply add the ASCII values of the characters: </li></ul>TableSize ABC h Example TableSize i str str h length i )% 67 66 65 ( ) ( : % ] [ ) ( 1 0                http://ecomputernotes.com
16. 16. Finding the hash function <ul><li>int hashCode( char* s ) { int i, sum; sum = 0; for(i=0; i < strlen(s); i++ ) sum = sum + s[i]; // ascii value return sum % TABLESIZE; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
17. 17. Example Hash Functions <ul><li>Another possibility is to convert the string into some number in some arbitrary base b ( b also might be a prime number): </li></ul>T b b b ABC h Example T b i str str h length i i )% 67 66 65 ( ) ( : % ] [ ) ( 2 1 0 1 0                 http://ecomputernotes.com
18. 18. Example Hash Functions <ul><li>If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features. </li></ul><ul><li>For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys. </li></ul><ul><li>In general, to avoid situations like this, T should be a prime number. </li></ul>http://ecomputernotes.com