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

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר.
1 Trees CLRS: chapter A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or black.
1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
1 Trees CLRS: chapter A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Data Structures, CS, TAU, Splay Tree Splay Tree  מימוש של עץ חיפוש בינארי  מטרה לדאוג ל- Amortized Time  פעולה בודדת יכולה לקחת O(N)  אבל כל רצף.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Data Structures, CS, TAU, Splay Tree 1 Splay Tree - עץ חיפוש בינארי - מטרה לדאוג ל - Amortized Time - פעולה בודדת יכולה לקחת O(N) - אבל כל רצף M פעולות.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Data Structures, CS, TAU, RB-Tree 1 עץ אדום-שחור - עץ חיפוש בינארי - בכל צומת ביט אינפורמציה נוסף - צבע « עץ “ כמעט מאוזן ” « (O(log n במקרה גרוע ביותר.
1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר.
1 - גמישות בבינאריות גמישות בעומק - עץ חיפוש בינארי: ממוצע O(log n) גרוע ביותר (O(n - היינו רוצים לשמור את העץ מאוזן תמיד Data Structures, CS, TAU
CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
1 Biased 2-b trees (Bent, Sleator, Tarjan 1980). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
1 Trees CLRS: chapter A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,
Data Structures Hanoch Levi and Uri Zwick March 2011 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
AA Trees.
Balanced Search Trees Modified from authors’ slides.
BCA-II Data Structure Using C
Red Black Trees
Finger search trees.
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Red-Black Trees.
AVL Tree.
Design and Analysis of Algorithms
Red-Black Trees Motivations
Data Structures, CS, TAU, Splay Tree
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
AVL-Trees.
Red-Black Trees.
Red Black Trees (Guibas Sedgewick 78)
Analysis of Algorithms CS 477/677
Red-black tree properties
Presentation transcript:

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

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

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

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

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

6 Data Structures, CS, TAU, RB-Tree x y x y Left_Rotate(T,x) Inorder doesn’t change! השפעת רוטציה על עומק העץ

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

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

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 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 Data Structures, CS, TAU, RB-Tree11 דוגמא ל-Insert x y Case II x y Case I :Node X was now inserted

12 Data Structures, CS, TAU, RB-Tree12 דוגמא ל-Insert (המשך) Case III x y x

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

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

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 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 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 Data Structures, CS, TAU, RB-Tree24 Demo

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

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 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 Data Structures, CS, TAU, RB-Tree28 Red Black trees - example

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 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 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 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 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 Data Structures, CS, TAU, RB-Tree34 Delete + insert -- analysis del====> ins===> All the operations where the problem moves upwards

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 Data Structures, CS, TAU, RB-Tree36 Delete + insert -- analysis - - ====> ===> Decr 2 + may incr 1 (blk triple)  total = decr 1

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 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 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 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 Data Structures, CS, TAU, RB-Tree41 Split -- analysis. Can prove that split takes O(log n) time.

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 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 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 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 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.