單元7: Heap 定義 for priority queues Leftist trees Binomial heap

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
Advanced Data structure
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Min-Max Heaps A double-ended priority queue is a data structure that supports the following operations: inserting an element with an arbitrary key deleting.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Review of Chapter 5 張啟中. Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is.
Deletion in MIN-MAX Heaps Delete min element Delete max element.
DEAPS By: Michael Gresenz Austin Forrest. Deaps A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min,
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Review of Chapter 9 張啟中. Inheritance Hierarchy Min PQ Max PQ Min Heap Mergeable Min PQ DeapMin-Max Min-LeftistMin-SkewMinFHeap MinBHeap DEPQMax Heap Symmetric.
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
5.9 Heaps of optimal complexity
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Priority Queues, Heaps & Leftist Trees
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Binomial heaps, Fibonacci heaps, and applications.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Chapter 9 Heap Structures
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
Chap 9 Priority Queues. Operations supported by priority queue The functions that have been supported: –SP1: Return an element with minmum priority –SP2:
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Foundations of Data Structures Practical Session #8 Heaps.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
Double-Ended Priority Queues Primary operations  Insert  Remove Max  Remove Min Note that a single-ended priority queue supports just one of the above.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Chapter 05 Trees (Part I). Definition A tree is a finite set of one or more nodes including: ▫A starting node called root. ▫The rest of nodes can be split.
Leftist Trees Linked binary tree.
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Splay Trees Binary search trees.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Splay Trees Binary search trees.
Initializing A Max Heap
© 2013 Goodrich, Tamassia, Goldwasser
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
Heaps By JJ Shepherd.
CS 6310 Advanced Data Structure Wei-Shian Wang
Pairing Heaps Actual Complexity
A Heap Is Efficiently Represented As An Array
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

單元7: Heap 定義 for priority queues Leftist trees Binomial heap (Chapter 9) 定義 for priority queues Leftist trees Binomial heap Min-Max heap

1.定義 A double-ended priority queue is a data structure that supports the following operation: Return an element with minimum priority. Return an element with maximum priority. Insert an element with arbitrary priority. Delete an element with minimum priority. Delete an element with maximum priority.

2.Leftist Trees An extended binary tree Leftist trees are defined using the concept of an extended binary tree. An extended binary tree is a binary tree in which all empty binary subtrees have been replaced by a square node. The square nodes in an extended binary tree are called external nodes. The original nodes of the binary tree are called internal nodes. A B C D E

2.Leftist Trees An extended binary tree Define shortest(x) to be the length of a shortest path from x to an external node. 2 A 2 B C 1 1 1 D E shortest(x) = 1+ min {shortest(leftChild(x)), shortest(rightChild(x))}

2.Leftist Trees leftist tree 定義: A leftist tree is a binary tree such that if it is not empty, then shortest(LeftChild(x)) ≥ hortest(RightChild(x)) for every internal node x. 2 A 2 A 2 1 B C 1 B C 2 1 1 1 1 D E D E

2.Leftist Trees Lemma : Let x be the root of a leftist tree that has n (internal) nodes (a) n ≥ 2shortest(x) – 1 (b) The rightmost root to external node path is the shortest root to external node path. Its length is shortest(x) ≤ log2(n+1).

2.Leftist Trees A min leftist tree (max leftist tree) -is a leftist tree in which the key value in each node is no larger (smaller) than the key values in its children (if any). In other words, a min (max) leftist tree is a leftist tree that is also a min (max) tree.

2.Leftist Trees A min leftist tree ( operation) : Insert; delete; combine (meld) meld 1. 找兩者之root 相比, 較小者為root, 且其左child 留下。 2. 其右child 與另一較大之tree 以相同步驟combine, 結果作為right child。 3. 調整為符合leftist tree

2.Leftist Trees meld 2 2 2 5 1 7 50 1 1 1 9 2 8 1 1 2 1 11 80 12 10 50 1 1 1 1 1 20 18 1 13 15 80

2.Leftist Trees meld 2 2 2 5 1 7 1 2 8 1 1 9 11 1 2 10 1 12 50 1 13 1 1 1 1 15 20 18 80

2.Leftist Trees meld The complexity O(log n) 2 2 1 2 7 5 1 1 2 8 1 9 11 1 2 10 1 12 50 13 1 1 1 1 1 15 20 18 80 The complexity O(log n)

