data_structures.hashing.hash_table

Classes

HashTable

Basic Hash Table example with open addressing and linear probing

Module Contents

class data_structures.hashing.hash_table.HashTable(size_table: int, charge_factor: int | None = None, lim_charge: float | None = None)

Basic Hash Table example with open addressing and linear probing

abstract _collision_resolution(key, data=None)

This method is a type of open addressing which is used for handling collision.

In this implementation the concept of linear probing has been used.

The hash table is searched sequentially from the original location of the hash, if the new hash/location we get is already occupied we check for the next hash/location.

references:

Examples: 1. The collision will be with keys 18 & 99, so new hash will be created for 99 >>> ht = HashTable(3) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.keys() {2: 17, 0: 18, 1: 99}

2. The collision will be with keys 17 & 101, so new hash will be created for 101 >>> ht = HashTable(4) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.insert_data(101) >>> ht.keys() {1: 17, 2: 18, 3: 99, 0: 101}

2. The collision will be with all keys, so new hash will be created for all >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.keys() {2: 17, 3: 18, 4: 99}

3. Trying to insert float key in hash >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99.99) Traceback (most recent call last): … TypeError: list indices must be integers or slices, not float

_set_value(key, data)

_set_value functions allows to update value at a particular hash

Examples: 1. _set_value in HashTable of size 5 >>> ht = HashTable(5) >>> ht.insert_data(10) >>> ht.insert_data(20) >>> ht.insert_data(30) >>> ht._set_value(0,15) >>> ht.keys() {0: 15, 1: 20, 2: 30}

2. _set_value in HashTable of size 2 >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(3,15) >>> ht.keys() {3: 15, 2: 17, 4: 99}

3. _set_value in HashTable when hash is not present >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(0,15) >>> ht.keys() {3: 18, 2: 17, 4: 99, 0: 15}

4. _set_value in HashTable when multiple hash are not present >>> ht = HashTable(2) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht._set_value(0,15) >>> ht._set_value(1,20) >>> ht.keys() {3: 18, 2: 17, 4: 99, 0: 15, 1: 20}

_step_by_step(step_ord)
balanced_factor()
bulk_insert(values)

bulk_insert is used for entering more than one element at a time in the HashTable.

Examples: 1. >>> ht = HashTable(5) >>> ht.bulk_insert((10,20,30)) step 1 [0, 1, 2, 3, 4] [10, None, None, None, None] step 2 [0, 1, 2, 3, 4] [10, 20, None, None, None] step 3 [0, 1, 2, 3, 4] [10, 20, 30, None, None]

2. >>> ht = HashTable(5) >>> ht.bulk_insert([5,4,3,2,1]) step 1 [0, 1, 2, 3, 4] [5, None, None, None, None] step 2 [0, 1, 2, 3, 4] [5, None, None, None, 4] step 3 [0, 1, 2, 3, 4] [5, None, None, 3, 4] step 4 [0, 1, 2, 3, 4] [5, None, 2, 3, 4] step 5 [0, 1, 2, 3, 4] [5, 1, 2, 3, 4]

hash_function(key)

Generates hash for the given key value

Examples:

Creating HashTable with size 5 >>> ht = HashTable(5) >>> ht.hash_function(10) 0 >>> ht.hash_function(20) 0 >>> ht.hash_function(4) 4 >>> ht.hash_function(18) 3 >>> ht.hash_function(-18) 2 >>> ht.hash_function(18.5) 3.5 >>> ht.hash_function(0) 0 >>> ht.hash_function(-0) 0

insert_data(data)

insert_data is used for inserting a single element at a time in the HashTable.

Examples:

>>> ht = HashTable(3)
>>> ht.insert_data(5)
>>> ht.keys()
{2: 5}
>>> ht = HashTable(5)
>>> ht.insert_data(30)
>>> ht.insert_data(50)
>>> ht.keys()
{0: 30, 1: 50}
keys()

The keys function returns a dictionary containing the key value pairs. key being the index number in hash table and value being the data value.

Examples: 1. creating HashTable with size 10 and inserting 3 elements >>> ht = HashTable(10) >>> ht.insert_data(10) >>> ht.insert_data(20) >>> ht.insert_data(30) >>> ht.keys() {0: 10, 1: 20, 2: 30}

2. creating HashTable with size 5 and inserting 5 elements >>> ht = HashTable(5) >>> ht.insert_data(5) >>> ht.insert_data(4) >>> ht.insert_data(3) >>> ht.insert_data(2) >>> ht.insert_data(1) >>> ht.keys() {0: 5, 4: 4, 3: 3, 2: 2, 1: 1}

rehashing()
__aux_list: list = []
_keys: dict
charge_factor
lim_charge
size_table
values