HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

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

1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 261 – Data Structures Priority Queues & Heaps.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Outline Priority Queues Binary Heaps Randomized Mergeable Heaps.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
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.
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 A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
2 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)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
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.
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 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
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Priority Queues Linked-list Insert Æ Æ head head
ADT Heap data structure
Chapter 8 – Binary Search Tree
Tree Representation Heap.
Heaps A heap is a binary tree.
CSCE 3110 Data Structures and Algorithm Analysis
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.
Ch. 12 Tables and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
CSCE 3110 Data Structures and Algorithm Analysis
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
EE 312 Software Design and Implementation I
Presentation transcript:

HEAPS

Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key value, depending on application.). What data structures may be used to implement a priority queue? What are the advantages/disadvantages of each? Arrays: removal quick but insertion slow O(log n) to find position but O(n) for shifts Linked lists: removal quick but insertion slow - O(n) to find position and O(1) to relink.

Def. Heap - a binary tree that is: completely filled in reading from left to right across each level, except the last level may not be full, and the key in each node is greater than (or equal to) both of its children’s keys. Note Since the greatest key is always at the top, a heap is a good data structure to implement a priority queue. Although removal is only O(log n) time, insertion is also O(log n) - better than arrays and linked lists.

Note: The highest priority key is always accessible at the top, but removing it produces a hole, which takes the same no. of moves as the height of the tree to fill.

Although we visualize a heap as a linked binary tree, we usually implement it as an array

Remember the children of node n are stored in locations: 2n + 1 and 2n + 2 if the first object is stored in location zero in the array but in 2n and 2n+1 when the first object is stored in location one in the array and the first location is left empty. Question: what is the location of the parent of the child at location x? (x - 1)/2 or x/2 if zero is left empty Question: does a heap provide easy searching?no Also, a heap does not provide easy traversal of nodes in order. But, a heap provides easy removal of the highest key and easy insertion - both O(log n) using the “trickleup()” and “trickledown()” methods - all that is needed to implement a priority queue.

“trickledown()” method needed for removal: After copying node with highest priority, replace it with the last node in the heap. Now the tree is not a heap! so re-heap Swap the new root node with the greater of its children. Repeat that process down to the lowest level. Remove the root node from the previous sample.

Num

Num 100

Num 100

Num NOTE: the last item is not really removed, only the length of the array is decremented.

public Node remove() // delete item with max key { // (assumes non-empty list) Node root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root; } // end remove()

public void trickleDown(int index) { int largerChild; Node top = heapArray[index]; // save root while(index < currentSize/2) // while node has at { // least one child, int leftChild = 2*index+1; int rightChild = leftChild+1; // find larger child if(rightChild < currentSize && // (rightChild exists?) heapArray[leftChild].iData < heapArray[rightChild].iData) largerChild = rightChild; else // what is passed to index? 0

largerChild = leftChild; // top >= largerChild? if(top.iData >= heapArray[largerChild].iData) break; // shift child up heapArray[index] = heapArray[largerChild]; index = largerChild; // go down } // end while heapArray[index] = top; // root to index } // end trickleDown()

“trickleup()” method is needed for insertion: Insert new node at bottom of tree (at heap[heap.length] then increment length. Compare with parent and swap if needed. Repeat until root node is reached.

public boolean insert(int key) { if(currentSize==maxSize) return false; Node newNode = new Node(key); heapArray[currentSize] = newNode; trickleUp(currentSize++); return true; } // end insert()

public void trickleUp(int index) { int parent = (index-1) / 2; Node bottom = heapArray[index]; while( index > 0 && heapArray[parent].iData < bottom.iData ) { heapArray[index] = heapArray[parent]; // move it down index = parent; parent = (parent-1) / 2; } // end while heapArray[index] = bottom; } // end trickleUp()

Note: trickledown() and trickleup() require the most work for removal and insertion, but that is never more than the no. of levels of the tree. no.node min no.levels height Note: height = log 2 n =(int) log 2 n No. levels = height + 1

Heapsort - another use of the adt heap. The heap provides a simple and efficient sorting algorithm: Insert items from an unordered array into a heap then Remove from the heap and replace in the original array: for (j = 0; j < size; j++) theHeap.insert( anArray[j]); for (j = 0; j < size; j++) anArray[j] = theHeap.remove(); Since insert and remove operate in O(log n) time and each must be executed for every node in the array (n) the heapsort operates in O(n*log n ) time. What other sorts run in that time? Quicksort & mergesort

Question: what is the disadvantage of the heapsort? Space requirement: 2 arrays needed This heapsort algorithm space requirement can be improved by placing the random data into a heap within the same array then: 1. Remove top and hold temporarily. 2. Trickledown() value that was put in place of top. 3. Copy temp to position of last value that had been put in place of top. Question: In what order is the data after this heapsort? Answer: ascending order.

To convert an array of random data to a heap within the array itself you may use an iterative approach or a recursive method. Iteratively this would be: for (j = size/2 - 1; j >= 0; j--) theHeap.trickleDown(j) Where in the binary tree is the item with index size/2 - 1? The rightmost node that has children. (Remember: the number of leaves = number of internal nodes +1.) Starting there to convert the random array into a heap with the array itself, trickledown is called about half as many times as it is in the insert() method.

Random array: Start trickledown here (only 1 swap needed this time)

The array is now a heap

Use the remove() method and insert() at bottom to sort within the array itself. Now trickledown() Remember remove decrements heap size.

Remove biggest and insert at bottom (of what is left of heap) trickledown() Remove biggest and insert at bottom (of what is left of heap) trickledown()

Remove biggest and insert at bottom (of what is left of heap) trickledown() Remove biggest and insert at bottom (of what is left of heap) No trickledown() needed