Jaucējkartes ir indeksētas datu struktūras. Jaucējkarte izmanto a jaucējfunkcija lai aprēķinātu indeksu ar atslēgu segmentu vai slotu masīvā. Tās vērtība tiek kartēta uz segmentu ar atbilstošo indeksu. Atslēga ir unikāla un nemainīga. Padomājiet par jaucējkarti kā skapi ar atvilktnēm ar etiķetēm tajās glabātajām lietām. Piemēram, lietotāja informācijas glabāšana — uzskatiet e-pastu kā atslēgu, un mēs varam kartēt šim lietotājam atbilstošās vērtības, piemēram, vārdu, uzvārdu utt.
Hash funkcija ir jaucējkartes ieviešanas kodols. Tas paņem atslēgu un pārvērš to par segmenta indeksu segmentu sarakstā. Ideālai jaukšanai katrai atslēgai ir jāizveido atšķirīgs indekss. Tomēr var rasties sadursmes. Ja jaukšana nodrošina esošu indeksu, mēs varam vienkārši izmantot kopu vairākām vērtībām, pievienojot sarakstu vai veicot atkārtotu jaukšanu.
Programmā Python vārdnīcas ir hash karšu piemēri. Mēs redzēsim jaucējkartes ieviešanu no nulles, lai uzzinātu, kā izveidot un pielāgot šādas datu struktūras meklēšanas optimizēšanai.
Jaucējkartes dizains ietvers šādas funkcijas:
- set_val(atslēga, vērtība): Ievieto atslēgu-vērtību pāri jaucējkartē. Ja vērtība jau pastāv jaucējkartē, atjauniniet vērtību.
- get_val(atslēga): Atgriež vērtību, ar kuru norādītā atslēga ir kartēta, vai Ieraksts nav atrasts, ja šajā kartē nav atslēgas kartējuma.
- delete_val(atslēga): Noņem konkrētās atslēgas kartējumu, ja jaucējkarte satur atslēgas kartējumu.
Zemāk ir tā ieviešana.
Python3
json no java objekta
class> HashTable:> ># Create empty bucket list of given size> >def> __init__(>self>, size):> >self>.size>=> size> >self>.hash_table>=> self>.create_buckets()> >def> create_buckets(>self>):> >return> [[]>for> _>in> range>(>self>.size)]> ># Insert values into hash map> >def> set_val(>self>, key, val):> > ># Get the index from the key> ># using hash function> >hashed_key>=> hash>(key)>%> self>.size> > ># Get the bucket corresponding to index> >bucket>=> self>.hash_table[hashed_key]> >found_key>=> False> >for> index, record>in> enumerate>(bucket):> >record_key, record_val>=> record> > ># check if the bucket has same key as> ># the key to be inserted> >if> record_key>=>=> key:> >found_key>=> True> >break> ># If the bucket has same key as the key to be inserted,> ># Update the key value> ># Otherwise append the new key-value pair to the bucket> >if> found_key:> >bucket[index]>=> (key, val)> >else>:> >bucket.append((key, val))> ># Return searched value with specific key> >def> get_val(>self>, key):> > ># Get the index from the key using> ># hash function> >hashed_key>=> hash>(key)>%> self>.size> > ># Get the bucket corresponding to index> >bucket>=> self>.hash_table[hashed_key]> >found_key>=> False> >for> index, record>in> enumerate>(bucket):> >record_key, record_val>=> record> > ># check if the bucket has same key as> ># the key being searched> >if> record_key>=>=> key:> >found_key>=> True> >break> ># If the bucket has same key as the key being searched,> ># Return the value found> ># Otherwise indicate there was no record found> >if> found_key:> >return> record_val> >else>:> >return> 'No record found'> ># Remove a value with specific key> >def> delete_val(>self>, key):> > ># Get the index from the key using> ># hash function> >hashed_key>=> hash>(key)>%> self>.size> > ># Get the bucket corresponding to index> >bucket>=> self>.hash_table[hashed_key]> >found_key>=> False> >for> index, record>in> enumerate>(bucket):> >record_key, record_val>=> record> > ># check if the bucket has same key as> ># the key to be deleted> >if> record_key>=>=> key:> >found_key>=> True> >break> >if> found_key:> >bucket.pop(index)> >return> ># To print the items of hash map> >def> __str__(>self>):> >return> ''.join(>str>(item)>for> item>in> self>.hash_table)> hash_table>=> HashTable(>50>)> # insert some values> hash_table.set_val(>'[email protected]'>,>'some value'>)> print>(hash_table)> print>()> hash_table.set_val(>'[email protected]'>,>'some other value'>)> print>(hash_table)> print>()> # search/access a record with key> print>(hash_table.get_val(>'[email protected]'>))> print>()> # delete or remove a value> hash_table.delete_val(>'[email protected]'>)> print>(hash_table)> |
>
>
tīģera un lauvas atšķirība
Izvade:

Laika sarežģītība:
Piekļuve atmiņas indeksam prasa pastāvīgu laiku, un jaukšana prasa pastāvīgu laiku. Tādējādi jaucējkartes meklēšanas sarežģītība ir arī nemainīgs laiks, tas ir, O(1).
HashMaps priekšrocības
● Ātra nejauša piekļuve atmiņai, izmantojot jaucējfunkcijas
● Var izmantot negatīvas un neintegrālās vērtības, lai piekļūtu vērtībām.
● Atslēgas var glabāt sakārtotā secībā, tādējādi tās var viegli atkārtot kartēs.
HashMaps trūkumi
● Sadursmes var izraisīt lielus sodus, un laika sarežģītība var kļūt lineāra.
● Ja taustiņu skaits ir liels, viena jaucējfunkcija bieži izraisa sadursmes.
HashMaps lietojumprogrammas
● Tiem ir lietojumprogrammas kešatmiņas implementācijās, kurās atmiņas vietas tiek kartētas nelielās kopās.
● Tos izmanto, lai indeksētu korešus datu bāzu pārvaldības sistēmās.
lateksa fontu izmēri
● Tos izmanto arī tādos algoritmos kā Rabina Karpa modeļa saskaņošana