420 likes | 562 Views
עץ אדום -שחור. (CLR chapter 14). עץ חיפוש בינארי. בכל צומת ביט אינפורמציה נוסף - צבע. צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר. עץ “כמעט מאוזן” ( O(log n במקרה גרוע ביותר. מבנה – כל צומת מכיל:. parent left right color key. תכונות (צבעים: מנגנון חשבונאות).
E N D
עץ אדום-שחור (CLR chapter 14) • עץ חיפוש בינארי • בכל צומת ביט אינפורמציה נוסף - צבע • צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר • עץ “כמעט מאוזן” • (O(log nבמקרה גרוע ביותר • מבנה – כל צומת מכיל: • parent • left • right • color • key Data Structures, CS, TAU, RB-Tree
תכונות (צבעים: מנגנון חשבונאות) גובה שחור:מס’ הצמתים השחורים מצומת לעלה (לא כולל הצומת). BH(T) סימון: 1. כל צומת אדום או שחור 2. כל עלה (NIL) הוא שחור-> לכל צומת פנימי יש שני ילדים 3. אם צומת אדום שני ילדיו שחורים 4. כל מסלול פשוט מצומת לעלה מכיל מספר זהה של צמתים שחורים Data Structures, CS, TAU, RB-Tree
דוגמא לעץ אדום-שחור 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
הוכחת הלמה הוכחה:טענה אינדוקציה הנח נכונות לילדים, אזי: הפעל על שורש • אינטואיציה: • רוב הצמתים שחורים • גובה שחור - אחיד: • מבחינת שחורים עץ מאוזן log • אדומים - רק תוספת (לכל היותר מכפילים כל מסלול) Data Structures, CS, TAU, RB-Tree
שמירת איזונים: רוטציות של עץ בינארי Right_Rotate(T,y) Y X X Y גדול קטן קטן אמצע אמצע גדול Left_Rotate(T,x) • עומק של עולה/יורד ב- 1 • עומק של אינו משתנה Data Structures, CS, TAU, RB-Tree
השפעת רוטציה על עומק העץ 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
ביצוע Insert 1. הכנסת Xבמקום המתאים (כמו בעץ בינארי) ובצבע אדום 2. ביצוע תיקונים מלמטה למעלה - אם יש שני אדומים רציפים Data Structures, CS, TAU, RB-Tree
תיקון צבעים ב-Insert • בתיקון: א) הצפת אדום האב כלפי מעלה (העברת הבעיה כלפי מעלה) או: ב) השכנת הבעיה במסגרת משפ’ הסבא ע”י התקזזות עם הדוד (עצירת הבעיה) אם צומת אדום ואביו אדום האח שחור • תיקון אם צומת אדום וגם אביו אדום Data Structures, CS, TAU, RB-Tree
דוד אדום – 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
דוד שחור – 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) + החלף צבעיםBC y Data Structures, CS, TAU, RB-Tree
דוגמא ל-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
דוגמא ל-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
ביצוע Delete Y - מושמט X - בנו Y X 3) אם Yהיה שחור בצע RB_DELETE_FIXUP(X) 3א) אם Xאדום צבע בשחור וסיים 1) מבצעים deleteכמו בעץ חיפוש בינארי רגיל 2) אם Yהיה אדום כל תכונות RB נשמרו. עצור. Data Structures, CS, TAU, RB-Tree
דוגמה Delete Fixup ל- Xיש אח כי Xהוא שחור (כפול)! יהי Wאח של X X - שחור X - מקום שמעליו בוצעה השמטה - נחשוב על Xכעל מכילשחור + שחור חסר(שחור כהה = חסר צמת!!!) כל מסלול בעץ הוא חוקי מטרה: להיפטר מהשחור המיותר (= להוסיף שם צמת) Data Structures, CS, TAU, RB-Tree
דוגמה 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
דוגמה 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
דוגמה 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
דוגמה 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
דוגמה Delete Fixup - מקרים Wשחור + בן שמאלי אדום + בן ימני שחור Case 3 החלף צבעים בין Wובנו השמאלי ובצע Right_Rotate מטפל בבעיה וגורם ללולאה להסתיים Case 4 Wשחור + בן ימני אדום שנה צבעים ו- Left_Rotate(P(X)) מצב 4 Data Structures, CS, TAU, RB-Tree
דוגמה 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
דוגמה 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
דוגמה 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
Demo • http://www.seanet.com/users/arsen/avltree.html Data Structures, CS, TAU, RB-Tree
Red Black Trees(Guibas Sedgewick 78)CLRS: Chapter 13 Data Structures, CS, TAU, RB-Tree
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
Red Black trees - example Data Structures, CS, TAU, RB-Tree
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
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
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
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
Delete + insert -- analysis - ====> - ===> ===> Data Structures, CS, TAU, RB-Tree
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
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
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
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
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
Split -- analysis. Can prove that split takes O(log n) time. Data Structures, CS, TAU, RB-Tree
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
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
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
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
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