- 58 Views
- Uploaded on
- Presentation posted in: General

Review of Chapter 9

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Review of Chapter 9

張啟中

Max PQ

Min PQ

Min Heap

DEPQ

Max Heap

Mergeable Min PQ

Deap

Min-Max

Symmetric Max Data Structures

Min-Leftist

Min-Skew

MinFHeap

MinBHeap

- A double-ended priority queue is a data structure that supports the following operations:
- inserting an element with an arbitrary key
- deleting an element with the largest key
- deleting an element with the smallest key

- There are two kinds of DEPQ
- Min-Max Heap
- Deap

- Definition
A min-max heap is a complete binary tree such that if it is not empty,

- Each element has a data member called key.
- Alternating levels of this tree are min levels and max levels, respectively.
- The root is on a min level.
- Let x be any node in a min-max heap. If x is on a min (max) level then the element in x has the minimum (maximum) key from among all elements in the subtree with root x. A node on a min (max) level is called a min (max) node.

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

max

- Step 1
- 將欲新增的元素插入 Min-Max Heap 最後一個節點

- Step 2
- 將新增節點與其父節點做比較，若父節點位於 Min (Max) Level，且新增節點小於 (大於) 父節點，則交換二者的位置。

- Step 3
- 若交換後新增節點的位於 Min (Max) Level，則依序往上與各 Min (Max) Level 的節點比較，若新增節點較小(大)，則二者交換。

- Step 4
- 重複 Step 3，直至不能再交換或到 root 為止。

O(logn)

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

5

max

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

80

max

- Step 1
- 刪除 root

- Step 2
- 將最後一個節點刪除，並重新插入 Min-Max Heap的 root

- Step 3
- Case 1 The root has no children.
- In this case x is to be inserted into the root.

- Case 2 The root has at least one child.
- Find the smallest key from the children or grandchildren of root.
- Assume node k has the smallest key. x is inserted node.
- x.key <= h[k].key Insert x to the root
- x.key > h[k].key and k is child of the root
Interchange x and k

- x.key > h[k].key and k is grandchild of the root
(1) h[k] is moved to the root.

(2) Let p is parent of node k. If x.key > h[p].key then h[p] and x

are to interchanged. Repeat Step 3 with root k.

- Case 1 The root has no children.

O(logn)

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

max

- Definition
A deap is a complete binary tree that is either empty or satisfies the following properties

- The root contains no element.
- The left subtree is a min heap.
- The right subtree is a max heap.
- If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such a j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.

5

45

10

8

25

40

20

15

19

9

30

Max Heap

Min Heap

- Step 1
- 將新增的元素插入 Deap 的最後面一個節點

- Step 2
- 與新增節點相對位置的節點比較大小，若該新增節點的位於 Max (Min) Heap，則新增節點必須大於相對節點，否則必須交換。

- Step 3
- 若新增節點位於 Min (Max) Heap，則依 Min (Max) Heap 的新增方式進行。

5

45

10

8

25

40

20

15

19

9

30

4

j

i

5

45

10

8

25

40

20

15

19

9

30

30

j

i

- Step 1
- 刪除 Min Heap 的 root

- Step 2
- 比較 Min Heap 中 root 的兩個 child，將較小的值移到 root，直到 leaf 為止。

- Step 3
- 將最後一個節點刪除，插入空出的 Leaf，並依照 Deap 新增的方式操作

5

45

10

8

25

40

j

20

15

19

9

30

i