The Heap Data Structure

Slides:



Advertisements
Similar presentations
The Stack Data Structure Mugurel Ionu Andreica Spring 2012.
Advertisements

CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
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.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Department of Computer Science University of Maryland, College Park
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Chapter 21 Binary Heap.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Lecture1 introductions and Tree Data Structures 11/12/20151.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Foundations of Data Structures Practical Session #8 Heaps.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
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.
Heaps & Priority Queues
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
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. "
Partially Ordered Data ,Heap,Binary Heap
Heap Chapter 9 Objectives Define and implement heap structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 210 Data Structures and Algorithms
BST Trees
Priority Queues and Heaps
Yuanming Yu CSCI2100B Data Structures Tutorial 7
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,
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Priority Queues Sections 6.1 to 6.5.
Priority Queues Linked-list Insert Æ Æ head head
The Binary Tree Data Structure
The Binary Search Tree Data Structure
CSCI2100 Data Structures Tutorial 7
Chapter 8 – Binary Search Tree
Binary Heaps Text Binary Heap Building a Binary Heap
Heapsort Heap & Priority Queue.
Section 10 Questions Heaps.
Search Sorted Array: Binary Search Linked List: Linear Search
Priority Queues.
CSCE 3110 Data Structures and Algorithm Analysis
"Teachers open the door, but you must enter by yourself. "
Heaps A heap is a binary tree that satisfies the following properties:
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Data Structures Lecture 29 Sohail Aslam.
CSCE 3110 Data Structures and Algorithm Analysis
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
CS 367 – Introduction to Data Structures
Priority Queues.
Heaps By JJ Shepherd.
Search Sorted Array: Binary Search Linked List: Linear Search
Computer Algorithms CISC4080 CIS, Fordham Univ.
Priority Queue and Heap
A Heap Is Efficiently Represented As An Array
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

The Heap Data Structure Mugurel Ionuț Andreica Spring 2012

Properties of the Heap Data Structure A heap has the structure of a balanced binary tree Every node of the heap has an associated numeric value The value of a node is smaller than or equal to the values associated to each of its descendants consequence: the root stores the minimum value in the heap

Operations insertElement(x) extractMin() peek() Inserts the element x into the heap Number of steps proportional to log(N) extractMin() Returns and removes the minimum element from the heap Returns an error if the heap is empty peek() Returns (but does not remove) the minimum element from the heap Only 1 step N=the (current) number of elements in the heap

How the operations work insertElement(x) Add a leaf node l on the last level, which will store the value x pushUp(l) Set node=l While (node≠root) and (node.parent.value<node.value) do Swap node.parent.value and node.value Set node=node.parent

How the operations work (cont.) extractMin() Set x=root.value Choose the rightmost leaf l on the last level Set root.value=l.value Remove l from the heap pushDown(root) Set node=root While (1) do If (node.left_son.value<=node.value) and (node.left_son.value<=node.right_son.value) then Swap node.left_son.value and node.value Set node=node.left_son Else if (node.right_son.value<=node.value) and (node.right_son.value<=node.left_son.value) then Swap node.right_son.value and node.value Set node=node.right_son Else break the while loop Return x Observations: If node.left_son does not exist then we consider node.left_son.value=+∞ If node.right_son does not exist then we consider node.right_son.value=+∞ peek() Return root.value

Implementation Details A heap can be implemented as a binary tree (every node has a value, a parent, a left son and a right son) The most common method is to use an array The elements of the heap are stored on the positions 1,...,N of the array Each position of the array => a node in the tree (position i => node i) Position 1 => the root of the tree The parent of the node i is node i/2 The left son of the node i is node 2*i (if 2*i<=n) The right son of the node i is node 2*i+1 (if 2*i+1<=n)

Array-based Heap Implementation (heap.h) #include <stdio.h> template<typename T> class Heap { public: T *H; int currentDim, maxDim; Heap(int maxDim) { this->maxDim = maxDim; H = new T[this->maxDim + 1]; currentDim = 0; } void insertElement(T x) { if (currentDim == maxDim) { fprintf(stderr, "Error!\n"); return; currentDim++; H[currentDim] = x; pushUp(currentDim); T peek() { if (currentDim == 0) { fprintf(stderr, "Error!\n"); T x; return x; } return H[1]; T extractMin() { T minValue = H[1]; H[1] = H[currentDim]; currentDim--;

Array-based Heap Implementation (cont.) void pushDown() { int l = 1; T vaux; while (1) { if (2 * l + 1 > currentDim) { if (2 * l > currentDim) break; else if (H[2 * l] < H[l]) { vaux = H[2 * l]; H[2 * l] = H[l]; H[l] = vaux; l = 2 * l; } else else { if (H[2 * l] <= H[2 * l + 1] && H[2 * l] < H[l]) { if (currentDim > 0) pushDown(); return minValue; } void pushUp(int l) { int parent; T vaux; parent = l / 2; while (l > 1 && H[parent] > H[l]) { vaux = H[parent]; H[parent] = H[l]; H[l] = vaux; l = parent;

Array-based Heap Implementation (cont.) heap.insertElement(17); heap.insertElement(6); heap.insertElement(9); heap.insertElement(7); heap.insertElement(13); printf("%d\n", heap.peek()); printf("%d\n", heap.extractMin()); } int main() { testHeap(); return 0; H[l] = vaux; l = 2 * l; } else if (H[2 * l + 1] <= H[2 * l] && H[2 * l + 1] < H[l]) { vaux = H[2 * l + 1]; H[2 * l + 1] = H[l]; l = 2 * l + 1; break; }; void testHeap() { Heap<int> heap(1000); heap.insertElement(10); heap.insertElement(100); heap.insertElement(21); heap.insertElement(4);