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 subtrees.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Advertisements

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)
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
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 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
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.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Balanced search trees: trees (or 2-4) trees improve the efficiency of insertItem and deleteItem methods of 2-3 trees, because they are performed.
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.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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:
Priority Queues A priority queue is an ADT where:
Heap Chapter 9 Objectives Define and implement heap structures
AA Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
CSCE 210 Data Structures and Algorithms
Binary Search Tree (BST)
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,
Priority Queues Chuan-Ming Liu
Lecture 22 Binary Search Trees Chapter 10 of textbook
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.
Priority Queues Linked-list Insert Æ Æ head head
Binary Search Trees.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Heaps and Priority Queues
Chapter 8 – Binary Search Tree
Priority Queues and Heaps
Part-D1 Priority Queues
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
Heaps and Priority Queues
Search Sorted Array: Binary Search Linked List: Linear Search
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Chapter 6: Transform and Conquer
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Data Structures Lecture 29 Sohail Aslam.
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
1 Lecture 10 CS2013.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues Binary Heaps
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.
Heaps.
Balanced search trees: trees.
Balanced search trees: 2-3 trees.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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 subtrees. Example The relational ordering between data in parent / children nodes can be different than >=, for example can be a “less than” ordering.

Height of a heap Proposition: The height of a heap H storing n keys is log(n) + 1. Justification: The number of internal nodes in a complete binary tree is at most h-1 = 2 h - 1 However, if the lowest level is not complete, the number of internal nodes can be as little as h = 2 h-1 That is, the number of internal nodes in a heap is 2 h-1 <= n <= 2 h - 1, which implies that the height, h, is log(n + 1) <= h <= log(n) + 1

Operations on heaps All heap operations are based on the following idea: 1 Make a simple structural modification of the heap (different for different operations), which may result in a violation of the heap condition. 2. Traverse the heap from the root to the bottom, or from the bottom to the root (depending on the operation) to recover the heap condition everywhere. The most important operations on heaps are: – insertItem (item) Inserts item – removeItem () Removes the root and returns the datum stored – replaceItem (item) Replaces the root with item unless this violates the heap condition – deleteItem (item) Deletes (arbitrary) item

Linear representation of a heap The easiest way to represent a complete binary tree is by means of a one- dimensional array of size 2 h+1 - 1, where h is the height of the tree. The most attractive property of this representation is that given a position of a node, j, positions of its parent and children can be easily defined (j / 2 is the position of the parent, j * 2 is the position of the left child, and j *2 + 1 is the position of the right child. Our example 1 heap can be represented as follows: k A[k]

Linear representation of a heap (contd.) The insertItem operation in this representation requires the size of the array to be increased by one, and A[size] := newNode k A[k] Now 13 must be walked upheap to fix the heap condition. Its parent is in 15/2, i.e. the 7th position; then, the parent’s parent is in 7/2 position, etc.

The upheap method Algorithm upheap (A[n], j, precedes) Input: array A[n] representing the heap, index of the child’s node j, precedes, a comparator object defining the ordering relationship Output: array A[n] with data originally in j walked up until accordingly ordered with its parent (according to the precedes function) int l := j int key := A[l] int k := l/2 while (k >= 1) and precedes(A[k], key) { A[l] := A[k] l := k k := l/2 } A[l] := key

The insertItem method Algorithm insertItem (H, Item, precedes) Input: heap H[size], item to be inserted precedes, a comparator object defining the ordering relationship Output: boolean variable success set to true if insertItem is successful if H.full() success := false else { success := true size := size + 1 H[size] := Item upheap(H[size], size, precedes) } return success The efficiency if insertItem operation is O(log n), where n is the number of tree nodes.

Linear representation of a heap (contd.) The removeItem operation decreases the size of the array by one and makes A[1] := A[size] k A[k] Now 1 must be walked downheap to fix the heap condition. Its children are in positions 1*2 and 1* Compare to the larger of the two children and if this child is greater, exchange the two. Continue until the heap condition is fixed.

The downheap method Algorithm downheap (A[n], j, precedes) Input: array A[n] representing the heap, index of the parent’s node j, precedes, a comparator object defining the ordering relationship Output: array A[n] with data originally in j walked down until accordingly ordered w.r.t both of its children (according to the precedes function) boolean foundSpot := false int l := j int key := A[l] int k := 2 * l // get the left child first while (k <= n) and (! foundSpot) { if (k < n) and (! precedes (A[k+1], A[k]]) k := k + 1 if (! precedes (A[k], key)) A[l] := A[k], l := k, k := 2 * l else foundSpot := true } // end while A[l] := key

The removeItem method Algorithm removeItem (H, precedes) Input: heap H[size], precedes, a comparator object defining the ordering relationship Output: boolean variable success set to true if removeItem is successful if H.empty() success := false else { success := true item := H[1] H[1] := H[size] size := size - 1 downheap(H[size], 1, precedes)} return success The efficiency of removeItem operation is O(log n), where n is the number of tree nodes.

Linear representation of a heap (contd.) The replaceItem operation does not change the size of the array but replaces the root node with a new one, and recovers the heap condition, if necessary k A[k] Now 12 must be walked downheap to fix the heap condition.

Linear representation of a heap (contd.) The deleteItem operation decreases the size of the array by one and makes A[item] := A[size]; the heap condition must be fixed after that. Assume we want to remove k A[k] Now 1 must be walked downheap to fix the heap condition.