Simplest hash table for deleting elements

Source: Internet
Author: User

There is a simple hash table that uses an array to store elements directly. The hash function is a modulo of the table length, and the conflict resolution adopts linear detection.

Such a hash table is easy to implement. The search and insertion methods follow the steps of hash first and then probing, but deleting the elements in the table is a little complicated.

 

Because conflicting elements exist in the table, these elements apply the probe method to be placed in other locations. Deleting an element in the table may affect the search of these elements, the normal process of searching elements may be interrupted. This is called because deleting an element creates a gap for the hash table. We call the hash value of the keyword key as the hash position of the key, and call the element that uses the probe technology to reschedule the position as the rear element.

 

To avoid this, we need fill in gaps until all elements can be found. Below is a simple piece of code.

 

// Assume that the hash table stores the relevant information of student, and the student ID is used as the keyword.

// The element named student ID is the element ID. Id = 0 indicates that the hash table is not in use.

 

Int find (int id); // returns the position of the element ID in the table.

Int Hash (int id); // calculate the hash value of the element ID

 

Void Delete (int id ){

Int
I = find (ID );

For (;;){

Hash (I). ID
= 0; // Delete element I to generate a gap.

Int
J = I; // the location where J stores the gap. Next we need to select a post element from behind the gap to fill the gap.

For (;) {// select the one closest to the Gap

I =
(++ I) % hash_table_size;

If (Hash (I). ID
= 0) {// an unused slot is encountered, because the post elements from the gap to the slot segment are handled properly, and no new gap will be generated.

Return; // so the method can end.

}

Int
R = hash (I); // If the hash position of the current element R is located between the gap and its current position I, this element is not selected.

If (j <r
& R <= I) // because the element should have been after the gap, it cannot be used to fill the gap, and it is not affected.

Continue; // The three if statements take into account the positional relationship between I and j, that is, the size of I and J. All three conditions limit that R must be between I and j.

If (j> I
& J <R)

Continue;

If (j> I
& R <= I)

Continue;

Break; // The hash position of the element is out of the permitted range, that is, it is the affected post element. Then:

} //

Hash (j) = hash (I); // select this element to fill the gap. And delete this element to generate a new gap.

} //

} // Delete

 

This is just the simplest hash table implementation. To avoid the trouble of such deletion, you can use the linked list method or set a public overflow zone to solve the conflict.

 

There are two good articles:

An implementation of a hash table: http://hi.baidu.com/wihate/blog/item/e0330b7a7a2e9fee2e73b3ae.html

A hash table class implemented by C ++: http://hi.baidu.com/wihate/blog/item/d537ba01070352d5277fb5dc.html

 

We hope to continue learning the hash table later.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.