blue bubbles template 6 tree.pdf · 235012 : data structure and algorithms...

125
235012 : Data Structure and Algorithms Chapter 6 ต้นไม้ (Tree) Chapter 1 235012 Data Structure and Algorithms

Upload: others

Post on 29-Oct-2019

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

Chapter 6

ตนไม (Tree)

Chapter 1

235012

Data Structure and Algorithms

Page 2: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

โครงสรางทวไปของตนไม

Non-Linear Data Structure

Hierarchical Relationship

Root - จดยอดของตนไม

Node - จดทมการแตกกงกานสาขาออกไป

Branch or Link - กงกานสาขาทเชอมตอระหวางโหนด

Page 3: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

ลกษณะของโครงสรางขอมลแบบตนไม

Root

Node

Branch/ Link

Page 4: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การเรยกชอสวนตางๆ ของตนไม

โหนดราก (Root Node) โหนดแรกของตนไม

โหนดแม(Parent Node) node ทม degree > 0

โหนดลก Children) node ทอยล าดบลางของอกโหนดหนง

โหนดพนอง (sibling) โหนดทอยในระดบเดยวกนมโหนดแมรวมกน

โหนดใบ (Leaf Node) node ทม degree = 0

Page 5: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การเรยกชอสวนตางๆ ของตนไม

ระดบ Level ระยะทางจาก ราก จนถงโหนดนนๆ

ความสง (height) หรอ

ความลก (depth)

ระดบสงสดของโหนดใบ บวกดวย 1

สวนยอยของตนไม

(Sub trees)

โหนดทอยในต าแหนงสบทอดของโหนดทเปนราก

Page 6: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

สวนยอยของตนไม (Sub-Tree)

ทกโหนดเปน root ของ sub-tree ของตวมนเอง

R เปน root ของ subtree A,B,C,D A เปน root ของ subtree E,F,G F เปน root ของ subtree J C เปน subtree ทมแต root C

Page 7: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

Root

Parents

Leaves

Children sibling

(A)

(B)

(H)

(C)

(E)

Page 8: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

ตนไมแบบทวภาค (Binary Tree)

Tree ทแตละ node มจ านวน sub tree <= 2

(แตละ node ม degree <= 2 )

Page 9: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

Empty หรอ

Null Tree

Page 10: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

1

2

3

4

1

2 3

2 3 4 4 4

4

Fig.A Fig.B

Page 11: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

ตนไมส าหรบคนหาแบบทวภาค (Binary Search Tree)

คอ Tree แบบทวภาค แตมเงอนไขเพมเตมคอ ขอมลของโหนดลก

ดานซายจะตองมคานอยกวาโหนดพอ และขอมลโหนดลก

ดานขวาจะตองมคามากกวาโหนดพอเสมอ

7

5

1

12

17 9 3

Page 12: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การแทนตนไมแบบทวภาคในคอมพวเตอร

การแทนโดยอาศยอารเรย

o ใชอารเรย 3 อารเรยในการจดเกบ

o ใชอารเรย 1 มตอารเรยเดยวในการจดเกบ

การแทนโดยอาศย Linklisted

Page 13: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การแทนโดยใชอารเรย 3 อารเรยในการจดเกบ

จะใชอารเรยทมขนาดเทากนจ านวน 3 ชด คอ

data – เกบขอมลในแตละ node

left – เกบต าแหนงของ node ลกทางดานซายมอ

right – เกบต าแหนงของ node ลกทางดานขวามอ

ถา node ใดไมม node ลก ใหเกบคา 0 (แทน NULL)

และใชตวแปร root เกบต าแหนงของ node แรกใน tree

Page 14: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

A 2 3 B 6 4 C 7 0 E 8 5 H 0 0 D 0 0 F 0 0 G 0 0

data left right 1

root 1 2 3 4 5 6 7 8

A

B C

D E F

G H

ตวอยาง

ขอมลในหนวยความจ า General binary tree

Page 15: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การแทนโดยใชอารเรย 1 มตอารเรยเดยวในการจดเกบ

