Podcast Ch22b Title: Inserting into a Heap

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

CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Binary Search Visualization i j.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 22 Heaps.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Trees Michael R. Wick
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
ADSA: Heaps/ Advanced Data Structures and Algorithms Objectives – –implement heaps (a kind of array-based complete binary tree), heap sort,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Foundations of Data Structures Practical Session #8 Heaps.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Podcast Ch24c Title: Breadth First Search
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
Binary Search Trees < > =
Trees Chapter 11 (continued)
Podcast Ch26a Title: Representing Graphs
Trees Chapter 11 (continued)
Podcast Ch17d Title: Drawing a Binary Tree
Binary Search Trees < > = Binary Search Trees
Podcast Ch17a Title: Expression Trees
QuickSort QuickSort is often called Partition Sort.
Priority Queues Linked-list Insert Æ Æ head head
ADT Heap data structure
Data Structures: Segment Trees, Fenwick Trees
Chapter 8 – Binary Search Tree
Priority Queue & Heap CSCI 3110 Nan Chen.
Binary Search Trees < > =
Heapsort Heap & Priority Queue.
Binary Search Trees < > = Binary Search Trees
CMSC 341 Lecture 14 Priority Queues & Heaps
Heapsort and d-Heap Neil Tang 02/11/2010
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
Podcast Ch24b Title: Graphs and Digraphs
2-3-4 Trees Red-Black Trees
Podcast Ch22c Title: Deleting from a Heap
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Data Structures Lecture 29 Sohail Aslam.
Algorithms: Design and Analysis
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
Priority Queues CSE 373 Data Structures.
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Priority Queues & Heaps
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Heapsort and d-Heap Neil Tang 02/14/2008
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Algorithms CSCI 235, Spring 2019 Lecture 26 Midterm 2 Review
Podcast Ch23a Title: Bit Arrays
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Podcast Ch22b Title: Inserting into a Heap Description: Overview; inserting into a heap; pushHeap method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Heaps A maximum heap is an array‑based tree in which the value of a parent is ≥ the value of its children. A minimum heap uses the relation ≤.

For each tree, indicate whether it is a heap (maximum or minimum) or is not a heap. (a) Maximum heap Minimum heap No heap (b) Maximum heap Minimum heap No heap (c) Maximum heap Minimum heap No heap

Inserting into a Heap Assume that the array the elements in the index range 0  i < last < n form a heap. The new element will enter the array at index last with the heap expanding by one element.

Inserting into a Heap (cont) Insert item into a heap by moving nodes on the path of parents down one level until the item is assigned as a parent that has heap ordering. Path of parents for insertion of item = 50

Inserting into a Heap (cont) The static method pushHeap() of the class Heaps inserts a new value in the heap. The parameter list includes the array arr, the index last, the new value item, and a Comparator object of type Greater or Less indicating whether the heap is a maximum or minimum heap.

Inserting into a Heap (continued) The algorithm uses an iterative scan with variable currPos initially set to last. At each step, compare the value item with the value of the parent and if item is larger, copy the parent value to the element at index currPos and assign the parent index as the new value for currPos. The effect is to move the parent down one level. Stop when the parent is larger and assign item to the position currPos.

Inserting into a Heap (continued)

Start with the min heap show below. Draw the heap after inserting 25.

pushHeap() // the array elements in the range // (0, last) are a heap; insert item // into the heap so that the range // (0, last+1) is a heap; use the Comparator // comp to perform comparisons public static <T> void pushHeap(T[] arr, int last, T item, Comparator<? super T> comp) { // assume the new item is at location // arr[last] and that the elements arr[0] // to arr[last-1] are in heap order int currPos, parentPos; // currPos is an index that traverses // path of parents; item is assigned // in the path currPos = last; parentPos = (currPos-1)/2;

pushHeap() (concluded) // traverse path of parents up to the root while (currPos != 0) { // compare target and parent value if (comp.compare(item,arr[parentPos]) < 0) { // move data from parent position to // current position; update current position // to parent position; compute next parent arr[currPos] = arr[parentPos]; currPos = parentPos; parentPos = (currPos-1)/2; } else // heap condition is ok; break break; // the correct location has been discovered; // assign target arr[currPos] = item;

For each of the following array-based trees, identify all of the properties that are valid. 1. maximum heap 2. minimum heap 3. sorted tree (array used to build the tree is sorted) 4. tree in which every level has all possible nodes binary search tree Tree (a) has properties: _________________________ Tree (b) has properties: _________________________ Tree (c) has properties: _________________________ Tree (d) has properties: _________________________ Tree (e) has properties: _________________________