המצגת נטענת. אנא המתן

המצגת נטענת. אנא המתן

Marina Kogan Sadetsky –

מצגות קשורות


מצגת בנושא: "Marina Kogan Sadetsky –"— תמליל מצגת:

1 Marina Kogan Sadetsky – 05.2015
יסודות מבני נתונים תרגול 7: עץ AVL(Adelson-Velsky and Landis') Marina Kogan Sadetsky –

2 עץ AVL x √ x עץ AVL – עץ חיפוש בינארי שבו לכל צומת v מתקיים:
ההפרש בין גובה תת העץ השמאלי של v לבין גובה תת העץ הימני של v הוא לכל היותר 1. |h(left[v]) – h(right[v])| ≤ 1 6 1 8 4 2 3 5 5 1 8 6 1 3 5 4 x x

3 Question 1 Suggest a data structure for storing integers that supports the following operations. Init() Initialize empty data structure O(1) Insert(x) Insert x, if it is not present yet O(log n) Delete(x) Delete x if it exists GetPlace(x) Return x’s place (according to the order of insertion). If x does not exist, return -1 DeletePlace(i) Delete the element in the ith place (according to the order of insertion) Insert(3), Insert(5), Insert(11), Insert(7), Insert(2) דוגמא: נשתמש בעץ AVL 2 5 11 3 7 GetPlace(7) מידע נוסף בעץ AVL של אינדקס הכנסה  returns 4 מיון לפי שני מפתחות צריך 2 עצי AVL הצבעה הדדית DeletePlace(2)  deletes 5

4 Question 1 We will use two AVL trees:
Solution: We will use two AVL trees: T1 stores the elements by their key T2 stores the elements by the order of insertion (using a running counter) two trees connect the nodes with the same key (by pointers) Insert(3), Insert(5), Insert(11), Insert(7), Insert(2) דוגמא: T1: 3 3 3 5 11 5 11 3 5 11 3 7 2 5 11 3 7 5 2 3 1 4 5 2 4 1 5 3 T2: 1 2 3 2 3 1 2 3 1 4 1 1 2

5 Question 1

6 Question 3 … דוגמא: 7 9 5 8 11 12 4 פתרון: נסרוק את העץ, רמה אחרי רמה:
(סריקה כזו נקראת BFS - Breadth First Search) queue queue queue queue queue queue dequeue (5) enqueue ( 6’s children) dequeue (4) enqueue ( 4’s children) ... 7 9 5 11 8 4 12 insert root dequeue (7) enqueue ( 7’s children) dequeue (12) enqueue ( 12’s children) dequeue (9) enqueue ( 9’s children) dequeue (11) enqueue ( 11’s children) {7} {7,5,9} {7,5,9,4,8,11} {7,5,9,4,8,11,12} נבנה את העץ מהרשימה שקיבלנו. נשים לב שהעץ גדל רמה רמה, ולכן אף פעם לא נצטרך לעדכן גובה. 7 7 7 7 7 5 5 9 5 9 5 9 4 8 11 4 8 11 12

7 Question 4 Suggest a data structure for storing clients by their id, and marked as ‘woman‘ or ‘man’, that supports the following operations. Insert(k) Insert a new client with id=k, and first mark them ‘woman’ O(log n) Update(k) Update client with id=k to be ‘man’ FindDiff(k) Find the difference between number of ‘woman’ and ‘man’ clients whose id < k Solution: נשתמש בעץ AVL 9 1 8 5 7 6 2 ‘man’ = 3 ‘woman’ = 4 FindDiff(5) =1-1=0 דוגמא: איך נענה לדרישה הזו ? 2 1 2 כל צומת צריך לדעת מראש כמה ‘man’ ו ‘woman’ יש בתת העץ שלו (כולל צומת עצמו). 1 1 1 ‘man’

8 Question 4 Suggest a data structure for storing clients by their id, and marked as ‘woman‘ or ‘man’, that supports the following operations. Insert(k) Insert a new client with id=k, and first mark them ‘woman’ O(log n) Update(k) Update client with id=k to be ‘man’ FindDiff(k) Find the difference between number of ‘woman’ and ‘man’ clients whose id < k Solution: נשתמש בעץ AVL 9 1 8 5 7 6 2 ‘man’ = 3 ‘woman’ = 4 FindDiff(5) =1-1=0 דוגמא: איך נענה לדרישה הזו ? 2 1 2 כל צומת צריך לדעת מראש כמה ‘man’ ו ‘woman’ יש בתת העץ שלו (כולל צומת עצמו). 1 1 1 ‘man’

9 Question 4

10 Question 4

11 Question 4

12 Question 4

13 Question 5 דוגמא: MinMax() = min{max(S1),max(S2),
max(S3)} = min{7,5,8} = 5 GlobalMax() = max{max(S1),max(S2), max(S3)} = max{7,5,8} = 8

14 Question 5 מותר להחזיק מספר קבוע של עותקים של אותו איבר. S2 S3 S1 Tsets m קבוצות, n מפתחות בקבוצה Insert(i,k) בזמן O(log n + log m) נחזיק אינדקסים של קבוצות בעץAVL Tsets כל צומת ב Tsets יצביע על הקבוצה שלו נחזיק כל קבוצה בעץ AVL נפרד TSi 4 7 2 TS1 1 6 8 3 TS2 5 TS3

15 Question 5 √ Tsets TS1 TS2 TS3 m קבוצות, n מפתחות בקבוצה
Delete(i,k) בזמן O(log n + log m) S2 S3 S1 Tsets TS1 TS2 TS3 4 6 5 2 7 3 8 1

16 Question 5 Tsets TS1 TS2 TS3 MinMax() בזמן O(1)
נחזיק משתנה עזר MMax עבור הערך העדכני של MinMax() Tsets MMax = 5 S2 כמה זמן יקח לנו לעדכן את הערך MMax ? S1 S3 O(m*log n + m) כי נצטרך לחשב את MinMax() מחדש. TS1 TS2 TS3 4 6 5 2 7 3 8 6 1

17 Question 5 √ Tmax MinMax() בזמן O(1)
MMax = 5 MinMax() בזמן O(1) נחזיק משתנה עזר MMax עבור הערך העדכני של MinMax() כמה זמן יקח לנו לעדכן את הערך MMax ? O(m*log n + m) כי נצטרך לחשב את MinMax() מחדש. נחזיק עץ AVL נוסף Tmax המכיל מקסימום של כל עץ TSi. ברגע שמכניסים איבר חדש x לעץ TSi, בודקים האם x גדול מהמקסימום (O(log n)), ואם כן, אז מוציאים מ Tmax את המקסימום הישן של TSi ומכניסים את x במקומו ל Tmax (O(log m)). מחפשים את המינימום החדש של Tmax (O(log m)) ומעדכנים את MMax במידת הצורך. 7 8 5 Tmax תוספת של O(log n + log m) זמן להכנסה

18 Question 5 √ Tmax GlobalMax() בזמן O(1)
MMax = 5 GMax = 8 GlobalMax() בזמן O(1) נחזיק משתנה עזר GMax עבור הערך העדכני של GlobalMax() כמה זמן יקח לנו לעדכן את הערך GMax ? ברגע שמכניסים איבר חדש x לעץ TSi, בודקים האם x גדול מהמקסימום (O(log n)), ואם כן, אז מוציאים מ TSi את המקסימום הישן ומכניסים את המקסימום העדכני של TSi(O(log m)). מחפשים את המקסימום החדש של Tmax (O(log m)) ומעדכנים את GMax במידת הצורך. ברגע שמכניסים איבר חדש x לעץ TSi, בודקים האם x גדול מהמקסימום (O(log n)), ואם כן, אז מוציאים מ Tmax את המקסימום הישן של TSi ומכניסים את x במקומו ל Tmax (O(log m)). מחפשים את המקסימום החדש של Tmax (O(log m)) ומעדכנים את GMax במידת הצורך. Tmax 7 תוספת של O(log n + log m) זמן להכנסה 5 8

19 Question 5

20 Question 5

21 Question 5

22 Question 2 Suggest efficient way to implement Join (T1, x, T2). T1 and T2 are AVL trees, and x is an element so that T1< x < T2 . T1 T2 x דוגמא: ? איך לחבר בין שני עצים כך שהתיקון גובה יקח זמן מינימלי ? צריך לעשות תיקון גובה נמצא תת עץ השמאלי ביותר ב 2T עם גובה של 1T נחליף את השורש שלו ב x נחבר אליו את 1T משמאל ואת תת העץ 2T מימין נשים לב שהגובה של העץ הסופי שנוצר הינו 1+ הגובה של העץ T2, לכן אנחנו יכולים להפעיל אלגוריתם תיקון גובה (כמו בהכנסה).

23 Question 2

24 Question 2


הורד את "ppt "Marina Kogan Sadetsky –

מצגות קשורות


מודעות Google