Heaps.

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

 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
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 Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
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 © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
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)
Heap Chapter 9 Objectives Define and implement heap structures
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Heaps (8.3) CSE 2011 Winter May 2018.
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,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
Priority Queues Linked-list Insert Æ Æ head head
Chapter 2: Basic Data Structures
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queues and Heaps
Part-D1 Priority Queues
Dr. David Matuszek Heapsort Dr. David Matuszek
i206: Lecture 14: Heaps, Graphs intro.
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
CSC212 Data Structure - Section AB
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Ch. 8 Priority Queues And Heaps
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 and Priority Queues
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Heapsort.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Heaps and Priority Queues
1 Lecture 10 CS2013.
Heapsort.
B-Trees.
Heapsort.
Computer Algorithms CISC4080 CIS, Fordham Univ.
Heaps & Multi-way Search Trees
CO 303 Algorithm Analysis and Design
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
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.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Heaps

Heaps Definition Recall It must satisfy the following property: A heap is a certain kind of complete binary tree. Recall Complete Binary Tree Every level but lowest has all possible nodes If lowest level is not full, all nodes as far left as possible It must satisfy the following property: The entry contained by the node is NEVER less than the entries of the node’s children // this is called the heap property

Height of a Heap Theorem: A heap storing n keys has height h≤ log n Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2i keys at depth i = 0, … , h - 1 and at least one key at depth h, we have n  1 + 2 + 4 + … + 2h-1 + 1 Thus, n  2h , i.e., h  log n 1 2 h - keys depth

Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 27 21 22 4 19 42 We can add new elements to a heap whenever we like. Because the heap is a complete binary tree, we must add the new element at the next available location, filling in the levels from left-to-right. In this example, I have just added the new element with a key of 42. Of course, we now have a problem: The heap property is no longer valid. The 42 is bigger than its parent 27. To fix the problem, we will push the new node upwards until it reaches an acceptable location.

Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 42 21 22 4 19 27 Here we have pushed the 42 upward one level, swapping it with its smaller parent 27. We can't stop here though, because the parent 35 is still smaller than the new node 42.

Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 42 23 35 21 22 4 19 27 Can we stop now? Yes, because the 42 is less than or equal to its parent.

Adding a Node to a Heap 45 The parent has a key that is >= new node, or The node reaches the root. The process of pushing the new node upward is called reheapification upward. 42 23 35 21 22 4 19 27 In general, there are two conditions that can stop the pushing upward: 1. We reach a spot where the parent is >= the new node, or 2. We reach the root. This process is called reheapification upward). Note: we need to easily go from child to parent as well as parent to child.

Removing a Node from a Heap 45 Which node to remove? Remove the top of the Heap (the root node) !!! 42 23 35 21 22 4 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the 27 onto the root. 19 27

Removing a Node from a Heap 27 Reconstruct the complete binary tree property: Move the last node onto the root. 42 23 35 21 22 4 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the 27 onto the root. 19 27

Removing a Node from a Heap Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 27 42 We'll fix the problem by pushing the out-of-place node downward. Perhaps you can guess what the downward pushing is called....reheapification downward.

Removing a Node from a Heap Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 42 27 When we push a node downward it is important to swap it with its largest child. (Otherwise we are creating extra problems by placing the smaller child on top of the larger child.) This is what the tree looks like after one swap. Should I continue with the reheapification downward?

Removing a Node from a Heap Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 27 23 42 35 Yes, I swap again, and now the 27 is in an acceptable location.

Removing a Node from a Heap 19 4 22 21 27 23 42 35 All the children have keys <= the out-of-place node, or The node reaches the leaf. The process of pushing the new node downward is called reheapification downward. Reheapification downward can stop under two circumstances: 1. The children all have keys that are <= the out-of-place node. 2. The out-of-place node reaches a leaf.

Heap Implementation Use linked node Use arrays node implementation is for a general binary tree but we may need to have doubly linked node Use arrays A heap is a complete binary tree which can be implemented more easily with an array than with linked nodes and do two-way links

Array Representation of a Heap Recall For the node with index i the left child is at index 2i the right child is at index 2i + 1 Links between nodes are not explicitly stored The cell of at index 0 is not used

Implementing a Heap 42 We will store the data from the nodes in a partially-filled array. 35 23 27 21 This slide shows the typical way that a heap is implemented. For the most part, there is nothing new here, because you already know how to implement a complete binary tree using a partially-filled array. That is what we are doing with the heap. An array of data

Implementing a Heap 42 Data from the root goes in the first location of the array. 35 23 27 21 Following the usual technique for implementing a complete binary tree, the data from the root is stored in the first entry of the array. 42 An array of data

Implementing a Heap 42 Data from the next row goes in the next two array locations. 35 23 27 21 The next two nodes go in the next two locations of the array. 42 35 23 An array of data

Implementing a Heap 42 Data from the next row goes in the next two array locations. 35 23 27 21 and so on. 42 35 23 27 21 An array of data

implementation in an ADT: array and size Comparable[] a; int size; private boolean full() { // first element of array is empty return (size == a.length-1); }

implementation in an ADT: insertion public void add(Comparable data) { if (full()) // expand array ensureCapacity(2*size); size++; a[size] = data; if (size > 1) heapifyUp(); }

implementation in an ADT: heapifyUp private void heapifyUp() { Comparable temp; int next = size; while (next != 1 && a[next].compareTo(a[next/2]) > 0) temp = a[next]; a[next] = a[next/2]; a[next/2] = temp; next = next/2; }

implementation in an ADT: deletion public Comparable removeMax() { if (size == 0) throw new IllegalStateException(“empty heap”); Comparable max = a[1]; a[1] = a[size]; size--; if (size > 1) heapifyDown(1); return max; }

implementation in an ADT: heapifyDown private void heapifyDown(int root) { Comparable temp; int next = root; while (next*2 <= size) // node has a child int child = 2*next; // left child if (child < size && a[child].compareTo(a[child+1]) < 0) child++; // right child instead if (a[next].compareTo(a[child]) < 0) temp = a[next]; a[next] = a[child]; a[child] = temp; next = child; } else; next = size; // stop loop

Merging Two Heaps We are given two heaps and a key k 7 5 2 8 4 6 We are given two heaps and a key k We create a new heap with the root node storing k and with the two heaps as subtrees We perform heapifyDown to restore the heap-order property 3 7 5 2 8 4 6 8 7 5 2 6 4 3

Example We have 15 element with keys: 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 First we construct (n+1)/2 element as 24 25 36 28 34 33 17 10 Add one more key for each pairs and do the merge process: 15 25 24 35 28 36 29 33 34 13 10 17

Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 15 25 24 35 28 36 29 33 34 13 10 17 heapifyDown process: 25 15 24 36 28 35 34 33 29 17 10 13

Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 Add one more key for each two subtrees and do the merge process: 33 32 25 36 34 17 24 15 35 28 29 31 13 10 heapifyDown process: 36 25 15 24 35 28 33 34 32 31 29 17 10 13

Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 Merge process: 30 36 34 25 35 32 17 24 15 33 28 29 31 13 10 heapifyDown process: 36 35 25 15 24 33 28 30 34 32 31 29 17 10 13

Bottom-up Heap Construction We can construct a heap storing n given keys in using a bottom-up construction with log n phases In phase i, pairs of heaps with 2i -1 keys are merged into heaps with 2i+1-1 keys 2i -1 2i+1-1