ก าหนดต าแหนงชองในอารเรยใหแตละ node โดย

เรมตงแต level 0 , 1 , … , k ตามล าดบ

แตละ level จะเรมจาก node ทางดานซายสดไปถงโหนดทางขวา

สด ตามล าดบ

ลกษณะเปนการนบชองจากบนลงลาง, ซายไปขวา

การก าหนดต าแหนงเชนนจะไมสนใจวามขอมลของโหนดนนจรง

หรอไม (Fix ต าแหนงอารเรยไว)

Page 16: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

1 A B C

D E G

F

H I

2 3 4 5 6 7

8 9 10 11 12 13 14 15

A B C D E - F G - - H - - - I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

general binary tree

ขอมลในหนวยความจ า

ตวอยาง

Page 17: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

จะไดความสมพนธระหวาง tree และ อารเรย ดงน

ถา binary tree มความสงเทากบ h จะตองใชอารเรยทมขนาด

2h – 1 ในการจดเกบ

parent ของโหนด i ใดๆ จะอยทต าแหนง i/2 ถา i ไมเทากบ 1

left child ของโหนด i ใดๆ จะอยทต าแหนง 2i

right child ของโหนด i ใดๆ จะอยทต าแหนง 2i + 1

Page 18: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การแทนโดยอาศย link listed

เปนวธการจดเกบทมประสทธภาพ เนองจากไมเกดปญหาการ

จองขอมลมากหรอนอยเกนไป

โดยขอมลแตละ node ของ tree จะประกอบไปดวย

data – เกบขอมลในแตละ node

Lptr – เกบ address ของ node ลกทางดานซายมอ

Rptr – เกบ address ของ node ลกทางดานขวามอ

Page 19: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

ตวอยาง

Page 20: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

Assignment : จงเขยนภาพแสดงการจดเกบขอมลในหนวยความจ า Binary

Tree ดวยวธตอไปน

1. ใชอารเรยทมขนาดเทากนจ านวน 3 ชด 2. ใชอารเรย 1 มตอารเรยเดยวในการจดเกบ 3. การแทนโดยอาศย link-listed

Page 21: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

ใชอารเรยทมขนาดเทากนจ านวน 3 ชด

A 4 3 B 2 1 C 0 -1 D -1 -1 E -1 -1

data left right 5

root 5 4 3 2 1

ใชอารเรย 1 มตอารเรยเดยวในการจดเกบ

F -1 -1 0

A B C D E F

เฉลย !!!

Page 22: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

การแทนโดยอาศย link-listed

A

B

null D null null E null

C null

null F null

เฉลย !!!

Page 23: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

23

Assignment : จงเขยนภาพแสดงการจดเกบขอมลในหนวยความจ า Binary

Tree ดวยวธตอไปน

1. ใชอารเรยทมขนาดเทากนจ านวน 3 ชด 2. ใชอารเรย 1 มตอารเรยเดยวในการจดเกบ 3. การแทนโดยอาศย pointer

Page 24: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

จากการขอมลในอารเรยตอไปน เขยนเปนโครงสรางขอมล Binary Tree ไดอยางไร

2 7 5 2 6

data left right 0

root

1 2 3 4 5 9 6

0

5 11 4

7 8

เฉลย !!!

Page 25: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

235012 : Data Structure and Algorithms

จากการขอมลในอารเรยตอไปน เขยนเปนโครงสรางขอมล Binary Tree ไดอยางไร

2 7 5 2 6 9 5 11 4

เฉลย !!!

Page 26: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

2

Binary tree traversals Expression Tree Binary Search Tree (BST) AVL Tree Heaps

ตนไมแบบทวภาค (Binary tree)

Page 27: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

27

ตนไมแบบทวภาค (binary tree) หมายถง tree ทแตละ node มจ านวน subtree <= 2

( แตละ node ม degree <= 2 )

Page 28: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

28

Empty หรอ Null Tree

Page 29: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

29

Complete และ Nearly Complete Binary Tree

Page 30: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Binary tree traversals 30

