---------------------------------------------------------------------------------
HASHING TABLE & BINARY TREE
---------------------------------------------------------------------------------
Hashing Table
Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the following items are to be stored. Item are in the (key,value) format.
- (1,20)
- (2,70)
- (42,80)
Sr.No.
|
Key
|
Hash
|
Array Index
|
1
|
1
|
1 % 20 = 1
|
1
|
2
|
2
|
2 % 20 = 2
|
2
|
3
|
42
|
42 % 20 = 2
|
2
|
Linear Probing
The hashing technique is used to create an already used index of the array. In such a case, we can search the next empty location in the array by looking into the next cell until we find an empty cell. This technique is called linear probing.
Sr.No.
|
Key
|
Hash
|
Array Index
|
After Linear Probing, Array Index
|
1
|
1
|
1 % 20 = 1
|
1
|
1
|
2
|
2
|
2 % 20 = 2
|
2
|
2
|
3
|
42
|
42 % 20 = 2
|
2
|
3
|
Basic Operation
- Search
Whenever an element is to be searched, compute the hash code of the key passed and locate the element using that hash code as index in the array. Use linear probing to get the element ahead if the element is not found at the computed hash code.
2. Insert
Whenever an element is to be inserted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing for empty location, if an element is found at the computed hash code.
3. delete
Whenever an element is to be deleted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing to get the element ahead if an element is not found at the computed hash code. When found, store a dummy item there to keep the performance of the hash table intact.
Binary Tree
Binary Tree
A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. A Binary Tree node contains following parts
- Data
- Pointer to left child
- Pointer to right child

No comments:
Post a Comment