forest ไปเป็น binary tree -...

70
การแปลง Forest ไปเป็น binary tree ทาแบบเดียวกันกับ การแปลง tree ไปเป็น binary tree คือ เชื่อม Root เข้าด้วยกัน และทาในส่วนของ Subtree ด้วย 1 การแปลง Forest ไปเป็น Binary Tree

Upload: vokhanh

Post on 14-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การแปลง Forest ไปเป็น binary tree

• ท าแบบเดียวกนักับ การแปลง tree ไปเป็น binary tree

• คือ เชื่อม Root เข้าด้วยกัน และท าในส่วนของ Subtree ด้วย

1

การแปลง Forest ไปเป็น Binary Tree

Page 2: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

2

A

B

D

C

เริ่มต้น

E

GF H

J K

การแปลง Forest ไปเป็น Binary Tree

Page 3: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

3

A

B

D

C

Step 1

E

GF H

J K

การแปลง Forest ไปเป็น Binary Tree

Page 4: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

4

A

B

D

C

E

GF

HJ

KStep 2

การแปลง Forest ไปเป็น Binary Tree

Page 5: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

5

Binary Search Tree

Page 6: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

6

ต าแหน่งของการเอา node เข้าไปไว้ใน tree จะขึ้นกับข้อมูลที่เก็บในแต่ละ Node และลักษณะ Tree

*** ไม่สามารถก าหนด ต าแหน่งได้65

40 75

70 9530 55

35 80

โดยโครงสร้างของทรีแบบนี้ คือค่า key หลักในโหนดใด ๆ จะต้อง1. มากกว่าค่า key ของ node

ที่อยู่ทางซ้าย2. และน้อยกว่าค่า key ของ node

ที่อยู่ทางขวา

Binary Search Tree

Page 7: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

7

โดยโครงสร้างของทรีแบบนี้ คือ ค่าkey หลักในโหนดใด ๆ จะต้อง1. มากกว่าค่า key ของโหนดที่อยู่ทางซ้าย2. และน้อยกว่าค่า key ของโหนดที่อยู่ทางขวา

65

40 75

70 9530 55

35 80

Binary Search Tree

Page 8: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

8

โดยโครงสร้างของทรีแบบนี้ คือ ค่าkey หลักในโหนดใด ๆ จะต้อง1. มากกว่าค่า key ของโหนดที่อยู่ทางซ้าย2. และน้อยกว่าค่า key ของโหนดที่อยู่ทางขวา

65

40 75

70 9530 55

35 80

Binary Search Tree

Page 9: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

9

Ken , 45

Jim , 35 Mandy , 15

Bobby , 60 Jone , 40 Patty , 60

John , 20

คีย์หลักคือชื่อหรืออายุ ?

Binary Search Tree

Page 10: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

10

การสร้าง Binary Search Tree

- การสร้างจะ เป็นการ Add สมาชิก 1 ตัว ( node ) เข้าไปใน Tree

- เริ่มหาที่ Root เสมอ และท่องไปตาม node ต่าง ๆ ตามล าดับ เพื่อ

หาต าแหน่งที่เหมาะสมส าหรับ node ใหม่

Binary Search Tree

Page 11: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

11

65

65ADD Root

65 40 75 30 55 35 70 95 80

Binary Search Tree

Page 12: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

12

65

40

40ADD

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 13: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

13

65

40 75

75ADD

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 14: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

14

65

40 75

30

30ADD

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 15: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

15

65

40

30 55

55ADD

75

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 16: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

16

65

40

30 55

35

35ADD

75

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 17: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

17

65

40 75

7030 55

35

70ADD

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 18: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

18

65

40 75

7030 55

35

95ADD

95

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 19: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

19

65

40 75

70 9530 55

35 80

80ADD

การสร้าง Binary Search Tree

Page 20: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

20

65

40 75

70 9530 55

35 80

สรุป การสร้าง Binary search Tree ตามล าดับ ดังต่อไปนี้

65 40 75 30 55 35 70 95 80

การสร้าง Binary Search Tree

Page 21: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

21

65

40 75

70 9530 55

35 80

40ADD

ซ้ า !! 1. ทิ้งไปเลย

การสร้าง Binary Search Tree

Page 22: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

22

65

40 75

70 9530 55

35 80

40ADD

ซ้ า !!

2 . เก็บความถี่ Node ที่ซ้ า

2

การสร้าง Binary Search Tree

Page 23: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

23

65

40 75

70 9530 55

35 80

