1 Data Structures, CS, TAU, Splay Tree Splay Tree מימוש של עץ חיפוש בינארי...

Preview:

Citation preview

Data Structures, CS, TAU, Splay Tree 1

Splay Tree

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

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

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

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

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

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

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

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

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

רעיון פשוט

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

Data Structures, CS, TAU, Splay Tree 4

k5k4A

k3

k2

k1

C

B

D

E

F

Rotate

Data Structures, CS, TAU, Splay Tree 5

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

ישנם מקרים שלוקחיםבעיה: (M*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

דוגמא

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

Data Structures, CS, TAU, Splay Tree 8

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

שפוץ:

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

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

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

Zig-zag

Zig-zig

?

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

Data Structures, CS, TAU, Splay Tree 10

k5דוגמא:k4

A

k3k2

k1

CB

D

E

F

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

Rotate

חזרה לדוגמה

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:

חזרה לדוגמא

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

Data Structures, CS, TAU, Splay Tree 13

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

Data Structures, CS, TAU, Splay Tree 14

המשך דוגמא

Data Structures, CS, TAU, Splay Tree 15

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

Data Structures, CS, TAU, Splay Tree 16

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

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גובה עץ קטן פי

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גובה עץ קטן פי

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גובה עץ קטן פי

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

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

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

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

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

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

Data Structures, CS, TAU, Splay Tree 26

הערות

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

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

זמן Amortizedיהיה טוב

Recommended