logo

Maišos žemėlapis Python

Maišos žemėlapiai yra indeksuotos duomenų struktūros. Maišos žemėlapyje naudojamas a maišos funkcija norėdami apskaičiuoti indeksą su raktu į segmentų arba lizdų masyvą. Jo reikšmė susieta su segmentu su atitinkamu indeksu. Raktas yra unikalus ir nekintamas. Pagalvokite apie maišos žemėlapį kaip apie spintelę su stalčiais su etiketėmis juose saugomiems daiktams. Pavyzdžiui, saugoti naudotojo informaciją – laikykite el. paštą kaip raktą, ir mes galime priskirti tą vartotoją atitinkančias reikšmes, pvz., vardą, pavardę ir pan., į segmentą.

Maišos funkcija yra maišos žemėlapio įgyvendinimo pagrindas. Jis paima raktą ir paverčia jį segmentų sąraše esančio segmento indeksu. Ideali maiša turėtų sukurti skirtingą kiekvieno rakto indeksą. Tačiau gali įvykti susidūrimai. Kai maiša suteikia esamą indeksą, galime tiesiog naudoti kelių verčių segmentą, pridėdami sąrašą arba atlikdami pakartotinę maišą.

Python kalboje žodynai yra maišos žemėlapių pavyzdžiai. Pamatysime maišos žemėlapio diegimą nuo nulio, kad išmoktume kurti ir pritaikyti tokias duomenų struktūras, kad būtų galima optimizuoti paiešką.



Maišos žemėlapio dizainas apims šias funkcijas:

  • set_val(raktas, reikšmė): Į maišos žemėlapį įterpia rakto-reikšmių porą. Jei reikšmė jau yra maišos žemėlapyje, atnaujinkite reikšmę.
  • get_val(raktas): Grąžina reikšmę, su kuria susietas nurodytas raktas, arba Įrašas nerastas, jei šiame žemėlapyje nėra rakto susiejimo.
  • delete_val(raktas): Pašalina konkretaus rakto susiejimą, jei maišos žemėlapyje yra rakto susiejimas.

Žemiau pateikiamas įgyvendinimas.

Python3

mia khalifa amžius




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)>

>

>

Išvestis:

Laiko sudėtingumas:

Atminties indekso prieiga trunka pastoviai, o maiša trunka pastoviai. Taigi maišos žemėlapio paieškos sudėtingumas taip pat yra pastovus laikas, ty O(1).

rdbms

HashMaps pranašumai

● Greita atsitiktinė prieiga prie atminties naudojant maišos funkcijas

● Vertėms pasiekti gali naudoti neigiamas ir neintegrines reikšmes.

● Raktai gali būti saugomi surūšiuota tvarka, todėl juos galima lengvai kartoti žemėlapiuose.

HashMaps trūkumai

● Dėl susidūrimų gali būti skiriamos didelės baudos, o laiko sudėtingumas gali būti tiesinis.

● Kai raktų skaičius yra didelis, viena maišos funkcija dažnai sukelia susidūrimus.

HashMaps programos

● Jie turi programų talpyklos diegimuose, kur atminties vietos susietos su mažais rinkiniais.

● Jie naudojami eilėms indeksuoti duomenų bazių valdymo sistemose.

● Jie taip pat naudojami algoritmuose, pvz., Rabino Karpo modelio derinimui