1. Depth-first traversals เปนการทองเขาไปในทรดวยการเดนทางจากรทโหนดไปยงโหนดลก ๆ

2. Breath-first traversals ประมวลผลทละ Level จากบนลงลาง

Page 31: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Depth-first traversals 31

มการบญญตค ายอเพอใชงานดงน

N แทน root node

L แทน left sub tree

R แทน right sub tree

ม 3 วธทนยมใชกนอยางแพรหลาย

Pre-order traversals

In-order traversals

Post-order traversals

Page 32: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

32

N L R L N R L R N

Depth-first tarversals

Page 33: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

33

แบบ Pre-order

N L R

algorithm preOrder (val root <node pointer>)

1 if (root is not null)

1 process(root)

2 preOrder(root->leftSubtree)

3 preOrder(root->rightSubtree)

2 return

Page 34: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบ Pre-Order Traversal

Y

R

X A

D C

G

start

R

Result

A C D X Y G

stop

N L R

Page 35: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

35

แบบ Inorder

L N R

algorithm inOrder (val root <node pointer>)

1 if (root is not null)

1 inOrder(root->leftSubtree)

2 process(root)

3 inOrder(root->rightSubtree)

2 return

Page 36: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบ In-Order Traversal

Y

R

X A

D

G

start

C

Result

A D R G Y X

stop

L N R

C

Page 37: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

37

แบบ Post-order

L R N

algorithm postOrder (val root <node pointer>)

1 if (root is not null)

1 postOrder(root->leftSubtree)

2 postOrder(root->rightSubtree)

3 process(root)

2 return

Page 38: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบ Post-Order Traversal

R

X A

D C

start

C

Result

D A G Y X R

stop

L R N

Page 39: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

A

B C

D

G

E F

H I

Pre-order – NLR

In-order – LNR

Post-order - LRN

A B D G C E H I F

D G B A H E I C F

G D B H I E F C A

Page 40: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

40

Pre-order – NLR

In-order – LNR

Post-order - LRN

2 7 2 6 5 11 5 9 4

2 7 5 6 11 2 5 4 9

2 5 11 6 7 4 9 5 2

Page 41: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

algorithm breathFirst (val root <node pointer>)

1 p = root

2 while (p not null)

1 process(p)

2 if (p->left not null)

1 enqueue(p->left)

3 if (p->right not null)

1 enqueue(p->right)

4 if (not emptyQueue)

1 dequeue(p) else

1 p = null

3 return

Breath-first

Page 42: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

A

B C

D

G

E F

H I

Breath-first A B C D E F G H I

Page 43: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

43

Expression Tree

หมายถง Binary Tree ทมคณสมบตดงตอไปน

1. Leaf เกบ Operand

2. Root และ Internal node เกบ Operator

3. Subtree เปน Sub expression

Page 44: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตวอยาง

Page 45: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

45

การทองใน Expression Tree

1. Pre-order Traversal

2. Post-order Traversal

3. In-order Traversal

Prefix Expression

Postfix Expression

Infix Expression

Page 46: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Pre-order Traversal

+ * a + b c d

1 if (root is not null)

1 print(root->token)

2 prefix(root->leftSubtree)

3 prefix(root->rightSubtree)

2 return

Page 47: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Post-order Traversal

a b c + * d +

1 if (root is not null)

1 postfix(root->leftSubtree)

2 postfix(root->rightSubtree)

3 print(root->token)

2 return

Page 48: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Inorder Traversal

1 if (root is not null)

1 if root->token is operand

1 print(root->token)

else

1 print(open parenthesis)

2 infix(root->leftSubtree)

3 print(root->token)

4 infix(root->rightSubtree)

5 print(close parenthesis)

2 return

( ( a * ( b + c ) ) + d )

Page 49: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

+

A -

* D

B C

จงหา Expression ของ Binary Tree น

1. Pre-fix Expression (NLR)

2. Infix Expression (LNR)

3. Postfix Expression (LRN)

Assignment

+ A - * B C D

A + B * C - D

A B C * D - +

Page 50: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

General Tree

