@@ -21,6 +21,29 @@ def __init__(
21
21
self ._keys : dict = {}
22
22
23
23
def keys (self ):
24
+ """
25
+ The keys function returns a dictionary containing the key value pairs.
26
+ key being the index number in hash table and value being the data value.
27
+
28
+ Examples:
29
+ 1. creating HashTable with size 10 and inserting 3 elements
30
+ >>> ht = HashTable(10)
31
+ >>> ht.insert_data(10)
32
+ >>> ht.insert_data(20)
33
+ >>> ht.insert_data(30)
34
+ >>> ht.keys()
35
+ {0: 10, 1: 20, 2: 30}
36
+
37
+ 2. creating HashTable with size 5 and inserting 5 elements
38
+ >>> ht = HashTable(5)
39
+ >>> ht.insert_data(5)
40
+ >>> ht.insert_data(4)
41
+ >>> ht.insert_data(3)
42
+ >>> ht.insert_data(2)
43
+ >>> ht.insert_data(1)
44
+ >>> ht.keys()
45
+ {0: 5, 4: 4, 3: 3, 2: 2, 1: 1}
46
+ """
24
47
return self ._keys
25
48
26
49
def balanced_factor (self ):
@@ -37,6 +60,43 @@ def _step_by_step(self, step_ord):
37
60
print (self .values )
38
61
39
62
def bulk_insert (self , values ):
63
+ """
64
+ bulk_insert is used for entering more than one element at a time
65
+ in the HashTable.
66
+
67
+ Examples:
68
+ 1.
69
+ >>> ht = HashTable(5)
70
+ >>> ht.bulk_insert((10,20,30))
71
+ step 1
72
+ [0, 1, 2, 3, 4]
73
+ [10, None, None, None, None]
74
+ step 2
75
+ [0, 1, 2, 3, 4]
76
+ [10, 20, None, None, None]
77
+ step 3
78
+ [0, 1, 2, 3, 4]
79
+ [10, 20, 30, None, None]
80
+
81
+ 2.
82
+ >>> ht = HashTable(5)
83
+ >>> ht.bulk_insert([5,4,3,2,1])
84
+ step 1
85
+ [0, 1, 2, 3, 4]
86
+ [5, None, None, None, None]
87
+ step 2
88
+ [0, 1, 2, 3, 4]
89
+ [5, None, None, None, 4]
90
+ step 3
91
+ [0, 1, 2, 3, 4]
92
+ [5, None, None, 3, 4]
93
+ step 4
94
+ [0, 1, 2, 3, 4]
95
+ [5, None, 2, 3, 4]
96
+ step 5
97
+ [0, 1, 2, 3, 4]
98
+ [5, 1, 2, 3, 4]
99
+ """
40
100
i = 1
41
101
self .__aux_list = values
42
102
for value in values :
@@ -69,6 +129,21 @@ def rehashing(self):
69
129
self .insert_data (value )
70
130
71
131
def insert_data (self , data ):
132
+ """
133
+ insert_data is used for inserting a single element at a time in the HashTable.
134
+
135
+ Examples:
136
+
137
+ >>> ht = HashTable(3)
138
+ >>> ht.insert_data(5)
139
+ >>> ht.keys()
140
+ {2: 5}
141
+ >>> ht = HashTable(5)
142
+ >>> ht.insert_data(30)
143
+ >>> ht.insert_data(50)
144
+ >>> ht.keys()
145
+ {0: 30, 1: 50}
146
+ """
72
147
key = self .hash_function (data )
73
148
74
149
if self .values [key ] is None :
@@ -84,3 +159,9 @@ def insert_data(self, data):
84
159
else :
85
160
self .rehashing ()
86
161
self .insert_data (data )
162
+
163
+
164
+ if __name__ == "__main__" :
165
+ import doctest
166
+
167
+ doctest .testmod ()
0 commit comments