Hash tables · טבלאות hash
Hash tables: fast lookup
- A hash table stores items so you can find them very fast — usually in one step.
- It is a list of slots. A hash function turns a key into a slot number.
- Instead of searching every item, you jump straight to the slot the key belongs in.
טבלאות חשיפה: חיפוש מהיר
- טבלת חשיפה מאחסנת פריטים כך שתוכלו למצוא אותם מהר מאוד — בדרך כלל בשלב אחד.
- זוהי רשימה של תאים. פונקציית חשיפה ממירה מקור לתא מספר.
- במקום לחפש בכל פריט, קופצים ישר לתא בו המקור שייך.
A hash function
- A hash function takes a key and returns a slot index from
0tosize - 1. - The same key always gives the same slot, so you can find it again later.
- A simple one: add the character codes, then take
% sizeto stay in range.
פונקציית חשיפה
- פונקציית חשיפה לוקחת מקור ומחזירה אינדקס תא מ
0עדsize - 1. - אותו מקור תמיד נותן אותו תא, כך שתוכלו למצוא אותו שוב מאוחר יותר.
- פשוטה: סכמו קודי האותיות, ואז לקחו
% sizeכדי להישאר בטווח.
def hash_key(key, size):
total = 0
for ch in key:
total += ord(ch) # ord("A") is 65, ord("B") is 66, ...
return total % size
print(hash_key("cat", 10)) # a slot from 0 to 9
print(hash_key("cat", 10)) # same key -> same slot
print(hash_key("dog", 10))
Collisions
- Two different keys can hash to the same slot. That is a collision.
- A table has limited slots, so collisions are unavoidable as it fills up.
- We need a rule for what to do when the slot we want is already taken.
התנגשויות
- שני מקורות שונים יכולים לחשוף לאותו תא. זהו התנגשות.
- לטבלה יש מספר מוגבל של תאים, ולכן התנגשויות בלתי נמנעות כשהיא מתמלאה.
- אנחנו זקוקים לכלל עבור מה לעשות כאשר התא הרצוי תפוס כבר.
Linear probing
- Linear probing: if a slot is full, try the next slot, then the next, wrapping around.
- Keep stepping
(slot + 1) % sizeuntil you find a free slot. - Below,
AandFboth want slot 0, soFis pushed to slot 1.
סריקה ליניארית
- חיפוש ליניארי: אם תא מלא, נסו את התא הבא, ואז את הבא, עם חזרה למתחילת הטבלה.
- המשיכו לצעוד
(slot + 1) % sizeעד שתמצאו תא פנוי. - למטה,
Aו-Fשואפים לתא 0, לכןFדוחק לתא 1.
def hash_key(key, size):
total = 0
for ch in key:
total += ord(ch)
return total % size
def insert(table, key):
slot = hash_key(key, len(table))
while table[slot] is not None: # slot taken -> try the next one
slot = (slot + 1) % len(table)
table[slot] = key
return slot
table = [None] * 5
print(insert(table, "A")) # 0
print(insert(table, "F")) # 1 (A and F both hash to slot 0)
print(table) # ['A', 'F', None, None, None]
Looking up a key
- To find a key: hash it, then probe forward, comparing each slot to the key.
- Stop and return the index when you find it.
- If you reach an empty slot (or check every slot), the key is not there — return
-1.
חיפוש מפתח
- כדי למצוא מפתח: חשבו אותו באמצעות פונקציית TODO, ולאחר מכן חפשו קדימה, בהשוואת כל תא למפתח.
- עצרו והחזירו את האינדקס כאשר תמצאו אותו.
- אם הגעתם לתא פנוי (או בדקתם כל תא), המפתח אינו קיים — החזירו
-1.
Why hash tables are fast
- With few collisions, insert and find take about one step — we call this
O(1). - As the table fills, probing gets longer, so it is wise to keep some slots free.
- In the worst case (everything collides) it slows to a linear scan,
O(n).
מדוע טבלאות TODO מהירות
- עם מעט התנגשויות, הוספה ומציאה לוקחות כ-שלב אחד — אנו מכנים זאת
O(1). - ככל שהטבלה מתמלאת, החיפוש נמשך זמן רב יותר, ולכן מומלץ לשמור על כמה תאים פנויים.
- במקרה הגרוע (הכל מתנגש) המהירות יורדת לסריקה ליניארית,
O(n).
Common mistakes
- A hash function maps a key to an index in the table.
- Two keys can collide at the same index — handle it, for example by chaining.
טעויות נפוצות
- פונקציית TODO ממפה מפתח לאינדקס בטבלה.
- שני מפתחות יכולים להתנגש באותו אינדקס — טפל בכך, לדוגמה באמצעות קישוריות.
Now you try
- Build the three parts: the hash function, insert with probing, and find.
- Each task checks your function on collisions and wrap-around cases.
- Press Check answer to test it.
כעת תנסו בעצמכם
- בנה את שלושת החלקים: פונקציית TODO, הוספה עם חיפוש ליניארי, ומציאה.
- כל משימה בודקת את הפונקציה שלך במקרי התנגשות ומקרי חזרה למתחילת הטבלה.
- לחץ על בדיקת תשובה כדי לבדוק אותה.
Hashing to a bucket · השמשה למיכל
A hash function sends each key to a bucket; clashes chain. · פונקציית גיבול שולחת כל מפתח למיכל; התנגשויות יוצרות שרשרת.
Write hash_key(key, size). Add the character codes of key (use ord(ch)) and return the total % size, so the result is a slot from 0 to size - 1. Example: hash_key("AB", 10) is (65 + 66) % 10 = 1. The empty string gives 0. · כתוב hash_key(key, size). הוסף את קודי האותיות של key (השתמש בord(ch)) והחזר את הסכום % size, כך שהתוצאה תהיה מקום בטוח בין 0 ל-size - 1. דוגמה: hash_key("AB", 10) הוא (65 + 66) % 10 = 1. המחרוזת ריקה נותנת 0.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
hash_key is provided. Write insert(table, key) using linear probing: go to the key's hash slot; while that slot is full (not None), step to (slot + 1) % len(table); put the key in the first free slot and return its index. · hash_key מסופק. כתוב insert(table, key) באמצעות חיפוש ליניארי: עבר למקום הטאבל של הגיבול; בזמן שמקום זה תפוס (לא None), צעד ל(slot + 1) % len(table); השתם את המפתח במקום הפנוי הראשון וחזור על מדדיו.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
hash_key is provided. Write find(table, key) that returns the index of key, or -1 if it is missing. Start at the hash slot and probe forward, comparing each slot. Stop at an empty slot (key not there). Make sure it ends even if the table is full — never loop forever. · hash_key מסופק. כתוב find(table, key) שמחזיר את מדד ה-key, או -1 אם הוא חסר. התחל במיקום הגיבול וחקור קדימה, השווה כל מקום. עצור במקום פנוי (המפתח אינו שם). ודא שהפונקציה תסתיים גם אם הטאבל מומלא — לעולם לא להילך בחיסור אינסופי.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.