หมายถง Tree ทสามารถม degree ไดไมจ ากดจ านวน

Page 51: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ขนตอน

1. ระบ chide ทอยดานซายสด

2. ใหเชอม node ทมพอเดยวกน ( node พนอง) เขาดวยกน

- ลบเสนเชอมโยงจาก node ทเปนพอไปยงลก ๆ ออกใหหมด

ยกเวน ลกทอยต าแหนงซายสด

3. เลอก son ทางซายและขวา ของแตละ node

- son ทางซาย คอ node ทอยใต node นน

- son ทางขวา คอ node ทอยขาง ๆ (ระดบเดยวกน / Brother )

การแปลง General Tree เปน Binary Tree

Page 52: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตวอยาง การแปลง General Tree เปน Binary Tree

Page 53: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

เรมตน A

B D

E F G T

C

H

1.ใหเชอม node ทมพอเดยวกน ( node พนอง) เขาดวยกน

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

Page 54: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

54

A

B D

F G T

C

H

ขนท 1

E

1.ใหเชอม node ทมพอเดยวกน ( node พนอง) เขาดวยกน

Page 55: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

A

B D

F G T

C

H E 1. ลบเสนเชอมโยงจาก node ทเปนพอไปยง ลก ๆ ออกใหหมด

ยกเวน ลกทอยต าแหนงซายสด

ขนท 1-1

Page 56: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

เลอก son ทางซายและขวา ของแตละ node

- son ทางซาย คอ node ทอยใต node นน

- son ทางขวา คอ node ทอยขาง ๆ

(ระดบเดยวกน / Brother ) 56

A

B

D

F G

T

C

H

E

ขนท 2

Page 57: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบฝกหด จงแปลง Tree ใหเปน Binary Tree

1

2

6

10

7

3 4 5

8 9

Page 58: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบฝกหด-เฉลย จงแปลง Tree ใหเปน Binary Tree

1

2

6

10 7

3

4

5 8

9

Page 59: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Binary Search Tree

เปน binary tree ทมคณสมบตพเศษ คอ ทก node ของ subtree ทางซายมคานอยกวา root ทก node ของ subtree ทางขวามคามากกวาหรอเทากบ root ทก ๆ subtree เปน binary search tree

>

Page 60: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตวอยาง Binary Search Tree ทถกตอง

Page 61: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

61

ตวอยาง Binary Search Tree ทไมถกตอง

Page 62: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ประโยชนของ Binary Search Tree

ตองการคนหาขอมล 1 ตวจากขอมลทงหมด 100,000 ตว

เกบในอารเรย : อาจตองคนหา 100,000 ครง

คนหาในอารเรยทละชอง

เกบใน BST : ลดจ านวนการคนหาลงไดสงสดเหลอเพยง 17 ครง(โดยประมาณ)

ถา root แบงขอมลเปน 2 สวนเทาๆกน

คนครงท 1 เหลอขอมล ½

คนครงท 2 เหลอขอมล ¼

...

Page 63: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

สงทควรทราบเพมเตมเกยวกบ binary search tree

ถาเปน BST ทลกทางดานซายมคานอยกวาพอแม เราสามารถใช inorder traversal

เพอหาขอมลทเรยงล าดบกนได

ถาตองการหาขอมลทมคานอยทสด สามารถหาได leaf ทอยทางดานซายสดของทร

หรอจะเทากบ root ของ subtree ทางดานซายสด ถากรณ subtree นนไมมโหนดลก

ทางดานซาย

ในท านองเดยวกน เรากสามารถหาขอมลทมคามากทสดไดเชนเดยวกน

Page 64: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

operation ทตองท าใน BST

BST ม operation ทส าคญอย 2 operation คอ

การเพมโหนด

เพมแลวโหนดใหมตองเปน Leaf เทานน

เพมในต าแหนงทยงคงคณสมบตของ Binary Search Tree ไว

การลบโหนด

Page 65: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

65

ตวอยาง การเพมขอมลใน Binary Search Tree

Page 66: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

66

Page 67: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

การลบโหนด ใน BST 67