40ADD

3. ใช้ List list มาเชื่อม40ซ้ า !!

การสร้าง Binary Search Tree

Page 24: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

24

แบบฝึกหดั

สร้าง Binary Search Tree จากการ Add ข้อมูล

ตามล าดับ

65 40 30 70 75 55 35 95 80

การสร้าง Binary Search Tree

Page 25: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การลบข้อมูล

• การลบข้อมูล ของ Binary Tree ต้องพิจารณาดังนี้1. ถ้าเป็น leaf node สามารถลบได้ทันที

2. ถ้าโหนดที่จะลบ มีลูกเพียง 1 ตัว สามารถลบได้เลย และหลังจากที ่Parent ถูกลบ ให้ชี้ address ไปยังลูกแทน

3. ถ้าโหนดที่จะลบมี subtree ซ้ายและ ขวาจะต้องเลือกโหนดของ Tree ย่อยทางซ้าย หรือขวามาแทน โดยพิจารณาค่าที่มากที่สุดของ Tree ย่อยซ้าย หรือจะเลือก ค่าน้อยสุดของ Tree ย่อยขวา

25

Page 26: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

26

65

40 75

70 9530 55

35 80

ลบ 80

Page 27: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

27

65

40 75

70 9530 55

35

ลบ 80

Page 28: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

28

65

40 75

70 9530 55

35

ลบ 30

Page 29: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

29

65

40 75

70 9535 55

ลบ 30

Page 30: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

30

65

40 75

70 9535 55

ลบ 65

Page 31: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

31

55

40 75

70 9535

ลบ 65

70

40 75

9535 55

Page 32: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

32

Complete Binary Tree

Page 33: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Complete Binary Trees

33

15

165

ความสัมพันธ์ระหว่างระดับและจ านวน node

n = 2l -1 เมื่อ l คือ max level

Complete Binary Tree

3 12 201

Page 34: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

34

65

40 70

75 8035 55

Height : 3

จ านวน Node ทั้งหมด n

n = 2 l - 1 เมือ l คือ Max level ของ Tree

Complete Binary Tree

Page 35: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

35

65

40 70

75 8035 55

Height : 3

Max level = 1 n = 21-1 = 1Max level = 2 n = 22-1 = 3Max level = 3 n = 23-1 = 7

Complete Binary Tree

Page 36: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

36

65

40 70

75 8035 55

n = 2x-17 = 2x-1

2x = 7+1 = 8 = 23

ถ้า n = 7 node ได้ Complete Binary Tree มีความสูงเท่าใด ?

n = 2x-1n+1 = 2x

log2( n+1) = log2 2x

log2( n+1) = x Height (x) = max level = log2( n+1)

Complete Binary Tree

Page 37: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

37

การท่องเข้าไปในBinary Tree

(Binary Tree Traversals)

Page 38: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การท่องเข้าไปใน Binary Tree• การท่องเข้าไปใน Binary Tree คือ การเข้าไปเยี่ยม node ต่างๆ node ละ 1 ครั้ง จนครบทุก node

อย่างมีแบบแผน

• การเยี่ยมอาจเป็นการเข้าไปอ่านข้อมลู หรือ ประมวลผลใดๆ

• หลักการส าคัญของการท่องเข้าไปใน Binary Tree คือ เยี่ยม Root ( R ) , Subtree ด้านซ้าย ( TL ) , Subtree ด้านขวา ( TR )

ซึ่งมีอยู่ 3 วิธี (ขึ้นอยู่กับว่าจะเยี่ยมสว่นใดก่อน)1. Inorder Traversal

จะเยี่ยม TL R TR ( Root อยู่ข้างใน)2. Preorder Traversal

จะเยี่ยม RTL TR ( Root อยู่ก่อน)3. Postorder Traversal

จะเยี่ยม TL TR R ( Root อยู่หลัง)

38

Page 39: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การท่องเข้าไปใน Binary Tree• Binary Tree จะประกอบด้วย Subtree

และใน Subtree ประกอบด้วย Subtree ย่อยๆ อีก ดังนั้นเพ่ือให้ง่าย จะแทน root ของ subtree ใดๆ ด้วย จะได้

39

A

B C

D E F

G H ILeft Subtree Right Subtree

Root node

E

G H I

A

C

F

B

D

Page 40: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Inorder Traversal

• Inorder Traversalจะเยี่ยม TL R TR ( Root อยู่ข้างใน)

