Tree 2 heap deap 2 3 tree 2

  • Slides: 50
Download presentation
Tree (2): heap, deap, 2 -3 tree, 2 -3 -4 tree Lai Ah Fur

Tree (2): heap, deap, 2 -3 tree, 2 -3 -4 tree Lai Ah Fur

Which are heaps? Nonheaps?

Which are heaps? Nonheaps?

Different heaps constructed with the same elements

Different heaps constructed with the same elements

create min heap 26 modify 5 15 77 61 1 48 26 5 59

create min heap 26 modify 5 15 77 61 1 48 26 5 59 11 26 19 19 1 15 77 48 5 59 11 15 26 15 11 19 48 61 48 11 61 1 1 5 19 1 61 77 77 5 59 15 26 48 11 19 61 77 59 59

建立min heap 26 heapify 5 5 Input 77 5 26 26 5 Input 1

建立min heap 26 heapify 5 5 Input 77 5 26 26 5 Input 1 26 77 1 heapify 1 5 26 Input 61, 11 1 1 heapify 77 5 26 61 5 77 11 n 26,5,77,1,61,11,59,15,48,19, 0 26 11 61 77 77

1 Input 59, 15 1 5 11 26 61 77 15 59 15 11

1 Input 59, 15 1 5 11 26 61 77 15 59 15 11 61 77 26 1 5 5 heapify 59 1 11 5 11 heapify Input 48, 19 15 26 61 77 48 19 59 15 26 19 77 48 61 n 26,5,77,1,61,11,59,15,48,19, 0 59

1 0 5 Input 0 15 26 11 19 77 48 61 0 1

1 0 5 Input 0 15 26 11 19 77 48 61 0 1 59 heapify 15 26 11 5 48 61 77 19 59

Enqueuing an element to a heap

Enqueuing an element to a heap

delqueuing an element to a heap

delqueuing an element to a heap

Deap n n n A DEAP is a double-ended heap that supports the double-ended

Deap n n n A DEAP is a double-ended heap that supports the double-ended priority queue operations of insert, delete min, and delete max. 完整二元樹 (a complete binary tree) 樹根節點不含資料項目,左子樹是min-heap,右 子樹是max-heap partner:左子樹節點i之鍵值必小於等於右子樹相 對位置節點j之鍵值 若節點j不存在,則節點i之鍵值必小於等於節點j之 父節點之鍵值. j=i+2└log i┘-1,if(j>n)j=j/2

Deap用途 n 作為double ended priority queue: insert an element with arbitrary key n delete

Deap用途 n 作為double ended priority queue: insert an element with arbitrary key n delete an element with the largest key n delete an element with the smallest key n

insert 5 45 5 Insert 4 8 10 15 19 9 25 10 29

insert 5 45 5 Insert 4 8 10 15 19 9 25 10 29 20 15 8 i 19 9 40 25 j 29 20 heapify 交換 5 45 8 10 15 40 45 4 9 40 25 29 20 5 19 15 45 8 4 10 9 40 25 29 20 19 4 比partner小(4<19), 必須 交換(move 19 to node j) heapify 8 5 15 45 10 9 40 25 29 20 19

insert 5 8 10 15 19 5 45 25 40 Insert 30 29 20

insert 5 8 10 15 19 5 45 25 40 Insert 30 29 20 9 10 15 19 45 8 9 40 25 29 20 30 比partner大(30>19), 不必交換 heapify 5 10 15 19 45 8 9 40 30 29 20 25

Delete: fetch max 4 8 5 15 10 40 25 30 20 9 15

Delete: fetch max 4 8 5 15 10 40 25 30 20 9 15 15 40 8 10 9 25 30 20 11 tmp 40 8 10 4 5 11 4 5 heapify 45 9 25 30 20 11 : 11>8(partner) : insert tmp into empty position

Delete min 5 15 8 10 19 19 20 30 20 9 8 15

Delete min 5 15 8 10 19 19 20 30 20 9 8 15 40 25 heapify 10 Fetch min 45 45 9 20 25 30 40 8 10 tmp 15 19 45 9 25 40 30 : 20<40(parent of partner 20), no change is needed, so insert 20 into the empty position

2 -3 tree 2 -node: 分支度(degree)為 2的節點,含 有一筆element n 3 -node: 分支度(degree)為 3的節點,含 有二筆element

2 -3 tree 2 -node: 分支度(degree)為 2的節點,含 有一筆element n 3 -node: 分支度(degree)為 3的節點,含 有二筆element n

An example 2 -3 tree A 40 C B 10 20 80

An example 2 -3 tree A 40 C B 10 20 80

Example of insertion < < A A 40 40 C B 10 20 80