2.Leftist Trees insert(7) A min leftist tree ( operation) : 2 7 4 3 2 1 1 1 2 1 6 8 5 7 1 1 1 9 8 6

2.Leftist Trees DELETE(2) A min leftist tree ( operation) : 2 4 3 2 6 1 1 1 2 1 6 8 5 7 1 1 1 9 8 6

2.Leftist Trees DELETE(2) A min leftist tree ( operation) : 2 3 4 5 6 1 4 5 1 1 2 1 6 8 7 9 1 1 1 8 8 6

2.Leftist Trees DELETE(2) A min leftist tree ( operation) : 2 3 4 5 6 1 4 5 1 2 1 6 7 9 1 1 1 8 6 8

2.Leftist Trees Weight-Biased Leftist tree Define w(x) to be the number of internal nodes in the subtree with root x 4 2 A A 3 1 2 B C B C 1 1 1 1 1 D E D E w(x) = 1+ w(leftChild(x))+w(rightChild(x))

3. Binomial heap • A binomial heap is a data structure that supports the same functions as those supported by leftist trees. • Unlike leftist trees, where an individual operation can be performed in O(log n) time, it is possible that certain individual operations performed on a binomial heap may take O(n) time. • By amortizing part of the cost of expensive operations over the inexpensive ones, then the amortized complexity of an individual operation is either O(1) or O(log n) depending on the type of operations.

3. Binomial heap 定義: The binomial tree Bk, of degree k is a tree such that if k = 0, then the tree has exactly one node, and if k > 0, then the tree consists of a root whose degree is k and whose subtrees are B0, B1, …, Bk-1. B3 B0 B1 B2

3. Binomial heap operation:Insert, Delete-min, Meld 7 5 4 6 9 Delete-min 1 12 7 16 15 30 9 20

Analysis Of Binomial Heaps

4. Min-Max heap – inserting an element with an arbitrary key 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 A Min-Max Heap supports all of the above operations.

4. Min-Max heap 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.

4. Min-Max heap The min-max heap stores in a one-dimension array h min 7 max 70 40 30 9 10 15 min max 45 50 20 30 7 7 8 12 Insert 5

4. Min-Max heap Insert x Check parent i 為max 或min (1) min 7 7 x 8 If x<i then x 和i 互換 對x執行verifymin 以找正確位置 (2) max If x>i then x 和i 互換 對x執行verifymax 以找正確位置

4. Min-Max heap Insert 5 min max verifymin min max 7 70 40 30 9 10 15 45 50 7 7 20 max 30 8 12 5

4. Min-Max heap Insert 80 min max min verifymax max 5 70 40 30 9 7 15 45 50 7 7 20 max 30 8 12 10 80

4. Min-Max heap Delete of min element 將root (最小值)拿掉 準備delete array最後元素x,再重新insert 最後元素x 尋找第2小元素k If x < k then root 設為x else if k 為root 之child x k max 7 7 k 8 max 7 7 x 8

4. Min-Max heap Delete of min element (6) else if k 為root 之grandchild 7 7 max 7 7 max 7 7 max min 7 7 k min 8 7 7 x min 8 7 7 p 8 check check p > x p < x

4. Min-Max heap Delete of min element 7 x min max k min max 7 70 99 30 10 15 min 45 50 7 7 20 max 30 8 90

4. Min-Max heap Deaps • A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min, and delete-max. • Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler.

4. Min-Max heap Def: A deap is a complete binary tree that is either empty or satisfies the following properties: (1) The root contains no element (2) The left subtree is a min heap. (3) The right subtree is a max heap. (4) 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.

4. Min-Max heap Deaps 小 大 Max heap Min heap 5 45 10 8 25 40 15 19 7 7 30 9 8 20 Max heap Min heap 5 < 45 ; 10 <25 ; 8 <40; 15 < 20; 19 < 25; 9 < 40; 30 < 40

4. Min-Max heap Deaps-Insert 4 比較 Min heap Max heap 5 45 10 8 25 40 4 15 19 30 9 7 7 8 20 比較 Min heap Max heap

4. Min-Max heap Deaps-Insert 60 比較 Min heap Max heap 5 45 10 8 25 40 15 19 7 7 30 4 60 9 8 20 比較 Min heap Max heap

4. Min-Max heap Deaps-delete 5 Max heap Min heap 比較 5 45 10 8 25 19 15 18 10 20 9 7 7 8 Last element Max heap Min heap 比較