1. เย่ียม Left Subtree แบบ Inorder2. เยี่ยม Root3. เยี่ยม Right Subtree แบบ Inorder

40

A

B C

D E F

G H ILeft Subtree Right Subtree

Root node

Page 41: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Inorder Traversal

41

ดังนั้นการท่องเข้าไปใน Binary Tree ด้วยวิธี Inorder จะได้ DGBAECHFI

1 E

G H I

A

C

F

B

D

เข้าไปใน Subtree ย่อยสุดทางซ้ายก่อนแล้ว Root ของ Subtree นั้นแล้วเข้าไปใน Subtree ทางขวา

2

3

4

5

6

7

8

9

Page 42: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Preorder Traversal

• Preorder Traversalจะเยี่ยม RTL TR ( Root อยู่ก่อน)

1. เยี่ยม Root 2. เยี่ยม Left Subtree แบบ Preorder3. เยี่ยม Right Subtree แบบ Preorder

42

A

B C

D E F

G H ILeft Subtree Right Subtree

Root node

Page 43: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Preorder Traversal

43

ดังนั้นการท่องเข้าไปใน Binary Tree ด้วยวิธี Preorder จะได ้ ABDGCEFHI

3 E

G H I

A

C

F

B

D

เข้าไปใน Root ของ Subtree นั้นก่อนแล้วเข้าไปใน Subtree ทางซ้ายแล้วเข้าไปใน Subtree ทางขวา

4

2

1

6

5

8

7

9

Page 44: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Preorder Traversal

• Preorder Traversalจะเยี่ยม RTL TR ( Root อยู่ก่อน)

1. เย่ียม Root 2. เยี่ยม Left Subtree แบบ Preorder3. เยี่ยม Right Subtree แบบ Preorder

44

A

B C

D E F

G H ILeft Subtree Right Subtree

Root node

Page 45: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Preorder Traversal

45

ดังนั้นการท่องเข้าไปใน Binary Tree ด้วยวิธี Preorder จะได ้ ABDGCEFHI

3 E

G H I

A

C

F

B

D

เข้าไปใน Root ของ Subtree นั้นก่อนแล้วเข้าไปใน Subtree ทางซ้ายแล้วเข้าไปใน Subtree ทางขวา

4

2

1

6

5

8

7

9

Page 46: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Postorder Traversal

• Postorder Traversalจะเยี่ยม TL TRR ( Root อยู่หลัง)

1. เยี่ยม Left Subtree แบบ Postorder2. เยี่ยม Right Subtree แบบ Postorder3. เยี่ยม Root

46

A

B C

D E F

G H ILeft Subtree Right Subtree

Root node

Page 47: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Postorder Traversal

47

ดังนั้นการท่องเข้าไปใน Binary Tree ด้วยวิธี Postorder จะได ้ GDBEHIFCA

2 E

G H I

A

C

F

B

D

เข้าไปใน Subtree ทางซ้ายก่อนแล้วเข้าไปใน Subtree ทางขวาแล้วเข้าไปใน Root ของ Subtree นั้น

1

3

9

4

8

5

7

6

Page 48: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Tree Traversal

• Preorder

48

A

B C

D E F G

Page 49: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Tree Traversal

• Inorder

49

A

B C

D E F G

Page 50: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Tree Traversal

• Postorder

50

A

B C

D E F G

Page 51: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

แบบฝึกหัด

51

จงท่องเข้าไปใน Binary Tree ต่อไปนี้ในแบบPreorder, Inorder และ PostOrder

A

B D

C

+

A -

* D

B C

A

B C

D

E

E

G

1.

2.

3.

Page 52: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

52

AVL Tree

Page 53: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

AVL Tree

• AVL Tree คือ binary search tree ทีม่ีเงื่อนไขสมดุล ของทร ีแต่ละโหนดมีความสูงของแต่ละทรยี่อยต่างกันไม่เกิน 1

• หลักทั่วไปคือ แต่ละโหนดจะมีทรีย่อยซ้ายและ ทรีย่อยขวาซึ่งจะมีความสูงเท่ากัน และจะมีการปรับความสมดุลจะต้องมีใน ทุกๆ โหนด

• AVL Tree เหมือนกับ Binary Tree ทุกอยา่งแต่จะมี ทรยี่อยซ้ายและขวาต่างกันได้สูงสุดคือ -1 0 1

• จะเรียกค่า ความต่างของ ทรยี่อยซ้ายและ ทรีย่อยขวาว่า บารานซ์เฟกเตอร์

53

Page 54: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

