A Heap Is Efficiently Represented As An Array

Slides:



Advertisements
Similar presentations
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)
Advertisements

Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Binomial Heaps. Min Binomial Heap Collection of min trees
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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.
Heap: A Special Kind of Tree
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Priority Queues, Heaps & Leftist Trees
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.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
SNU IDB Lab. Ch.13 Priority Queues © copyright 2006 SNU IDB Lab.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Heaps & Priority Queues
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Leftist Trees Linked binary tree.
Sorting With Priority Queue In-place Extra O(N) space
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
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,
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
October 30th – Priority QUeues
Binary Search Tree Chapter 10.
Hashing Exercises.
Source: Muangsin / Weiss
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps.
Dynamic Dictionaries Primary Operations: Additional operations:
Splay Trees Binary search trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Interval Heaps Complete binary tree.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Pairing Heaps Actual Complexity.
Chapter 8 – Binary Search Tree
Initializing A Max Heap
Part-D1 Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
CMSC 341 Lecture 19.
Heaps By JJ Shepherd.
การวิเคราะห์และออกแบบขั้นตอนวิธี
CS 6310 Advanced Data Structure Wei-Shian Wang
Pairing Heaps Actual Complexity
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps Section 6.4, Pg. 309 (Section 9.1).
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

A Heap Is Efficiently Represented As An Array 9 8 6 7 2 5 1 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10

Moving Up And Down A Heap 9 8 6 7 2 5 1 3 4

Inserting An Element Into A Max Heap 9 8 6 7 2 5 1 7 Complete binary tree with 10 nodes.

Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 5 7 New element is 5.

Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 7 7 New element is 20.

Inserting An Element Into A Max Heap 9 8 7 6 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 Complete binary tree with 11 nodes.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.

Inserting An Element Into A Max Heap 20 9 7 6 2 6 5 1 7 7 8 7 8 New element is 15.

Inserting An Element Into A Max Heap 20 15 7 6 9 2 6 5 1 7 7 8 7 8 New element is 15.

Complexity of Inserting 8 6 7 2 5 1 20 9 15 Complexity is O(log n), where n is heap size.

Deleting the Max Element 8 6 7 2 5 1 20 9 15 Max element is in the root.

Deleting the Max Element 15 7 6 9 2 6 5 1 7 7 8 7 8 After max element is removed.

Deleting the Max Element 15 7 6 9 2 6 5 1 7 7 8 7 8 Heap with 10 nodes. Reinsert 8 into the heap.

Deleting the Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Deleting the Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Deleting the Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Deleting the Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Max element is 15.

Deleting the Max Element 9 7 6 8 2 6 5 1 7 7 7 After max element is removed.

Deleting the Max Element 9 7 6 8 2 6 5 1 7 7 7 Heap with 9 nodes.

Deleting the Max Element 9 7 6 8 2 6 5 1 Reinsert 7.

Deleting the Max Element 9 7 6 8 2 6 5 1 Reinsert 7.

Deleting the Max Element 9 8 7 6 7 2 6 5 1 Reinsert 7.

Complexity of Remove Max Element 6 2 5 1 7 9 8 Complexity is O(log n).

Initializing a Max Heap 8 4 7 6 9 3 10 1 11 5 2 input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Initializing a Max Heap 1 2 3 4 5 6 7 8 9 7 7 11 10 8 Start at rightmost array position that has a child. Index is n/2.

Initializing a Max Heap 1 2 3 4 11 6 7 8 9 10 7 7 5 8 Move to next lower array position.

Initializing a Max Heap 1 2 3 4 11 6 7 8 9 7 7 5 10 8

Initializing a Max Heap 1 2 3 9 11 6 7 8 4 7 7 5 10 8

Initializing a Max Heap 1 2 3 9 11 6 7 8 4 7 7 5 10 8

Initializing a Max Heap 1 2 7 9 11 6 3 8 4 7 7 5 10 8

