42
Data Structures, CS, TAU, RB-Tree 1 1 ץץ ץץץץץץץץ- ץץ ץץץץץ ץץץץץץ ץץץ- ץץץ ץץץץ ץץץ ץץץץץץץץץץ ץץץץ ץץ “ץץץץ ץץץץץ” ( O(log n ץץץץץ ץץץץץ ץץץץ ץץץץץ ץץץץץץ ץץץץ ץץץץץ ץץץץ ץץ ץץץץץ ץץץץ ץץץץ – ץץ ץץץץ ץץץץ:parent left right color key (CLR chapter 14(

עץ אדום -שחור

  • 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

Page 1: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 1 1

-שחוראדוםעץ

עץ חיפוש בינארי

בכל צומת ביט אינפורמציה נוסף - צבע

”עץ “כמעט מאוזן ( O(log nבמקרה גרוע ביותר

צביעה מבטיחה שאין מסלול ארוך פי שניים מאחר

:מבנה – כל צומת מכיל

• parent• left• right• color• key

(CLR chapter 14(

Page 2: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 2 2

. כל צומת אדום או שחור1

-< לכל צומת פנימי יש שני ילדים ) הוא שחורNIL. כל עלה )2

שני ילדיו שחורים אדום. אם צומת 3

. כל מסלול פשוט מצומת לעלה מכיל מספר זהה של צמתים 4שחורים

מס’ הצמתים השחורים מצומת לעלה )לא כולל גובה שחור:הצומת).

)BH(T סימון:

תכונות )צבעים: מנגנון חשבונאות)

Page 3: עץ  אדום -שחור

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(

-שחוראדוםדוגמא לעץ

Page 4: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 4 4

אינטואיציה:

רוב הצמתים שחורים

:גובה שחור - אחיד

logמבחינת שחורים עץ מאוזן

רק תוספת )לכל היותר מכפילים כל מסלול) אדומים -

טענה הוכחה:

אינדוקציה

הנח נכונות לילדים,

אזי:

הוכחת הלמה

12)(int# )( xBHxnodes

)1lg(21212 2/ NhN hbh

12)12(21)(int# )(1)( xBHxBHxnodesהפעל על שורש

Page 5: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 5 5

קטן

X

Y

Y

X

Right_Rotate(T,y(

Left_Rotate(T,x(

גדול

גדול

קטן

אמצע אמצע

עולה/יורד ב- עומק של •

1 אינו משתנה עומק של •

ירת איזונים: רוטציות של עץ בינארישמ

Page 6: עץ  אדום -שחור

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!

השפעת רוטציה על עומק העץ

Page 7: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 7 7

אדום במקום המתאים )כמו בעץ בינארי) ובצבע X. הכנסת 1

רציפיםאדומים. ביצוע תיקונים מלמטה למעלה - אם יש שני 2

Insertביצוע

Page 8: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 8 8

Insertתיקון צבעים ב-

אדום וגם אביו אדום תיקון אם צומת

:בתיקון

האב כלפי מעלה )העברת הבעיה כלפי אדוםא) הצפת

מעלה)ב) השכנת הבעיה במסגרת משפ’ הסבא ע”י התקזזות עם

הדוד )עצירת הבעיה)

או:

אדום ואביו אדום אם צומת

האח שחור

Page 9: עץ  אדום -שחור

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

Cs
8/1/07
Page 10: עץ  אדום -שחור

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(

Page 11: עץ  אדום -שחור

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

Page 12: עץ  אדום -שחור

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

Page 13: עץ  אדום -שחור

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

Page 14: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 14 14

Delete Fixupדוגמה

Xשחור -

Xמקום שמעליו בוצעה השמטה -

)שחור כהה = חסר צמת!!!) כעל מכיל שחור + שחור חסרX- נחשוב על

כל מסלול בעץ הוא חוקי

להיפטר מהשחור המיותר )= להוסיף שם צמת)מטרה:

הוא שחור )כפול)!X יש אח כי Xל- X אח של Wיהי

Page 15: עץ  אדום -שחור

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 שחור

Page 16: עץ  אדום -שחור

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 אדום

Page 17: עץ  אדום -שחור

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 שחור

Page 18: עץ  אדום -שחור

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

Page 19: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 19 19

- מקריםDelete Fixupדוגמה

Case 4W אדום שחור + בן ימני

))Left_Rotate(P(Xשנה צבעים ו-

מטפל בבעיה וגורם ללולאה להסתיים

Case 3W בן ימני שחוראדום שחור + בן שמאלי +

Right_Rotate ובנו השמאלי ובצעWהחלף צבעים בין

4 מצב

Page 20: עץ  אדום -שחור

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

Page 21: עץ  אדום -שחור

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שנה צבעים ו-

מטפל בבעיה וגורם ללולאה להסתיים

Page 22: עץ  אדום -שחור

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

Page 23: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 23 23

Demo

• http://www.seanet.com/users/arsen/avltree.html

Page 24: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 24 24

Red Black Trees(Guibas Sedgewick 78)

CLRS: Chapter 13

Page 25: עץ  אדום -שחור

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

Page 26: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 27 27

Red Black trees - example

Page 27: עץ  אדום -שחור

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 ?

Page 28: עץ  אדום -שחור

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.

Page 29: עץ  אדום -שחור

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 ?

Page 30: עץ  אדום -שחור

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

Page 31: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 32 32

Delete + insert -- analysis

--

====>

===>

===>

Page 32: עץ  אדום -שחור

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

Page 33: עץ  אדום -שחור

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

Page 34: עץ  אדום -שחור

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

Page 35: עץ  אדום -שחור

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

Page 36: עץ  אדום -שחור

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

Page 37: עץ  אדום -שחור

Data Structures, CS, TAU, RB-Tree 38 38

Split -- analysis.

Can prove that split takes O(log n) time.

Page 38: עץ  אדום -שחור

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

Page 39: עץ  אדום -שחור

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

Page 40: עץ  אדום -שחור

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)

Page 41: עץ  אדום -שחור

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ה( אין חסם עליון על גובהו של •• 

Page 42: עץ  אדום -שחור

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.