Upload
whitley
View
47
Download
6
Embed Size (px)
DESCRIPTION
עץ אדום -שחור. (CLR chapter 14). עץ חיפוש בינארי. בכל צומת ביט אינפורמציה נוסף - צבע. צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר. עץ “כמעט מאוזן” ( O(log n במקרה גרוע ביותר. מבנה – כל צומת מכיל:. parent left right color key. תכונות (צבעים: מנגנון חשבונאות). - PowerPoint PPT Presentation
Citation preview
Data Structures, CS, TAU, RB-Tree 1 1
-שחוראדוםעץ
עץ חיפוש בינארי
בכל צומת ביט אינפורמציה נוסף - צבע
”עץ “כמעט מאוזן ( O(log nבמקרה גרוע ביותר
צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר
:מבנה – כל צומת מכיל
• parent• left• right• color• key
(CLR chapter 14(
Data Structures, CS, TAU, RB-Tree 2 2
. כל צומת אדום או שחור1
-< לכל צומת פנימי יש שני ילדים ) הוא שחורNIL. כל עלה )2
שני ילדיו שחורים אדום. אם צומת 3
. כל מסלול פשוט מצומת לעלה מכיל מספר זהה של צמתים 4שחורים
מס’ הצמתים השחורים מצומת לעלה )לא כולל גובה שחור:הצומת).
)BH(T סימון:
תכונות )צבעים: מנגנון חשבונאות)
Data Structures, CS, TAU, RB-Tree 3 3
* מס’ ליד צומת הוא גובה שחור של אותו צומת
NIL NIL
3 NIL
77
10
NIL NIL
712 15
NIL NIL
716
NIL
7142
11
1
1
1
Lemma 14.1 (CLR(:
A red-black tree with n internal nodes has height at most 2lg(n+1(
-שחוראדוםדוגמא לעץ
Data Structures, CS, TAU, RB-Tree 4 4
אינטואיציה:
רוב הצמתים שחורים
:גובה שחור - אחיד
logמבחינת שחורים עץ מאוזן
רק תוספת )לכל היותר מכפילים כל מסלול) אדומים -
טענה הוכחה:
אינדוקציה
הנח נכונות לילדים,
אזי:
הוכחת הלמה
12)(int# )( xBHxnodes
)1lg(21212 2/ NhN hbh
12)12(21)(int# )(1)( xBHxBHxnodesהפעל על שורש
Data Structures, CS, TAU, RB-Tree 5 5
קטן
X
Y
Y
X
Right_Rotate(T,y(
Left_Rotate(T,x(
גדול
גדול
קטן
אמצע אמצע
עולה/יורד ב- עומק של •
1 אינו משתנה עומק של •
ירת איזונים: רוטציות של עץ בינארישמ
Data Structures, CS, TAU, RB-Tree 6 6
7
11
9 18
14 19
22
20
12 17
x
y
7
11
9
18
14
19
22
2012 17
x
y
Left_Rotate(T,x(
Inorder doesn’t change!
השפעת רוטציה על עומק העץ
Data Structures, CS, TAU, RB-Tree 7 7
אדום במקום המתאים )כמו בעץ בינארי) ובצבע X. הכנסת 1
רציפיםאדומים. ביצוע תיקונים מלמטה למעלה - אם יש שני 2
Insertביצוע
Data Structures, CS, TAU, RB-Tree 8 8
Insertתיקון צבעים ב-
אדום וגם אביו אדום תיקון אם צומת
:בתיקון
האב כלפי מעלה )העברת הבעיה כלפי אדוםא) הצפת
מעלה)ב) השכנת הבעיה במסגרת משפ’ הסבא ע”י התקזזות עם
הדוד )עצירת הבעיה)
או:
אדום ואביו אדום אם צומת
האח שחור
Data Structures, CS, TAU, RB-Tree 9 9
Case I – אדוםדוד
החלף צבעים בין סבא לבניו
x
C
B
DA
C
xy
D
B
A
new x
C
A
DB
y
A
B
C new x
D
Data Structures, CS, TAU, RB-Tree 10 10
Case IIדוד שחור –
Left_Rotate(A(, Right_Rotate(B( +
BC החלף צבעים
y
Case IIC
B
A
y
C
A
B
x
δ δ
x
A
B
C
Case III
x
δ
כל התכונות נשמרות בעיה עלתה למעלה )רק Case I(
Data Structures, CS, TAU, RB-Tree 11 11
Insertדוגמא ל-
11
2 14
15 1
4
x
y
Case II
7
8 5
11
2 14
15 1 7
4
5 8
x
y Case I
:Node X was now inserted
Data Structures, CS, TAU, RB-Tree 12 12
)המשך)Insertדוגמא ל-
Case III
x
11
214
15 1
4
y7
8
5
x 2
14
15
1
4
8 5
7
11
Data Structures, CS, TAU, RB-Tree 13 13
Deleteביצוע
כמו בעץ חיפוש בינארי רגילdelete) מבצעים 1
Y
X
Yמושמט -
Xבנו -
נשמרו.RB כל תכונות היה אדום Y) אם 2
עצור.
היה שחור בצע Y) אם 3
RB_DELETE_FIXUP(X(
אדום צבע בשחור וסייםXא) אם 3
Data Structures, CS, TAU, RB-Tree 14 14
Delete Fixupדוגמה
Xשחור -
Xמקום שמעליו בוצעה השמטה -
)שחור כהה = חסר צמת!!!) כעל מכיל שחור + שחור חסרX- נחשוב על
כל מסלול בעץ הוא חוקי
להיפטר מהשחור המיותר )= להוסיף שם צמת)מטרה:
הוא שחור )כפול)!X יש אח כי Xל- X אח של Wיהי
Data Structures, CS, TAU, RB-Tree 15 15
- מקריםDelete Fixupדוגמה
)P(X והוסף ל- X ו- Wהורד שחור מ-
X שחור בודד
W אדום
P(x נהפך לשחור או לדו-שחור)אדום שחור או(
))Left_Rotate(P(X ו-P(X(, Wהחלף צבעי
-מוליך ל Case 1,2,3,4 כל המסלולים לתתי עצים באותו גובה שחור
Case 1W אדום
Case 2W + בנים שחורים2 שחור
Data Structures, CS, TAU, RB-Tree 16 16
Delete Fixupדוגמה
B
B
A D
E C
E
C
D
A
Case 1x w
new w
x
))Left_Rotate(P(X ו-P(X(, Wהחלף צבעי
-מוליך ל Case 1,2,3,4 כל המסלולים לתתי עצים באותו גובה שחור
Case 1W אדום
Data Structures, CS, TAU, RB-Tree 17 17
Delete Fixupדוגמה
A
E
Case 2B
D
C
A D
E C
Bx w
c c, new x/black
)P(X והוסף ל- X ו- Wהורד שחור מ-
X שחור בודד
W אדום
P(x נהפך לשחור או לדו-שחור)אדום שחור או(
Case 2W + בנים שחורים2 שחור
Data Structures, CS, TAU, RB-Tree 18 18
Delete Fixupדוגמה
B
B
A D
E C
E
C
D
A
Case 1x w
new w
x
A
E
Case 2B
D
C
A D
E C
Bx w
c c, new x/black
Data Structures, CS, TAU, RB-Tree 19 19
- מקריםDelete Fixupדוגמה
Case 4W אדום שחור + בן ימני
))Left_Rotate(P(Xשנה צבעים ו-
מטפל בבעיה וגורם ללולאה להסתיים
Case 3W בן ימני שחוראדום שחור + בן שמאלי +
Right_Rotate ובנו השמאלי ובצעWהחלף צבעים בין
4 מצב
Data Structures, CS, TAU, RB-Tree 20 20
Delete Fixupדוגמה
A
E
Case 3B
D
C
A
E
B
D
C
x w
c
xnew w
c
Case 3W בן ימני שחוראדום שחור + בן שמאלי +
Right_Rotate ובנו השמאלי ובצעWהחלף צבעים בין
מצב
4
Data Structures, CS, TAU, RB-Tree 21 21
Delete Fixupדוגמה
A
B
D
Case 4
C E
E
A
D
B
C
x w
c c
‘c
Case 4W אדום שחור + בן ימני
))Left_Rotate(P(Xשנה צבעים ו-
מטפל בבעיה וגורם ללולאה להסתיים
Data Structures, CS, TAU, RB-Tree 22 22
Delete Fixupדוגמה
A
E
Case 3B
D
C
A
E
B
D
C
x w
c
xnew w
c
A
B
D
Case 4
C E
E
A
D
B
C
x w
c c
‘c
Data Structures, CS, TAU, RB-Tree 23 23
Demo
• http://www.seanet.com/users/arsen/avltree.html
Data Structures, CS, TAU, RB-Tree 24 24
Red Black Trees(Guibas Sedgewick 78)
CLRS: Chapter 13
Data Structures, CS, TAU, RB-Tree 25 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 27 27
Red Black trees - example
Data Structures, CS, TAU, RB-Tree 28 28
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 29 29
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 30 30
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 31 31
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 32 32
Delete + insert -- analysis
--
====>
===>
===>
Data Structures, CS, TAU, RB-Tree 33 33
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
Data Structures, CS, TAU, RB-Tree 34 34
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
Data Structures, CS, TAU, RB-Tree 35 35
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
Data Structures, CS, TAU, RB-Tree 36 36
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 37 37
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
Data Structures, CS, TAU, RB-Tree 38 38
Split -- analysis.
Can prove that split takes O(log n) time.
Data Structures, CS, TAU, RB-Tree 39 39
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 40 40
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)
12/12/ 2)1(2)( hh nhn
)(lg2lg2 nOnh
Data Structures, CS, TAU, RB-Tree 41 41
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 42 42
Demo
דוגמה לשאלה:• )כל Cormenהתייחסו לעץ אדום שחור כפי שנלמד בכיתה ומתואר ב-. 1•
מוגדר y נחשבים כעלים וצבעם הוא שחור(. יהי הגובה של צומת Nilsה-. הספירה לא y)נספר בצמתים( הארוך ביותר מעלה ל-כאורך המסלול
שימו לב: הכוונה ) שמושמט צומת x הנידון. יהי nil ואת ה-yכוללת את כמו (deleteבפעולת ה-לצומת המושמט ולא לערך המושמט)
לפני xשנלמד בכיתה בעץ חיפוש בינארי(. הננו מעוניינים בגובהו של ההשמטה. אזי:
(.0 )כלומר גובה 0 הינו xא( החסם העליון ההדוק ביותר על גובהו של •(.1 )כלומר גובה 1 הינו xב( החסם העליון ההדוק ביותר על גובהו של •(.2 )כלומר גובה 2 הינו xג( החסם העליון ההדוק ביותר על גובהו של •(.3)כלומר גובה 3 הינו xד( החסם העליון ההדוק ביותר על גובהו של • .xה( אין חסם עליון על גובהו של ••
Data Structures, CS, TAU, RB-Tree 43 43
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.