54

6

2 8

7

80

1 4

1

-1 1

010

0

Page 55: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

55

6

2 8

780

1 4

2

-1 0

0

00

0

Page 56: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การหมุน

• ค่าของบารานซ์แฟคเตอร ์ในแต่ละโหนดมีได้ไม่เกิน -1 0 1 ถ้าระหว่างที่มีการเพ่ิมข้อมูลลงใน Binary แล้ว ท าให้ ทรเีสียสมดุล ไม่เป็น AVL Tree จะต้องมีการปรับทรีใหม่โดยวิธีการหมุน ซึ่งมี 2 วิธีคือ

• หมุน 1 ครั้ง

• หมุน 2 ครั้ง

56

Page 57: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การหมุน 1 ครงั

• การหมุน 1 ครัง้ สามารถท าได้ 2 ลักษณะคอื หมุนซ้ายกับหมุนขวา

การหมุนขวา

57

k2

k1

T1 T2

T3

k2

k1

T1 T2 T3

Page 58: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

หมุนขวา

• ให้ K2 และ K1 เป็นโหนด T1 T2 T3 เป็น subtree

• ให้ K2 เป็นโหนดที่เกิดวิกฤต

ท าโดยที่ K1 <K2

สมาชิกทุกตัวที่อยู่ใน T1 < K1

สมาชิกทุกตัวใน T3 > k2

สมาชิกทุกตัวใน T2 อยู่ระหว่าง K1 และ K2

58

k2k1

T1

T2

T3

Page 59: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

หมุนซ้าย

• ให้ K2 และ K1 เป็นโหนด T1 T2 T3 เป็น subtree

• ให้ K2 เป็นโหนดที่เกิดวิกฤต

ท าโดยที่ K1 > K2

สมาชิกทุกตัวที่อยู่ใน T1 < K2

สมาชิกทุกตัวใน T3 > k1

สมาชิกทุกตัวใน T2 อยู่ระหว่าง K1 และ K2

59

k2k1

T3

T2

T1

k1k2

T3

T2

T1

Page 60: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

ตัวอย่าง

• สร้าง AVL Tree จากเลข 1 ถงึ 7 ตามล าดับ

60

12

31

2

3

Page 61: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

61

1

2

34

5

1

2

4

3 5

Page 62: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

62

1

2

4

3 5

6

2

45

1 63

Page 63: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

63

2

45

1 63

7

2

46

1 73 5

Page 64: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

การหมุน 2 ครัง้

ในบางกรณี ไม่สามารถที่จะปรับ โดยการหมุนเพียง 1 ครั้งได้ จ าเป็นต้องปรับ 2 ครั้งคือ หมุนซ้าย 1 ครั้งและขวา 1 ครั้ง (Left-right rotation) หรือ หมุนขวา 1 ครัง้และ หมุนซ้าย 1 ครั้ง(right-left rotation)

64

Page 65: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Left-right rotation

65

k1

k2

T3

T2

T1

T4

k3

k2

k1

T3

T2

T1

T4

k3

k2

k1

T3

T2

T1

T4

k3

Page 66: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

Right-left rotation

66

k2

k3

T3

T2

T1

T4

k1

k2k1

T2

T3

T4

T1

k3

k2

k3

T3

T2

T1

T4

k1

Page 67: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

ตัวอย่าง

67

2

46

1 73 515

14

2

46

1 143 5

7 15

Page 68: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

68

2

46

1 143 5

7 15

13

2

47

1 143 6

5 1513

Page 69: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

เพิ่มข้อมูลต่อ โดยเพิ่ม 12 11 8 9 8.5 ตามล าดับ

69

2

47

1 143 6

5 1513

Page 70: Forest ไปเป็น Binary Tree - mathcom.uru.ac.thmathcom.uru.ac.th/~beebrain/Slide/4122303A/TreeTravel.pdf · การแปลง Forest ไปเป็นbinary tree •

แบบฝึกหัด

ท าการสร้าง binary Tree ตามข้อมูลดังนี้ 39 15 20 40 45 11 60 48 5 2 7 30 80 70 22 13 8 33 จากนั้นท าการ search แบบ Preorder inorder และ Postorder

ท าการเพ่ิมข้อมูล ให้เป็นแบบ AVL Tree โดยเพิ่มข้อมูลดงันี้ 6 9 43 42 14 17 19 วาดภาพขึ้นตอนการเพิ่มข้อมูลลง Tree และปรับ Tree ให้ Balance

70