Heaps and the Heapsort Heaps and priority queues

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Week 6 - Wednesday CS221.
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Interval Heaps Complete binary tree.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Priority Queue & Heap CSCI 3110 Nan Chen.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Heap Sort The Heap Data Structure
CE 221 Data Structures and Algorithms
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Data Structures and Analysis (COMP 410)
CSE 373 Data Structures and Algorithms
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
B-Trees.
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Heaps.
CSE 373: Data Structures and Algorithms
Presentation transcript:

Heaps and the Heapsort Heaps and priority queues Heap structure property Heap ordering property Removal of top priority node Inserting a new node into the heap The heap sort

Heaps and priority queues A heap is a data structure used to implement an efficient priority queue. The idea is to make it efficient to extract the element with the highest priority ­ the next item in the queue to be processed. We could use a sorted linked list, with O(1) operations to remove the highest priority node and O(N) to insert a node. Using a tree structure will involve both operations being O(log2N) which is faster.

Heap structure and position numbering 1 A heap can be visualised as a binary tree in which every layer is filled from the left. For every layer to be full, the tree would have to have a size exactly equal to 2n­1, e.g. a value for size in the series 1, 3, 7, 15, 31, 63, 127, 255 etc. So to be practical enough to allow for any particular size, a heap has every layer filled except for the bottom layer which is filled from the left.

Binary Heap Properties Structure Property Ordering Property Describe these for a binary search tree. For an AVL tree.

Tree Review A DEFJ..NI Its parent or parent’s ancestor Tree T A root(T): leaves(T): children(B): parent(H): siblings(E): ancestors(F): descendents(G): subtree(C): A DEFJ..NI B C D E F G H I Trees have many uses, and we’ll take about them later. To start, let’s just review the basics. Let’s review the words: root: A leaf: DEFJKLMNI child:A - C or H - K leaves have no children parent: C - A or L - H the root has no parent sibling: D - E or F or J - K,L,M, or N grandparent: G to A grandchild: C to H or I ancestor: the node itself or any ancestor’s parent descendent: the node itself or any child’s descendent subtree: a node and all its descendents Its parent or parent’s ancestor Its child or child’s descendent J K L M N Itself plus all descendents

More Tree Terminology # edges on path from root to self depth(B): height(G): degree(B): branching factor(T): # edges on path from root to self # edges on longest path from node to leaf B C # children of a node D E F G MAX # children of a node for a BST this is?? H I Depth: the number of edges along the path from the root to the node height: the number of edges along the longest path from the node to a leaf degree: the number of children of the node branching factor: the maximum degree of any node (or sometimes the average) preorder traversal: running through all the nodes in the tree, starting with the parent, then all the children postorder traversal: run through all the nodes starting with the children and then the parents J K L M N

Some Definitions: skip A Perfect binary tree – A binary tree with all leaf nodes at the same depth. All internal nodes have 2 children. height h 2h+1 – 1 nodes 2h – 1 non-leaves 2h leaves 11 5 21 This is NOT a heap! (but it has the heap structure property) 2 9 16 25 1 3 7 10 13 19 22 30

Heap Structure Property A binary heap is a complete binary tree. Complete binary tree – binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: Since they have this regular structure property, we can take advantage of that to store them in a compact manner. Since they have this regular structure property, we can take advantage of that to store them in a compact manner. If I asked to you store a plain old binary tree in an array, how would you do it?

Representing Complete Binary Trees in an Array 1 2 3 4 5 6 9 8 10 11 12 A From node i: left child: right child: parent: B C 2 * i 7 D E F G (2 * i)+1 H I J K L └ i / 2┘ Left child of node I = 2 * I Right child I = (2*i) +1 Parent of node I is at i/2 (floor) implicit (array) implementation: A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13

Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. This is the order for a MIN heap – could do the same for a max heap. 10 10 20 80 20 80 Tree is PARTIALLY ORDERED. For each node: its value is less than value of all of its descendants. This is the definition for a MIN heap – could do the same for a max heap. 40 60 85 99 30 15 50 700 not a heap This is a PARTIAL order (diff than BST) For each node, its value is less than all of its descendants (no distinction between left and right)

Heap Operations findMin: insert(val): percolate up. How? findMin: insert(val): percolate up. deleteMin: percolate down. Is the tree unique? Swap 85 and 99. Swap 700 and 85? 10 20 80 Is the tree unique? Swap 85 and 99. Swap 700 and 85? 40 60 85 99 50 700 65

Heap – Insert(val) Basic Idea: Put val at “next” leaf position Percolate up by repeatedly exchanging node until no longer needed How long does this take? How long does this take? – max # of exchanges = O(log N) On “average” only need to move up 1.67 levels so get O(1) How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate up to root. On average, analysis shows that you tend to only need to move up 1.67 levels, so get O(1) on average. Optimization in the book – bubble up an EMPTY space, and then do a swap (reduces the # of swaps).

Insert: percolate up 10 Now insert 90. (no swaps, even though 99 is larger!) Now insert 7. 20 80 40 60 85 99 50 700 65 15 Optimization, bubble up an empty space to reduce # of swaps 10 15 80 Now insert 90. (no swaps, even though 99 is larger!) Now insert 7. 40 20 85 99 50 700 65 60

Heap – DeleteMin Basic Idea: Remove root (that is always the min!) Put “last” leaf node at root Find smallest child of node Swap node with its smallest child if needed. Repeat steps 3 & 4 until no swaps needed. If I < = both children, then you are done. How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate all the way to the bottom level. In fact this is often the case, you just took the value from the bottom, there is a good chance it has to go down to the lowest level. O(log N) Optimization in the book – bubble up an EMPTY space down, and then do a swap (reduces the # of swaps). Max # of exchanges? = O(log N), there is a good chance goes to bottom

DeleteMin: percolate down Max # of exchanges? = O(log N), There is a good chance goes to bottom (started at bottom) vs. insert - Could also use the percolate empty bubble down 10 20 15 40 60 85 99 50 700 65 15 Deletemein 20 65 40 60 85 99 50 700

Working on Heaps What are the two properties of a heap? Structure Property Order Property How do we work on heaps? Fix the structure Fix the order

BuildHeap: Floyd’s Method 10 11 12 0 1 2 3 5 11 3 10 6 9 4 8 1 7 2 12 Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 12 Red nodes need to percolate down 5 11 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 3 10 6 9 4 8 1 7 2

BuildHeap: Floyd’s Method 10 11 12 0 1 2 3 12 5 11 Red nodes need to percolate down 3 10 6 9 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 4 8 1 7 2

BuildHeap: Floyd’s Method 12 5 11 3 10 2 9 4 8 1 7 6

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 5 2 3 1 6 9 4 8 10 7 11

BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11

Finally… - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof. 1 3 2 4 5 6 9 How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. Most nodes move only a small number of steps  the runtime is O(n). (see text for proof) Full sum = (I=0 to height) SUM (h-I) * 2^i 12 8 10 7 11 runtime:

Facts about Heaps Observations: Realities: Finding a child/parent index is a multiply/divide by two Operations jump widely through the heap Each percolate step looks at only two new nodes Inserts are at least as common as deleteMins Realities: Division/multiplication by powers of two are equally fast Looking at only two new pieces of data: bad for cache! With huge data sets, disk accesses dominate At library – writing a research paper Now, let’s look at some facts about heaps and see if we can’t come up with a new priority queue implementation.

Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } Adding the items one at a time is O(n log n) in the worst case