CSE 373: Data Structures and Algorithms

Slides:



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

Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
CS 261 – Data Structures Priority Queues & Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
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 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
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
A tree set Our SearchTree class is essentially a set.
Section 9.2b Adding and Removing from a Heap.
Binary Heaps What is a Binary Heap?
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Priority Queues Linked-list Insert Æ Æ head head
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
ADT Heap data structure
- Alan Perlis Heaps "You think you know when you can learn,
Chapter 8 – Binary Search Tree
Priority Queue & Heap CSCI 3110 Nan Chen.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
CSE 373 Data Structures and Algorithms
Tree Representation Heap.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Hassan Khosravi / Geoffrey Tien
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
"Teachers open the door, but you must enter by yourself. "
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Building Java Programs
Binary Search Trees continued; Tree Sets
slides created by Marty Stepp
Priority Queues & Heaps
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 373 Data Structures and Algorithms
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Heaps By JJ Shepherd.
CSE 373: Data Structures and Algorithms
Implementing a Priority Queue
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 14: Priority Queues (Heaps) II

Adding to a heap, cont'd. to restore heap ordering property, the newly added element must be shifted upward ("bubbled up") until it reaches its proper place bubble up (book: "percolate up") by swapping with parent how many bubble-ups could be necessary, at most? 10 10 20 80 15 80 40 60 85 99 40 20 85 99 50 700 65 15 50 700 65 60

Heap practice problem Draw the state of the min-heap tree after adding the following elements to it: 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

Code for add method public void add(int value) { // grow array if needed if (size >= array.length - 1) { array = this.resize(); } // place element into heap at bottom size++; int index = size; array[index] = value; bubbleUp();

The bubbleUp helper private void bubbleUp() { int index = this.size; while (hasParent(index) && (parent(index) > array[index])) { // parent/child are out of order; swap them swap(index, parentIndex(index)); index = parentIndex(index); } // helpers private boolean hasParent(int i) { return i > 1; } private int parentIndex(int i) { return i/2; } private int parent(int i) { return array[parentIndex(i)]; }

The peek operation peek on a min-heap is trivial; because of the heap properties, the minimum element is always the root peek is O(1) peek on a max-heap would be O(1) as well, but would return you the maximum element and not the minimum one 99 60 40 80 20 10 50 700 85 65

Code for peek method public int peek() { if (this.isEmpty()) { throw new IllegalStateException(); } return array[1];

Removing from a min-heap min-heaps support remove of the min element (the root) must remove the root while maintaining heap completeness and ordering properties intuitively, the last leaf must disappear to keep it a heap initially, just swap root with last leaf (we'll fix it) 10 65 20 80 20 80 40 60 85 99 40 60 85 99 700 50 65 700 50 65

Removing from heap, cont'd. must fix heap-ordering property; root is out of order shift the root downward ("bubble down") until it's in place swap it with its smaller child each time What happens if we don't always swap with the smaller child? 65 20 20 80 40 80 40 60 85 99 50 60 85 99 700 50 700 65

Heap practice problem The heap below is the min-heap built in the last heap practice problem. Now, show the state of the heap after remove has been executed on it 3 times, and state which elements are returned by the removal. 104 42 25 6 19 2 76 50 11 55 88 20

Code for remove method public int remove() { int result = this.peek(); // move last element of array up to root array[1] = array[size]; array[size] = 0; size--; bubbleDown(); return result; }

The bubbleDown helper private void bubbleDown() { int index = 1; while (hasLeftChild(index)) { int childIndex = leftIndex(index); if (hasRightChild(index) && (array[rightIndex(index)] < array[leftIndex(index)])) { childIndex = rightIndex(index); } if (array[childIndex] < array[index]) { swap(childIndex, index); index = childIndex; } else { break; // helpers private int leftIndex(int i) { return i * 2; } private int rightIndex(int i) { return i * 2 + 1; } private boolean hasLeftChild(int i) { return leftIndex(i) <= size; } private boolean hasRightChild(int i) { return rightIndex(i) <= size; }

Advantages of array heap the "implicit representation" of a heap in an array makes several operations very fast add a new node at the end (O(1)) from a node, find its parent (O(1)) swap parent and child (O(1)) a lot of dynamic memory allocation of tree nodes is avoided the algorithms shown usually have elegant solutions

Generic Collection Implementation

PrintJob Class public class PrintJob { private String user; private int number; private int priority; public PrintJob(int number, String user, int priority) { this.number = number; this.user = user; this.priority = priority; } public String toString() { return this.number + " (" + user + "):" + this.priority;

Type Parameters (Generics) ArrayList<Type> name = new ArrayList<Type>(); Recall: When constructing a java.util.ArrayList, you specify the type of elements it will contain between < and >. We say that the ArrayList class accepts a type parameter, or that it is a generic class. ArrayList<String> names = new ArrayList<String>(); names.add("Kona"); names.add("Daisy");

Implementing generics // a parameterized (generic) class public class name<Type> { ... } By putting the Type in < >, you are demanding that any client that constructs your object must supply a type parameter. The rest of your class's code can refer to that type by name. Exercise: Convert our priority queue classes to use generics.