Review of chapter 9
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Review of Chapter 9 PowerPoint PPT Presentation


  • 36 Views
  • Uploaded on
  • Presentation posted in: General

Review of Chapter 9. 張啟中. Inheritance Hierarchy. 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. Double-Ended Priority Queue.

Download Presentation

Review of Chapter 9

An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Review of chapter 9

Review of Chapter 9

張啟中


Inheritance hierarchy

Inheritance Hierarchy

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


Double ended priority queue

Double-Ended Priority Queue

  • 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


Min max heaps

Min-Max Heaps

  • 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.


Example

Example

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

max


Insertion of a min max heap

Insertion of a Min-Max Heap

  • Step 1

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

  • Step 2

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

  • Step 3

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

  • Step 4

    • 重複 Step 3,直至不能再交換或到 root 為止。


Insertion of a min max heap1

Insertion of a Min-Max Heap

O(logn)

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

5

max


Insertion of a min max heap2

Insertion of a Min-Max Heap

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

80

max


Deletion of the min element

Deletion of The Min Element

  • Step 1

    • 刪除 root

  • Step 2

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


Deletion of the min element1

Deletion of The Min Element

  • 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.


Deletion of the min element2

Deletion of The Min Element

O(logn)

7

min

70

40

max

30

9

10

15

min

45

50

30

20

12

max


Deaps

Deaps

  • 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.


Review of chapter 9

Deap

5

45

10

8

25

40

20

15

19

9

30

Max Heap

Min Heap


Insertion into a deap

Insertion Into a Deap

  • Step 1

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

  • Step 2

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

  • Step 3

    • 若新增節點位於 Min (Max) Heap,則依 Min (Max) Heap 的新增方式進行。


Insertion into a deap1

Insertion Into a Deap

5

45

10

8

25

40

20

15

19

9

30

4

j

i


Insertion into a deap2

Insertion Into a Deap

5

45

10

8

25

40

20

15

19

9

30

30

j

i


Deletion of min element

Deletion of Min Element

  • Step 1

    • 刪除 Min Heap 的 root

  • Step 2

    • 比較 Min Heap 中 root 的兩個 child,將較小的值移到 root,直到 leaf 為止。

  • Step 3

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


Deletion of min element1

Deletion of Min Element

5

45

10

8

25

40

j

20

15

19

9

30

i


  • Login