ตองพจารณาวา Node ทจะลบนนตรงกบกรณ

1. ไมม Child

2. มเฉพาะ Right Subtree

3. มเฉพาะ Left Subtree

4. มทง Left และ Right Subtree

<== ลบไดเลย

<== ดง Right Subtree ขนมา

<== ดง Left Subtree ขนมา

น าคาทมากทสดของ Left Subtree มา

แทน

Page 68: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

68

ตวอยาง กรณท 1 ไมมโหนดลก

23

18 X X 44 X X before delete 44

23 X

18 X X after delete 44

Page 69: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

69

ตวอยาง กรณท 2 มเฉพาะ right subtree

23

18 X X 44 X

before delete 44

46 X X

58 X

after delete 44

23

18 X X

46 X X

58 X

Page 70: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

70

ตวอยาง กรณท 3 มเฉพาะ left subtree

23

18 X X 44 X

before delete 44

36 X X

28 X

23

18 X X

after delete 44

36 X X

28 X

Page 71: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

71

ตวอยาง กรณท 4 มโหนดลกทง 2 ขาง

23

18 44 X

before delete 23

20 X X 10 X X

20

18 X 44 X

after delete 23 10 X X

Page 72: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

การสราง BST

insert ขอมลเขาไปทละ 1 โหนด

เชน 10 15 8 14 13 12

10 X X

insert 10 insert 15

10 X

15 X X

insert 8

10

15 X X 8 X X

Page 73: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

73

insert 14 10

15 X 8 X X

14 X X

insert 13 10

15 X 8 X X

14 X

13 X X

insert 12 10

15 X 8 X X

14 X

13 X

12 X X

Page 74: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

65

40 75

70 95 30 55

35 80

65 40 75 30 55 35 70 95 80

ตวอยาง

Page 75: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

75

65

40 75

70 95 30 55

35 80

40 ADD

ซ ำ !! 1. ทงไปเลย

Page 76: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

76

65

40 75

70 95 30 55

35 80

40 ADD

ซ ำ !!

2 . เกบควำมถ Node ทซ ำ

2

Page 77: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

77

65

40 75

70 95 30 55

35 80

40 ADD

3. ใช Link list มำเชอม 40 ซ ำ !!

Page 78: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

แบบฝกหด สราง Binary Search Tree จากการ Add ขอมลตามล าดบ

65 40 30 70 75 55 35 95 80

Page 79: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

คณภาพของ BST ขนอยกบการกระจายคาของขอมล

ถามขอมลทเกอบเรยงหรอเรยงกนอยแลว จะท าใหเกด BST ทไมสมดล

เชน ชดขอมล 10 12 18 24 35

10 X 12 X

18 X 24 X

25 X X

Page 80: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

80

ขอเสย : ความเรวในการคนหาขอมลจะแย (กรณแยทสดคอตองเปรยบเทยบ n

ครง)

ป 1960 นกคณตศาสตรชาวรสเซยไดพฒนาเทคนคในการจดรปแบบของ BST

โดยปรบแตงโหนดใหสมดลหรอเกอบสมดล ทกครงทมการเพมหรอลบขอมล และ

ยงคงคณสมบตของ BST อย

เรยกตนไมชนดนวา AVL tree หรอ ตนไมความสงสมดล

Page 81: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

AVL tree (Adel’son, Vel’skii and Landis) 81

Height-balanced binary search tree หรอ ตนไมความสงสมดล

คอ BST ชนดหนงทมคณสมบตเพมเตมคอ

ทก subtree ม balance factor (bf) แตกตางกนไมเกน 1 [-1, 0, +1]

bf คอ ความสงของ left subtree ลบดวย ความสงของ right subtree

bf = | HL - HR |

| bf | <= 1

Page 82: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

82

0

-1

-2

-3

-4

-5

-6

-7

0 0

0 0 0

1 -1

1

Page 83: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

83

AVL tree

unbalanced BST

Page 84: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

การเพม , ลบโหนดออกจาก AVL Tree

ใชวธการเดยวกนกบการเพม , ลบ โหนดจาก BST

