Dictionary¶
(Also called Hash Table) First we import the dictionary from the AKDSFramework structure module.
from AKDSFramework.structure.dictionary import Hash
Now create a new hash object
hashtable = Hash()
By default the hashtable will create a bucket worth for 60 values. Once it overflows the hashtable will dynamically allocate more bucket spaces for new elements.
Now to include new elements we mention a key value pair in the hashtable’s include
method.
hashtable.include(
key="hello",
value=23
)
hashtable.include(
key="22",
value=1314
)
hashtable.include(
key="4",
value="Hello, Mom"
)
Now Let’s print the buckets. I’ll provide a str method soon to see the hashtable easily.
print(hashtable.buckets)
[None, <Node: (4, Hello, Mom, Next: None)>, None, None, None, None, None, None, None, None, None, None, None, <Node: (22, 1314, Next: None)>, None, None, None, None, None, None, None, None, None, None, None, None, None, <Node: (hello, 23, Next: None)>, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
Searching¶
Hashtables provide a constant time \(O(1)\) time searching for any key. Just use the following default function to find any value corrosponding to a key.
hashtable.searchFor(key="4")
'Hello, Mom'
Deleting¶
You can also remove any key value pair from the hashtable buckets like this
hashtable.removeFor(key="22")
1314
print(hashtable.buckets)
[None, <Node: (4, Hello, Mom, Next: None)>, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, <Node: (hello, 23, Next: None)>, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]