Example of insertion < < A A 40 40 C B 10 20 80 C B 10 20 70 80 (a) 70 inserted 20 40 D B C 10 30 70 80 (b) 30 inserted B is 3 -node, so Create a new node D 最大值放D, 最小值放B, 中間值放B的parent

A 20 40 D B C 30 10 70 80 New node G A

A 20 40 D B C 30 10 70 80 New node G A 40 20 40 70 C D B 10 30 60 A F 20 E 80 70 D B 10 30 Insertion of 60 into the 2 -3 tree E C 60 80

The three cases for rotation in a 2 -3 tree r r y ?

The three cases for rotation in a 2 -3 tree r r y ? x ? a p q q p y z x z b c d a b c d (a) p is the left child of r r r y ? z ? q p x y a b c d p q x z a b c d (b) p is the middle child of r

w z a w y q p x y b c d a q

w z a w y q p x y b c d a q p x e b c (c) p is the right child of r z

Combining in a 2 -3 tree when p is a the left child of

Combining in a 2 -3 tree when p is a the left child of r r r x a p q p y x y b c a b c (a) r r z x z p q y a b c d p x y a b c (b) d

Deletion from a 2 -3 tree (1) A A 50 80 C B 10

Deletion from a 2 -3 tree (1) A A 50 80 C B 10 20 50 80 D 60 70 90 95 (a) Initial 2 -3 tree C B 10 20 D 60 90 95 (b) 70 deleted

Deletion from a 2 -3 tree (2) A 50 A 80 20 C B

Deletion from a 2 -3 tree (2) A 50 A 80 20 C B 10 20 D 60 95 (c) 90 deleted 80 C B 10 rotate D 50 95 (d) 60 deleted

Deletion from a 2 -3 tree (3) (e) 95 deleted (f) 50 deleted A

Deletion from a 2 -3 tree (3) (e) 95 deleted (f) 50 deleted A A 20 10 80 Combine: move 80 to D’s left sibling C, Delete D 10 80 80 combine (g) 10 deleted C B 20 • If C is 3 -node , then rotate 20 C B 50 B 20 C B New root 80 可續向上combine, 但已到root

2 -3 -4 tree: (2, 4) tree n n Every node has at most

2 -3 -4 tree: (2, 4) tree n n Every node has at most four children. 定義 n n n 2 -node: 分支度(degree)為 2的節點,含有一筆element • 3 -node: 分支度(degree)為 3的節點,含有二筆element • 4 -node: 分支度(degree)為 4的節點,含有三筆element • 內部節點可為 2 -node,3 -node或 4 -node的找尋樹 • All the external node have the same depth. 所有失敗節點( 外部節點)都在同一level The height of a (2, 4) tree storing n items is ⊝(log n)

Insert 4, 6, 12 2 node 3 node 4 node split Insert 15, cause

Insert 4, 6, 12 2 node 3 node 4 node split Insert 15, cause overflow

Insert 3 split Insert 5, cause overflow

Insert 3 split Insert 5, cause overflow

Insert 10 Insert 8

Insert 10 Insert 8

Example 2: insertion Insert 17, cause overflow split

Example 2: insertion Insert 17, cause overflow split

After split, a new overflow occur Another split, create a new root node

After split, a new overflow occur Another split, create a new root node

Delete 4, cause underflow The Rotate (transfer) operation Delete 12, cause underflow

Delete 4, cause underflow The Rotate (transfer) operation Delete 12, cause underflow

The combine (fusion operation) Delete 13

The combine (fusion operation) Delete 13

Delete 14, cause underflow Second fusion, cause the root to be removed fusion, cause

Delete 14, cause underflow Second fusion, cause the root to be removed fusion, cause another underflow

Transformation when the 4 node is the root t t x y z a

Transformation when the 4 node is the root t t x y z a b c y d z x a b c d

Transformation when the 4 -node is the child of a 2 -node x z

Transformation when the 4 -node is the child of a 2 -node x z z e e a b c y w w x y a d b c d (a) w y w a a x b y c d z x z e (b) b c d e

Transformation when the 4 -node is the left child of a 3 -node w

Transformation when the 4 -node is the left child of a 3 -node w y z e e f v w x x v a b c d f

Transformation when the 4 -node is the left middle child of a 3 -node

Transformation when the 4 -node is the left middle child of a 3 -node v x z v z a f f a w x y w y b c d e

Deletion transformation when the nearest sibling is a 3 -node p x z p

Deletion transformation when the nearest sibling is a 3 -node p x z p w z q v a q v w f r x y b c d e a f r y b c d e (a) q is the left child of a 3 -node p p w y z v y z q u a g r w x b c d q u v f e a b (b) q is the left child of a 4 -node r f x c d e g