การเพม – เพมทต าแหนงทเปน leaf เทานน

การลบ – ม 4 กรณ

หลงจากเพม – ลบ โหนดทกๆ ครง

ตองสราง Balancing Tree เพอคงคณสมบตของ AVL Tree

Page 85: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Balancing Tree (หลกการท าตนไมใหสมดล)

คอการปรบตนไมทไมเปน AVL tree ใหเปน AVL tree

( | bf | <= 1 , | HL - HR | <= 1 )

เกดขนทกครงหลงการเพมหรอลบโหนด ซงท าใหตนไมขาดคณสมบตของ AVL tree

ไป

มกรณทตองจ าอย 4 กรณ คอ

- SLR (single left rotation) - SRR (single right rotation)

- DLR (double left rotation) - DRR (double right rotation)

Page 86: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

SLR (single left rotation) 86

ใชในกรณทโหนดลางสด ทไมสมดลม bf = -2 (right subtree ของโหนดนสงกวา

left subtree อย 2) ดงนนจงตองมการหมนไปทางซาย

และโหนดลกทางดานขวาของโหนดขางบนม bf = -1 (right subtree ของโหนดนก

ยงสงกวา left subtree อย 1) ซงหมายความวาตนไมมการเอยงไปทางดานขวา

ตลอด ดงนนจงหมนไปทางซายแคครงเดยว

Page 87: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

87 10 20

30

(-2)

(-1)

(0)

20

10 30 (0) (0)

(0)

10 20

30

(-2)

(-1)

(-1,1) A

B

D C

กรณม 3 โหนด

10 20

30

(0)

(-1,1)

A B D C

(0)

กรณทวไป เปลยนต าแหนงของ

subtree B

Page 88: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

88

SLR

Page 89: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

SRR (single right rotation) 89

ใชในกรณทโหนดลางสดทไมสมดลม bf = 2 (left subtree ของโหนดนสงกวา right

subtree อย 2) ดงนนจงตองมการหมนไปทางขวา

และโหนดลกทางดานซายของโหนดนม bf = 1 (left subtree ของโหนดนกยงสง

กวา right subtree อย 1) ซงหมายความวาตนไมมการเอยงไปทางดานซายตลอด

ดงนนจงหมนไปทางขวาแคครงเดยว

Page 90: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

90 30 20

10

(2)

(1)

(0)

20

10 30 (0) (0)

(0)

30 20

10

(2)

(1)

(-1,1) D

C

B A

กรณม 3 โหนด

10 20

30

(0)

(-1,1)

A B D C

(0)

กรณทวไป เปลยนต าแหนงของ subtree C

Page 91: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

91 SRR

Page 92: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

DLR (double left rotation) 92

ใชในกรณทโหนดลางสด ทไมสมดลม bf = -2 (right subtree ของโหนด

นสงกวา left subtree อย 2) ดงนนจงตองมการหมนไปทางซาย

แตโหนดลกทางดานขวาของโหนดขางบน ม bf = 1 (right subtree ของ

โหนดลกกลบต ากวา left subtree อย 1) ซงหมายความวาตนไมมการ

เอยงไปทางดานขวาและดานซาย ดงนนจงหมนไปทางซาย 2 ครง

Page 93: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

93 10 30

20

(-2)

(1)

(0)

20

10 30 (0) (0)

(0)

10 30

20

(-2)

(1)

(-1,1) A

D

C B

กรณม 3 โหนด

10 20

30

(0)

(0,-1)

A B D C

(1,0)

กรณทวไป เปลยนต าแหนงของ subtree B,C

Page 94: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

94 DLR

case ท 1

Page 95: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

95

DLR

case ท 2

+

Page 96: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

DRR (double right rotation) 96

ใชในกรณทโหนดลางสดทไมสมดลม bf = 2 (left subtree ของโหนดนสงกวา right

subtree อย 2) ดงนนจงตองมการหมนไปทางขวา

แตโหนดลกทางดานซายของโหนดน ม bf = -1 (left subtree ของโหนดนกลบต า

กวา right subtree อย 1) ซงหมายความวาตนไมมการเอยงไปทางดานซายและ