Initializing a Max Heap 1 2 7 9 11 6 3 8 4 7 7 5 10 8

Initializing a Max Heap 1 11 7 9 6 3 8 4 10 7 7 5 8 Find a home for 2.

Initializing a Max Heap 1 11 7 9 10 6 3 8 4 8 7 7 5 Find a home for 2.

Initializing a Max Heap 1 11 7 9 10 6 3 8 4 7 7 2 8 5 Done, move to next lower array position.

Initializing a Max Heap 1 11 7 9 10 6 3 8 4 7 7 2 5 8 Find home for 1.

Initializing a Max Heap 11 7 9 10 6 3 8 4 7 7 2 8 5 Find home for 1.

Initializing a Max Heap 11 10 7 9 6 3 8 4 7 7 2 5 8 Find home for 1.

Initializing a Max Heap 11 10 7 9 5 6 3 8 4 7 7 2 8 Find home for 1.

Initializing a Max Heap 11 10 7 9 5 6 3 8 4 7 7 2 1 8 Done.

Time Complexity Height of heap = h. 11 9 7 8 5 6 3 1 4 10 7 7 2 8 Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1).

Complexity Time for level j subtrees is <= 2j-1(h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) = O(n).

Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time.

Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.

A Binary Tree

An Extended Binary Tree number of external nodes is n+1

The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

s() Values Example

s() Values Example 2 2 1 2 1 1 1 1 1

Properties of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1 s() values may be computed easily using a postorder traversal.

Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))

A Leftist Tree 2 2 1 2 1 1 1 1 1

Leftist Trees--Property 1 In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).

A Leftist Tree 2 2 1 2 1 1 1 1 1 Length of rightmost path is 2.

Leftist Trees—Property 2 The number of internal nodes is at least 2s(root) - 1 Because levels 1 through s(root) have no external nodes. So, s(root) <= log(n+1)

A Leftist Tree Levels 1 and 2 have no external nodes. 2 2 1 2 1 1 1 1 1 1 1 Levels 1 and 2 have no external nodes.

Leftist Trees—Property 3 Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree. Follows from Properties 1 and 2.

Leftist Trees as Priority Queues Min leftist tree … leftist tree that is a min tree. Used as a min priority queue. Max leftist tree … leftist tree that is a max tree. Used as a max priority queue.

A Min Leftist Tree 2 4 3 6 8 5 9 8 6

Some Min Leftist Tree Operations insert() delete() meld() initialize() insert() and delete() use meld().

Insert Operation insert(7) 2 4 3 6 8 5 9 8 6

Insert Operation insert(7) Create a single node min leftist tree. 2 4 3 6 8 5 9 8 6 Create a single node min leftist tree. 7

Insert Operation insert(7) Create a single node min leftist tree. 2 4 3 6 8 5 9 8 6 Create a single node min leftist tree. Meld the two min leftist trees. 7

Delete Min 2 4 3 6 8 5 9 8 6

Delete Min 2 4 3 6 8 5 9 8 6 Delete the root.

Delete Min 2 4 3 6 8 5 9 8 6 Delete the root. Meld the two subtrees.

Meld Two Min Leftist Trees 8 6 9 5 4 3 Traverse only the rightmost paths so as to get logarithmic performance.

Meld Two Min Leftist Trees 4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 6 4 6 8 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 6 8 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.

Meld Two Min Leftist Trees 8 6 Make melded subtree right subtree of smaller root. 6 8 Swap left and right subtree if s(left) < s(right). 6 8

Meld Two Min Leftist Trees 8 6 4 8 6 4 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

Meld Two Min Leftist Trees 9 5 3 8 6 4 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

Meld Two Min Leftist Trees 3 4 5 6 6 9 8 6 8

Initializing in O(n) Time Create n single node min leftist trees and place them in a FIFO queue Repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue The process terminates when only 1 min leftist tree remains in the queue Analysis is the same as for heap initialization