Reader small image

You're reading from  C++ Data Structures and Algorithms

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788835213
Edition1st Edition
Languages
Right arrow
Author (1)
Wisnu Anggoro
Wisnu Anggoro
author image
Wisnu Anggoro

Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net
Read more about Wisnu Anggoro

Right arrow

Chapter 8. Associating a Value to a Key in a Hash Table

In the previous chapter, we discussed the hierarchical tree data type, which is a non-linear data type, that stores data in a tree-like structure. In this chapter, we are going to discuss another non-linear data type, the hash table, which stores data based on a key. The following topics are discussed in this chapter:

  • Understanding hash tables
  • Preventing a collision in a hash table
  • Using a separate chaining technique to handle a collision
  • Using an open addressing technique to handle a collision

Technical requirement


To follow along with this chapter, including the source code, we require the following:

Getting acquainted with hash tables


Suppose we want to store a collection of numbers, for instance, a phone number, and let's say we have approximately 1,000,000 numbers. In previous chapters, we also discussed several data structures, and here we can consider using one of them. We can use an array or a list, but we have to provide a million slots of data in the array. If we need to add some phone numbers again, we have to resize the array. Also, the operation of searching will be costly, since we have to use a linear search algorithm with time complexity O(N), where the time consumption will increase if we add data to the list. Indeed, we can use a binary search algorithm with O(log N) time complexity if we manage to sort the elements of the list containing the bunch of phone numbers; however, the insert operation will be costly, since we have to maintain the sorted list.

Another data structure we can choose is the balanced binary search tree. It can give us a moderate time complexity, since...

Implementing a separate chaining technique


Separate chaining is a collision handling technique that will make each cell in the hash table point to a chaining node containing values with the same hash key. We are going to create an ADT named HashTable to handle the preceding phone number list. Since the phone number contains only numbers, it will be stored in the int data type, and the owner of the phone number name will be stored in the string data type. However, if the phone number we have are saved as 123-456-789 format, we need to remove the dash (-) character first.

The HashTable will be four basic operations and they are:

  • Insert() is used to insert a new pair<int, string> to the hash table. It passes an int as a key and a string as a value. It then finds a hash key for the inputted key. If the inputted key is found in the hash table, it will update the value of the key. If no key is found, it will append a new node to the linked list containing the inputted key and inputted value...

Implementing the open addressing technique


As we discussed earlier at beginning of this chapter, an open addressing technique stores all elements in the hash table itself. A collision will not happen, since there is a calculation that will be performed if a collision is about to happen. Based on this calculation, we can have three types of open addressing technique—Linear probing, quadratic probing, and double hashing. The difference between the three is the formula for finding the next free space if the hash key of the given element has been occupied:

  • In linear probing, if the hash key has been occupied by another element, we use the following formula to find the next free space—(HashFunction(key) + n) % TABLE_SIZE, then increase n from 0 until a free slot is found. Here is the explanation—If HashFunction(key) % TABLE_SIZE is occupied, then try (HashFunction(key) + 1) % TABLE_SIZE. If the slot is still occupied, try (HashFunction(key) + 2) % TABLE_SIZE. Repeat it by increasing n until a...

Summary


In this chapter, we have discussed another non-linear data type called a hash table. Although a collision can happen to the hash table data type, we have learned how to handle it by using a separate chaining technique or open addressing technique. Regarding the open addressing technique, it has three different kinds of handling—linear probing, quadratic probing, and double hashing; however, they are relatively similar in terms of the implementation, except for the way in which we find the next free slot if the hash key we need to insert has been occupied.

In the next chapter, we are going to discuss common algorithm paradigms to design algorithms specifically for a certain purpose.

QA section


  • What is collision in a hash table?
  • Specify techniques to handle collision in a hash table!
  • Specify four basic operations in a hash table?
  • How does we obtain hash key for each data?
  • Why do we need hash table data type although we have already had others data type such as array, list, and linked list?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Data Structures and Algorithms
Published in: Apr 2018Publisher: PacktISBN-13: 9781788835213
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Wisnu Anggoro

Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at&nbsp;wisnu@anggoro.net
Read more about Wisnu Anggoro