26
Data Structures, CS, TAU, Splay Tr ee 1 Splay Tree מ ממממ ממ ממ מממממ ממממממ- ממממ מממממ מAmortized Time מממממ מממממ מממממ ממממO(N) מממ ממ מממM ממממממ ממממO(M log N) (ממ מממממ) ממ ממממ- ממ ממ מממממ ממממממ ממ ממממממ ממממ מממממ מממממ ממממ מממממ מממממ ממ מממ ממממ- M - מממממ ממממ מממממ מO(M*N) ממממ ממממממממ ממ ממממממ מממממ ממממממ ממממממ. מממממ מממממ: מממממ ממממממ מממ ממממ מממממ-

1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

  • View
    228

  • Download
    11

Embed Size (px)

Citation preview

Page 1: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 1

Splay Tree

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

-מטרה לדאוג ל Amortized Time

פעולה בודדת יכולה לקחת O(N)

אבל כל רצף Mפעולות לוקח O(M log N)

(לא מאוזן) בעית עץ חיפוש בינארי - עץ עמוק

אם ביקשנו צומת מסוים הנמצא עמוק עלינו לשנות את העץ O(M*N) גישות אליו יגרמו ל- M - אחרת

גבוהים. תורם לביצועים כי סיכויי הגישה החוזרת

- תכונה נוספת: צמתים שנגענו בהם צפים למעלה

Page 2: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 2

Rotateהצף את הצומת שניגשו אליו ע”י

k5דוגמא:k4

A

k3k2

k1

CB

D

E

F

(מסלול גישה מסומן באדום)

Rotate k5k4

A

k3k1

k2 C

B

D

E

F

Rotate

רעיון פשוט

Page 3: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 3

k5k4

A

k3k2

k1

CB D

E

F(מסלול גישה מסומן

באדום)

Rotate k5

k4

A k3

k2

k1

C

B

D

E

F

Rotate

Page 4: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 4

k5k4A

k3

k2

k1

C

B

D

E

F

Rotate

Page 5: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 5

k1 שקוע (כמעט) לעומקו המקורי של k3 בעיה:

ישנם מקרים שלוקחיםבעיה: (M*N)

דוגמה לבעיה זו בעמ’ הבא.

בעיות בעץ

Page 6: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 6

Find(1)5

34

2

1

5

34

12

5

14

2

3

5

1

23

4

2

1

34

5

דוגמא

Page 7: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 7

2

1

34

5 Find(2)

3

1

4

52

3

1

4

52

Find(3)3

1 4

52

Find(4)

3

1

45

2

Page 8: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 8

N פעולות לוקחות NM

שפוץ:

משופר,Rotate ביצוע

המבוצע על שלישיית צמתים.

שתי אפשרויות:

Zig-zag

Zig-zig

?

Page 9: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 9

A

D

G

P

B C

X

A B

G

P

C D

X

Zig-zag

A

D

GP

B

CXA

D

GP

BC

X

Zig-zig

Page 10: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 10

k5דוגמא:k4

A

k3k2

k1

CB

D

E

F

(מסלול גישה מסומן באדום)

Rotate

חזרה לדוגמה

Page 11: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 11

k5k4

A

k3k2

k1

CB D

E

F

k1, Zig-Zag gives:

k5

k4

A

k3

k2

k1

C

B

D

E F

k1, Zig-Zig gives:

חזרה לדוגמא

Page 12: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 12

5

34

2

1

7

65

4

7

6

12

3

1

46

57

23

1

2

7

6

4

35

Splaying at Node 1

Page 13: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 13

(אותה דוגמה חוזרת מוגדלת מאוחר יותר)דוגמא

Page 14: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 14

המשך דוגמא

Page 15: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 15

המשך דוגמאהמשך דוגמאהמשך דוגמא

Page 16: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 16

סוף דוגמאסוף דוגמאסוף דוגמא

Page 17: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 173

2 5

4 7

6 9

8 11

10 13

12 15

14 17

16 19

18 21

20 23

22 25

24 27

26 29

28 31

30

32

1

אותה דוגמא מוגדלת

Result of splaying at node 1 a tree of all left children

!2גובה עץ קטן פי

Page 18: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 18

4

5 7

6

32

2

3

8

9 11

10

12

13 15

14

16

17 19

18

20

21 23

22

24

25 27

26

28

29 31

30

1

Result of splaying previous tree at node 2

!2גובה עץ קטן פי

Page 19: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 19

4

3

8

9 11

10

2

5 7

6

12

16

17 19

18

13 15

14

20

24

25 27

26

21 23

22

28

32

29 31

30

1

Result of splaying previous tree at node 3

!2גובה עץ קטן פי

Page 20: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 20

6

32

32

8

9 11

10

7

5

28

29 31

30

1

16

17 19

18

13 15

14

24

25 27

26

21 23

22

4

20

12

Result of splaying previous tree at node 4

Page 21: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 21

32

32

28

29 31

30

1

16

17 19

18

13 15

14

24

25 27

26

21 23

22

4

20

8

9 11

10

6

7

5

12

Result of splaying previous tree at node 5

Page 22: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 22

32

28

29 31

30

16

17 19

18

13 15

14

24

25 27

26

21 23

22

20

8

9 11

107

12

3

2

1

45

6

Result of splaying previous tree at node 6

Page 23: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 23

32

28

29 31

30

16

17 19

18

13 15

14

24

25 27

26

21 23

22

20

9 11

10

123

2

1

45

67

8

Result of splaying previous tree at node 7

Page 24: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 24

32

28

29 31

30

16

17 19

18

13 15

14

24

25 27

26

21 23

22

20

9 11

10

12

3

2

1

45

67

8

Result of splaying previous tree at node 8

Page 25: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 25

32

28

29 31

30

16

17 19

18

13 15

14

24

25 27

26

21 23

22

20

9

10

11

12

3

2

1

45

67

8

Result of splaying previous tree at node 9

Page 26: 1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי מטרה לדאוג ל- Amortized Time פעולה בודדת יכולה לקחת O(N)

Data Structures, CS, TAU, Splay Tree 26

הערות

) פעולות זולות - עשויות להוליך לעץ לא טוב (כמו בנית העץ המקורי)1

) פעולות יקרות - מוליכות לעץ טוב 2

זמן Amortizedיהיה טוב