Initializing A Max Heap

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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Dr. Andrew Wallace PhD BEng(hons) EurIng
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
Priority Queues, Heaps & Leftist Trees
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
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 Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
"Teachers open the door, but you must enter by yourself. "
Priority Queues An abstract data type (ADT) Similar to a queue
Heaps, Heap Sort and Priority Queues
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,
October 30th – Priority QUeues
Binary Search Tree Chapter 10.
Hashing Exercises.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
CMSC 341 Lecture 13 Leftist Heaps
Heaps.
Dynamic Dictionaries Primary Operations: Additional operations:
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
Priority Queue & Heap CSCI 3110 Nan Chen.
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
Priority Queues.
Ch 6: Heapsort Ming-Te Chi
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Heaps 11/27/ :05 PM Heaps Heaps.
Priority Queues.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
"Teachers open the door, but you must enter by yourself. "
CS6045: Advanced Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
Heapsort Sorting in place
Algorithms: Design and Analysis
CMSC 341 Lecture 19.
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.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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.  Heapify

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

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 Move to next lower array position and Heapify.

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 Move to next lower array position and Heapify.

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 and Heapify.

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.

Initializing a Max Heap* * Source: CLRS 3rd edition

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 empty() size() top() push() pop() meld() initialize() push() and pop() use meld().

Push Operation push(7) 2 4 3 6 8 5 9 8 6

Push Operation push(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

Push Operation push(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

Remove Min (pop) 2 4 3 6 8 5 9 8 6

Remove Min (pop) 2 4 3 6 8 5 9 8 6 Remove the root.

Remove Min (pop) Remove the root. Meld the two subtrees. 2 4 3 6 8 5 9

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 FIFO queue analysis is the same as for heap initialization