ดานขวา ดงนนจงหมนไปทางขวา 2 ครง

Page 97: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

97 30 10

20

(2)

(-1)

(0)

20

10 30 (0) (0)

(0)

30 10

20

(2)

(-1)

(1,-1) D

A

C B

กรณม 3 โหนด

10 20

30

(0)

(-1,0)

A B D C

(0,1)

กรณทวไป เปลยนต าแหนงของ subtree B,C

Page 98: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

98

0

DRR

case ท 1

Page 99: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

99 DRR

case ท 2

Page 100: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Heaps หวขอวนน

Heaps โครงสรางของ heap heap operation

Reheap Up , Reheap Down การแทน heap ในหนวยความจ า heap sort (application of heap) ขอดของ heap sort

100

Page 101: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Heap

คอ binary tree ทมคณสมบต คอ แตละ subtree คาของ root >= child เสมอ (Max-heap) หรอ แตละ subtree คาของ root <= child เสมอ (Min-heap) เปน complete binary tree หรอ nearly complete

binary tree Heap ไมใช binary search tree

101

Page 102: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตนไมทวภาคแบบสมบรณ (complete binary tree)

คอ binary tree ทม node เตมทก level (ทก node ทไมใช leaf ม child

ทงดานซายและดานขวา)

102

nearly complete binary tree คอ binary tree ทม node เตมทก level ยกเวน level สดทาย และ

node ใน level สดทายอยเรยงกนทางซายมอ

Page 103: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

103

Page 104: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

โครงสรางของ Max-heap 104

< <

คาทมากทสดอยท root เสมอ

Page 105: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

โครงสรางของ Min-heap 105

> >

คาทนอยทสดอยท root เสมอ

Page 106: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตวอยาง Heap ทถกตอง 106

Page 107: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

ตวอยาง Tree ทไมใช Heap 107

Page 108: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Heap Operation 1. Insert & Reheap Up Insert โหนดใหมท level ลางสด ณ ต าแหนงวางดานซายสด (ท าให

ยงคงสภาพของ nearly complete หรอ complete binary tree อย) Reheap Up ปรบ tree หลงจาก insert ใหเปน heap

2. Delete & Reheap Down Delete ท Root เทานน และน า Node ท level ลางสดต าแหนงขวาสด

ขนมาแทน (ท าใหยงคงสภาพของ nearly complete หรอ complete binary tree อย)

Reheap Down ปรบ tree หลงจาก delete ใหเปน heap

108

Page 109: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Reheap Up สลบทระหวางโหนดท insert เขามากบ Parent ของมนเองตามหลกของ

heap ท าซ ากบโหนดเดมจนกวาจะอยต าแหนงทถกตอง

109

a heap

Page 110: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

110

ตวอยาง

Page 111: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Reheap Down สลบทระหวางโหนดทต าแหนง root (โหนดทพงสลบทมาหลงจาก

delete) กบโหนดลกของมนเองจนกวาโหนดนนจะอยต าแหนงทถกตอง (เลอกโหนดลกตามหลกของ heap) ท าซ าจนกวา root จะอยต าแหนงทถกตอง

111

a heap

Page 112: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

112

ตวอยาง

Delete

Page 113: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

113

Page 114: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

การแทน heap ในหนวยความจ า นยมแทน heap ลงในอารเรย 1 มต เนองจากคณสมบตของการเปน nearly

complete ของตวมนเอง ขอมลทจดเกบลงในอารเรยจะเรยงตอกนไปตามล าดบจากซายไปขวาของแต

ละ level ใน heap จะไดความสมพนธของต าแหนงขอมลใน heap คอ

parent ของโหนด i ใดๆ = i/2 ถา i <> 1 left child ของโหนด i ใดๆ = 2i right child ของโหนด i ใดๆ = 2i + 1

114

Page 115: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

115 [1]

[2] [3]

[4] [5] [6] [7]

[1] [2] [3] [1] [4] [5] [6] [7]

