Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר.

Similar presentations


Presentation on theme: "1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר."— Presentation transcript:

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

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

3 3 Data Structures, CS, TAU, RB-Tree3 * מס’ ליד צומת הוא גובה שחור של אותו צומת NIL 3 77 10 NIL 71215 NIL 716 NIL 714 2 11 1 1 1 Lemma 14.1 (CLR): A red-black tree with n internal nodes has height at most 2lg(n+1) דוגמא לעץ אדום-שחור

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

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

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

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

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

9 9 Data Structures, CS, TAU, RB-Tree9 צומתּ ואבא אדומים;ׂדוד אדום – Case I החלף צבעים בין סבא לבניו x C B DA C    x y  D  B   A  new x C A DB   y  A    B C D Uncle red. Cannot shift weight to the right Roll it upwards Color flip BLACK PROPERTY NOT HURT

10 10 Data Structures, CS, TAU, RB-Tree10 דוד שחור – Case II Left_Rotate(A), Right_Rotate(B) + החלף צבעים B  C y Case II C B A    y C A B   x δ δ x A   B C Case III x δ  כל התכונות נשמרות  בעיה עלתה למעלה (רק Case I) Uncle red. Weight lack on right. Shift to right!

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

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

13 13 Data Structures, CS, TAU, RB-Tree13 ביצוע Delete 1) מבצעים delete כמו בעץ חיפוש בינארי רגיל Y X Y - מושמט X - בנו 2) אם Y היה אדום  כל תכונות RB נשמרו. עצור. 3) אם Y היה שחור בצע RB_DELETE_FIXUP(X) 3א) אם X אדום צבע בשחור וסיים X single son! What about the 2 nd son of Y??

14 14 Data Structures, CS, TAU, RB-Tree14 Delete Fixup X - שחור X - מקום שמעליו בוצעה השמטה - נחשוב על X כעל מכיל שחור + שחור חסר (שחור כהה = חסר צמת!!!)  כל מסלול בעץ הוא חוקי מטרה: להיפטר מהשחור המיותר (= להוסיף שם צמת) יהי W אח של X ל- X יש אח כי X הוא שחור (כפול)! B AD E C    x w Who said x has a brother??

15 15 Data Structures, CS, TAU, RB-Tree15 דוגמה Delete Fixup - מקרים Case 1 W אדום Case 2 W שחור + 2 בנים שחורים B AD E C    x w

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

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

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

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

20 20 Data Structures, CS, TAU, RB-Tree20 דוגמה Delete Fixup - מקרים Case 4 W שחור + בן ימני אדום Case 3 W שחור + בן שמאלי אדום + בן ימני שחור  מצב 4

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

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

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

24 24 Data Structures, CS, TAU, RB-Tree24 Demo http://www.seanet.com/users/arsen/avltree.html

25 25 Data Structures, CS, TAU, RB-Tree25 Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

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

27 27 Data Structures, CS, TAU, RB-Tree27 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that all items in L2 are greater than all items in L1. split(x,L) : returns two lists one with all item less than or equal to x and the other with all items greater than x.

28 28 Data Structures, CS, TAU, RB-Tree28 Red Black trees - example

29 29 Data Structures, CS, TAU, RB-Tree29 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 ?

30 30 Data Structures, CS, TAU, RB-Tree30 דוד אדום – Case I החלף צבעים בין סבא לבניו x C B DA C    x y  D  B   A  new x C A DB   y  A    B C D Uncle red. Cannot shift weight to the right Roll it upwards Color flip

31 31 Data Structures, CS, TAU, RB-Tree31 Insert - analysis Each time we do a color-flip-step (roll upwards) 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.

32 32 Data Structures, CS, TAU, RB-Tree32 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 ?

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

34 34 Data Structures, CS, TAU, RB-Tree34 Delete + insert -- analysis del====> ins===> All the operations where the problem moves upwards

35 35 Data Structures, CS, TAU, RB-Tree35 Delete + insert -- analysis  (tree) = #() + 2 #() ==> amortized(delete) = O(1) amortized(insert) = O(1) sequence of m delete and inserts, starting from an empty tree takes O(m) time Why 2? … Next slide

36 36 Data Structures, CS, TAU, RB-Tree36 Delete + insert -- analysis - - ====> ===> Decr 2 + may incr 1 (blk triple)  total = decr 1

37 37 Data Structures, CS, TAU, RB-Tree37 Concatenation + = 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 T1 T2

38 38 Data Structures, CS, TAU, RB-Tree38 Concatenation (cont) + = T1 T2 x Make y a child of p(x) Continue as for insert y Allocate y. make the root of T1 and x children of y. Color y red This is for a model where values are in the leaves! So y is a new separator. In our model more complicated. Can be done by inserting y=x and then deleting x

39 39 Data Structures, CS, TAU, RB-Tree39 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

40 40 Data Structures, CS, TAU, RB-Tree40 Split x 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

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

42 42 Data Structures, CS, TAU, RB-Tree42 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

43 43 Data Structures, CS, TAU, RB-Tree43 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)

44 44 Data Structures, CS, TAU, RB-Tree44 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)

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

46 46 Data Structures, CS, TAU, RB-Tree46 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.


Download ppt "1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר."

Similar presentations


Ads by Google