1 / 42

עץ אדום -שחור

עץ אדום -שחור. (CLR chapter 14). עץ חיפוש בינארי. בכל צומת ביט אינפורמציה נוסף - צבע. צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר. עץ “כמעט מאוזן” ( O(log n במקרה גרוע ביותר. מבנה – כל צומת מכיל:. parent left right color key. תכונות (צבעים: מנגנון חשבונאות).

whitley
Download Presentation

עץ אדום -שחור

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. עץ אדום-שחור (CLR chapter 14) • עץ חיפוש בינארי • בכל צומת ביט אינפורמציה נוסף - צבע • צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר • עץ “כמעט מאוזן” • (O(log nבמקרה גרוע ביותר • מבנה – כל צומת מכיל: • parent • left • right • color • key Data Structures, CS, TAU, RB-Tree

  2. תכונות (צבעים: מנגנון חשבונאות) גובה שחור:מס’ הצמתים השחורים מצומת לעלה (לא כולל הצומת). BH(T) סימון: 1. כל צומת אדום או שחור 2. כל עלה (NIL) הוא שחור-> לכל צומת פנימי יש שני ילדים 3. אם צומת אדום שני ילדיו שחורים 4. כל מסלול פשוט מצומת לעלה מכיל מספר זהה של צמתים שחורים Data Structures, CS, TAU, RB-Tree

  3. דוגמא לעץ אדום-שחור 14 7 2 10 16 7 1 7 7 12 7 15 1 1 1 NIL 3 NIL NIL NIL NIL NIL 1 NIL NIL * מס’ ליד צומת הוא גובה שחור של אותו צומת Lemma 14.1 (CLR): A red-black tree with ninternal nodeshas height at most 2lg(n+1) Data Structures, CS, TAU, RB-Tree

  4. הוכחת הלמה הוכחה:טענה אינדוקציה הנח נכונות לילדים, אזי: הפעל על שורש • אינטואיציה: • רוב הצמתים שחורים • גובה שחור - אחיד: • מבחינת שחורים עץ מאוזן log • אדומים - רק תוספת (לכל היותר מכפילים כל מסלול) Data Structures, CS, TAU, RB-Tree

  5. שמירת איזונים: רוטציות של עץ בינארי Right_Rotate(T,y) Y X X Y   גדול קטן       קטן אמצע אמצע גדול Left_Rotate(T,x) • עומק של  עולה/יורד ב- 1 • עומק של  אינו משתנה    Data Structures, CS, TAU, RB-Tree

  6. השפעת רוטציה על עומק העץ x 7 7 y 11 11 9 9 18 17 22 19 14 18 19 22 20 20 12 17 12 14 Left_Rotate(T,x) y x Inorder doesn’t change! Data Structures, CS, TAU, RB-Tree

  7. ביצוע Insert 1. הכנסת Xבמקום המתאים (כמו בעץ בינארי) ובצבע אדום 2. ביצוע תיקונים מלמטה למעלה - אם יש שני אדומים רציפים Data Structures, CS, TAU, RB-Tree

  8. תיקון צבעים ב-Insert • בתיקון: א) הצפת אדום האב כלפי מעלה (העברת הבעיה כלפי מעלה) או: ב) השכנת הבעיה במסגרת משפ’ הסבא ע”י התקזזות עם הדוד (עצירת הבעיה)  אם צומת אדום ואביו אדום  האח שחור • תיקון אם צומת אדום וגם אביו אדום Data Structures, CS, TAU, RB-Tree

  9. דוד אדום – Case I new x y A B C D D C   x         D B B A A A C B D C new x y           החלף צבעים בין סבא לבניו x Data Structures, CS, TAU, RB-Tree

  10. דוד שחור – Case II Case II C δ δ C B δ y x   x B B A C A A     Case III x    • כל התכונות נשמרות • בעיה עלתה למעלה (רק Case I) Left_Rotate(A), Right_Rotate(B) + החלף צבעיםBC y Data Structures, CS, TAU, RB-Tree

  11. דוגמא ל-Insert :Node X was now inserted 11 14 5 8 1 14 1 7 11 y Case I 4 8 5 2 15 2 15 7 4 x y x Case II Data Structures, CS, TAU, RB-Tree

  12. דוגמא ל-Insert (המשך) y 11 5 14 14 8 8 5 1 1 7 x Case III 2 2 15 4 4 7 15 11 x Data Structures, CS, TAU, RB-Tree

  13. ביצוע Delete Y - מושמט X - בנו Y X 3) אם Yהיה שחור בצע RB_DELETE_FIXUP(X) 3א) אם Xאדום צבע בשחור וסיים 1) מבצעים deleteכמו בעץ חיפוש בינארי רגיל 2) אם Yהיה אדום  כל תכונות RB נשמרו. עצור. Data Structures, CS, TAU, RB-Tree

  14. דוגמה Delete Fixup ל- Xיש אח כי Xהוא שחור (כפול)! יהי Wאח של X X - שחור X - מקום שמעליו בוצעה השמטה - נחשוב על Xכעל מכילשחור + שחור חסר(שחור כהה = חסר צמת!!!)  כל מסלול בעץ הוא חוקי מטרה: להיפטר מהשחור המיותר (= להוסיף שם צמת) Data Structures, CS, TAU, RB-Tree

  15. דוגמה Delete Fixup - מקרים Wאדום Wשחור + 2 בנים שחורים Case 1 Case 2 החלף צבעי P(X), Wו- Left_Rotate(P(X))  מוליך ל-Case 1,2,3,4  כל המסלולים לתתי עצים באותו גובה שחור הורד שחור מ- Wו- Xוהוסף ל- P(X) Xשחור בודד Wאדום P(xשחור או אדום (נהפך לשחור או לדו-שחור) Data Structures, CS, TAU, RB-Tree

  16. דוגמה Delete Fixup Wאדום Case 1 Case 1 x w E C D A E C A B             B D new w החלף צבעי P(X), Wו- Left_Rotate(P(X))  מוליך ל-Case 1,2,3,4  כל המסלולים לתתי עצים באותו גובה שחור x Data Structures, CS, TAU, RB-Tree

  17. דוגמה Delete Fixup Wשחור + 2 בנים שחורים Case 2 הורד שחור מ- Wו- Xוהוסף ל- P(X) Xשחור בודד Wאדום P(xשחור או אדום (נהפך לשחור או לדו-שחור) c c, new x/black B Case 2 B x w D A A D   C E   C E         Data Structures, CS, TAU, RB-Tree

  18. דוגמה Delete Fixup Case 1 x w A D C E B E A C             D B new w x c c, new x/black B Case 2 B x w D A A D   C E   C E         Data Structures, CS, TAU, RB-Tree

  19. דוגמה Delete Fixup - מקרים Wשחור + בן שמאלי אדום + בן ימני שחור Case 3 החלף צבעים בין Wובנו השמאלי ובצע Right_Rotate מטפל בבעיה וגורם ללולאה להסתיים Case 4 Wשחור + בן ימני אדום שנה צבעים ו- Left_Rotate(P(X)) מצב 4 Data Structures, CS, TAU, RB-Tree

  20. דוגמה Delete Fixup c c Case 3 x new w Wשחור + בן שמאלי אדום + בן ימני שחור w Case 3 x A E D C A E החלף צבעים בין Wובנו השמאלי ובצע Right_Rotate             B D C B מצב 4 Data Structures, CS, TAU, RB-Tree

  21. דוגמה Delete Fixup A A E B D מטפל בבעיה וגורם ללולאה להסתיים C B E D C c c Case 4 x Case 4 Wשחור + בן ימני אדום w שנה צבעים ו- Left_Rotate(P(X))             ‘c Data Structures, CS, TAU, RB-Tree

  22. דוגמה Delete Fixup c c Case 3 x new w w x E D A D B E A A E C A             D C C B D E C B B c c Case 4 x w             ‘c Data Structures, CS, TAU, RB-Tree

  23. Demo • http://www.seanet.com/users/arsen/avltree.html Data Structures, CS, TAU, RB-Tree

  24. Red Black Trees(Guibas Sedgewick 78)CLRS: Chapter 13 Data Structures, CS, TAU, RB-Tree

  25. We assume items at the leaves • Don’t show keys throughout the presentation, they are basically handled as before Data Structures, CS, TAU, RB-Tree

  26. Red Black trees - example Data Structures, CS, TAU, RB-Tree

  27. Insert - analysis O(log n) time worst case, since the height is O(log n) Suppose you start with an empty tree and do m insertions such that the point of insertion is given to you each time, how much time does it take ? Obviously O(mlog n), but maybe we can prove it cannot be that bad ? Data Structures, CS, TAU, RB-Tree

  28. Insert - analysis Each time we do a color-flip-step the number of red nodes decreases by one. (tree) = #red nodes Actual(insert) = O(1) + #color-flips-steps (insert) = O(1) - #color-flips-steps ==> amortized(insert) = O(1) and the sequence actually takes O(m) time. Data Structures, CS, TAU, RB-Tree

  29. Delete + insert -- analysis O(log n) time, since the height is O(log n) Suppose you start with an empty tree and do m insertions and deletions such that the point of insertion is given to you each time, how much time does it take ? Obviously O(mlog n), but maybe we can prove it cannot be that bad ? Data Structures, CS, TAU, RB-Tree

  30. Delete + insert - analysis The previous potential won’t do the trick (tree) = #red nodes Here are the transformation that we want to release potential Data Structures, CS, TAU, RB-Tree

  31. Delete + insert -- analysis - ====> - ===> ===> Data Structures, CS, TAU, RB-Tree

  32. Delete + insert -- analysis #( ) 2 #( ) + (tree) = ==> amortized(delete) = O(1) amortized(insert) = O(1) sequence of m delete and inserts, starting from an empty tree takes O(m) time Data Structures, CS, TAU, RB-Tree

  33. Concatenation T2 T1 + = Define the rank of a node v as the number of black nodes from v to a leaf . Assume T1 has a black root. Look on the left spine of T2 for a node x of the same rank as the root of T1 Data Structures, CS, TAU, RB-Tree

  34. Concatenation (cont) y Allocate y make the root of T1 and x children of y. Color y red Make y a child of p(x) Continue as for insert T2 T1 x + = Data Structures, CS, TAU, RB-Tree

  35. Concatenation (analysis) O(|r1-r2| + 1) = O(log n) worst case. If the right point on the spine of the taller tree is given then its O(1) amortized Data Structures, CS, TAU, RB-Tree

  36. Split Concatenate all trees to the left of the path from the root to x to the left tree T1 (including x). Concatenate all trees to the right of the path from the root to x to the right tree T2 x Data Structures, CS, TAU, RB-Tree

  37. Split -- analysis. Can prove that split takes O(log n) time. Data Structures, CS, TAU, RB-Tree

  38. AVL TREE • Similar to RB-tree (but different) • Counts the height of each sub-tree explicitly • PROPERTY: For sub-tree T: • | Height (Right(T)) – Height (Left(T)) | <= 1 H-2 H-1 Data Structures, CS, TAU, RB-Tree

  39. AVL - cont • n(h): the minimum number of internal nodes of an AVL tree of height h • n=1, 2 easy. • For n>2 an AVL at height h contains: • root • 1 sub-tree of height h-1 • 1 sub-tree of height h-2 •  n(h) = 1 + n(h-1) + n(h-2) •  n(h) > 2n(h-2) Data Structures, CS, TAU, RB-Tree

  40. AVL – principle of operation • Principles: • Insert + delete as in binary search tree. • After operation: the balance principle may be violated. • From bottom to top: • Update height • Examine balance • If balance is violated – violation by at most 1 •  correct by rotation. • 4 cases of rotation: LL RR LR RL (two of them symmetric to each other) Data Structures, CS, TAU, RB-Tree

  41. Demo • דוגמה לשאלה: • 1. התייחסו לעץ אדום שחור כפי שנלמד בכיתה ומתואר ב-Cormen (כל ה-Nils נחשבים כעלים וצבעם הוא שחור). יהי הגובה של צומת y מוגדר כאורך המסלול (נספר בצמתים) הארוך ביותר מעלה ל-y. הספירה לא כוללת את y ואת ה-nil הנידון. יהי x צומת שמושמט(שימו לב: הכוונה לצומת המושמט ולא לערך המושמט) בפעולת ה-delete (כמו שנלמד בכיתה בעץ חיפוש בינארי). הננו מעוניינים בגובהו של x לפני ההשמטה. אזי: • א) החסם העליון ההדוק ביותר על גובהו של x הינו 0 (כלומר גובה 0). • ב) החסם העליון ההדוק ביותר על גובהו של x הינו 1 (כלומר גובה 1). • ג) החסם העליון ההדוק ביותר על גובהו של x הינו 2 (כלומר גובה 2). • ד) החסם העליון ההדוק ביותר על גובהו של x הינו 3(כלומר גובה 3). • ה) אין חסם עליון על גובהו של x . Data Structures, CS, TAU, RB-Tree

  42. Demo • Solution: • Binary search tree  deleted node (N) is either a leaf, or has one child. • Leaf  depth 0. • One child: • Child CANNOT be BLACK and none of its descendants can be black (otherwise: depth black of N is not well defined). •  Child is red. • Grandchild cannot be red (two reds in a row are not allowed) • Grandchild cannot be black •  NO GRANDCHILD •  DPETH <= 1. Data Structures, CS, TAU, RB-Tree

More Related