Page 116: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

Heap sort (Application of heap)

ส าหรบ heap ขอมลทมคาสงสด/ต าสดจะเกบอยท root คณสมบตดงกลาว + ลกษณะการเกบขอมล สามารถน ามาชวย

ในการเรยงล าดบขอมลได 1. น าขอมลเขาในลกษณะของ heap จนหมด 2. delete ทละโหนด จนหมด

ขอมลท delete จะสลบทกบโหนดสดทายแทน ล าดบของขอมลท delete เรยงจาก มาก -> นอย คาในอารเรย เรยงจาก นอย -> มาก

116

Page 117: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

117

1. insert 14

14 14

70

2. insert 70 3. reheap up

70

14

ก าหนดใหขอมลน าเขาคอ 14 , 70 , 32 , 100 , 65 , 56

ขนท 1 น าขอมลเขา heap ทละโหนดจนหมด

4. insert 32

70

14 32

70

14 32

100

5. insert 100 6. reheap up

100

70 32

14

Page 118: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

118

7. insert 65

100

70 32

14 65

100

70 32

14 65 56

7. insert 56

100

70 56

14 65 32

8. reheap up

ในหนวยความจ า

100 70 56 14 65 32

Page 119: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

119

1. delete 100

32

70 56

14 65 100

70

65 56

14 32 100

2. reheap down

3. delete 70

32

65 56

14 70 100

4. reheap down

65

32 56

14 70 100

ขนท 2 delete ขอมลทละโหนดจนหมด (สลบกบโหนดสดทายแทนตดจาก heap )

Page 120: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

120

5. delete 65 6. reheap down

7. delete 56 8. reheap down

14 32 56

65 70 100

56 32 14

65 70 100

14 32 56

65 70 100

32 14 56

65 70 100

Page 121: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

121

9. delete 32

14 32 56

65 70 100

10. delete 14

14 32 56

65 70 100

เหลอโหนดเดยว ไมตองปรบ heap

สนสดการท างาน ขอมลเรยงล าดบแลว

Page 122: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

[1] [2] [3] [4] [5] [6] ขนท 1 สราง heap 1. insert 14 14 2. insert 70 14 70 3. reheap up 70 14

122 ในการเขยนโปรแกรมจรงจะเปนการจดการกบอารเรยทแทนโครงสราง heap ดงนนจากตวอยางขางตน ส งทจะเกดในหนวยความจ าจะเปนการเปลยนแปลงคาขอมลในอารเรยแทน

Page 123: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

[1] [2] [3] [4] [5] [6] 4. insert 32 70 14 32 5. insert 100 70 14 32 100 6. reheap up 100 70 32 14 7. insert 65 100 70 32 14 65 8. insert 56 100 70 32 14 65 56 9. reheap up 100 70 56 14 65 32 ขนท 2 delete heap 100 70 56 14 65 32 1. สลบ 100 กบ 32 32 70 56 14 65 100 2. ปรบ heap 70 65 56 14 32 100

123

Page 124: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

[1] [2] [3] [4] [5] [6] 3. สลบ 70 กบ 32 32 65 56 14 70 100 4. ปรบ heap 65 32 56 14 70 100 5. สลบ 65 กบ 14 14 32 56 65 70 100 6. ปรบ heap 56 32 14 65 70 100 7. สลบ 56 กบ 14 14 32 56 65 70 100 8. ปรบ heap 32 14 56 65 70 100 9. สลบ 32 กบ 14 14 32 56 65 70 100 10. ปรบ heap 14 32 56 65 70 100 11. สนสดการเรยงล าดบ 14 32 56 65 70 100

124

Page 125: Blue Bubbles Template 6 Tree.pdf · 235012 : Data Structure and Algorithms การเรียกชื่อส่วนต่างๆ ของต้นไม้ โหนดราก

1. จงสราง min-heap จากขอมลตอไปน 23 15 17 13 31 10 2 4 29 14 5 2. จงแสดงวธการลบขอมลจาก min-heap ทไดจากขอ 1 ทละขนตอน

125

การบาน