Hash tables open and closed hash hash (C ++ to achieve)

hash concept

Hashing algorithm mapping arbitrary length binary value to a binary value of a short fixed length value known as small binary hash value. Hash value is a unique value of a period and extremely compact data representation.
Search method: a small amount may be compared once or 2-3 times to obtain the data from the data to be searched, a large number of data search time complexity becomes O (1).

Hash function hashfunc

Can build relationships one hundred million mapped by the hash function storage elements in the table, you can find through this function directly to the location of the data in the hash table.

Insert elements

As required by hash function, mapping data by speaking the longer an shorter data hash function, for storage of the element

Search element

To give the corresponding map data and hash function values ​​by the element to be searched, and then find the position of the element and the hash table according to the map data.

Common hash function
  • Direct addressing method: hash (key) = a * key + b - a linear address set directly
  • In addition to I stay: hash (key) = key% n - the number of I as a hash address
  • Middle-square method: For example, data 123, 15129 squared, take the middle of the hash address 12
  • Random number method: hash (key) = random (key) random random number
  • Mathematical analysis: Some regular store data, such as phone numbers. (To be analyzed, during storage)

Hash collision

When using the hash value except when the address I stay, then talk about 4,5,6,7,9 memory hash table, at the time when the deposit of 44, found that 44 places have been occupied, that is, He said the same two data calculated hash address, so it happened hash collision
Here Insert Picture Description
Here Insert Picture Description

Hash solution of the conflict - closed hash

Closed Hash - linear probing

insert

Is simply look back from the place of conflict, can be stored until you find the location, store it. If you look to the end yet in place, continue to look backwards from the position 0.
hash (key) = key% capacity
Here Insert Picture Description

delete

When using closed hashing hash collision, not just physically remove the existing elements of the hash table, if directly delete elements will affect the search for other elements. For example, remove elements 4, if you delete a direct swap, 44 find them may be affected. Thus using linear probe labeled pseudo-deletion methods to remove an element. Use {EMPTY, EXIST, DELETE} three flags is represented.

Expansion

= Load factor populated elements in the table / hash table length
when the load factor is greater than equal to 0.7, for expansion, expansion is the need to re-hash storage elements.

Closed Hash - secondary probe

Obviously the second probe is to use the remainder of the division remainder method is left quadratic carried out in order to detect backwards .

insert

And inserting the same manner as linear probing

delete

And delete the same way as linear probing

Expansion

The load factor for expansion is generally 0.5.

Closed hash code implementation

Linear probing hash C ++ implementation github link

Open hash

In the form of hash bucket, that is, the internal hash table to store the list, so you can make the same elements during storage. Directly to the node insertion.
Here Insert Picture Description
When the insertion element 44, the hash table Run
Here Insert Picture Description

delete

The nodes can be deleted directly

Expansion

When expansion is also necessary to increase the hash bucket Capacity, and then rearranged node.

Open hash code implementation

Hash bucket Code github link

Published 52 original articles · won praise 26 · views 3400

Guess you like

Origin blog.csdn.net/weixin_43796685/article/details/104538600