Lecture 16: Midterm recap + Heaps ii

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
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)
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
CSE373: Data Structures & Algorithms
Data Structures and Algorithms for Information Processing
Priority Queues and Heaps
October 30th – Priority QUeues
Source: Muangsin / Weiss
Part-D1 Priority Queues
March 31 – Priority Queues and the heap
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Cse 373 April 26th – Exam Review.
Heaps.
BST Review Jika terdapat urutan bilangan di atas, dimulai dari 10 dan diakhiri dengan 10. Buatkan BST nya Tampilkan secara inorder Preorder.
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
CSCI2100 Data Structures Tutorial 7
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Data Structures and Algorithms
Binary Heaps Text Binary Heap Building a Binary Heap
Part-D1 Priority Queues
Data Structures and Algorithms
Priority Queues.
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
Priority Queue and Binary Heap Neil Tang 02/12/2008
B-Tree Insertions, Intro to Heaps
Priority Queues.
ITEC 2620M Introduction to Data Structures
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
CSCE 3110 Data Structures and Algorithm Analysis
Hassan Khosravi / Geoffrey Tien
Implementing Hash and AVL
"Teachers open the door, but you must enter by yourself. "
Heap Sort CSE 2011 Winter January 2019.
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Lecture 9: Self Balancing Trees
CSCE 3110 Data Structures and Algorithm Analysis
CSE 373 Data Structures and Algorithms
Lecture 4: Introduction to Code Analysis
Priority Queues & Heaps
Lecture 9: Intro to Trees
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Heaps & Multi-way Search Trees
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Priority Queues Binary Heaps
Priority Queues (Heaps)
Presentation transcript:

Lecture 16: Midterm recap + Heaps ii CSE 373 Data Structures and Algorithms CSE 373 19 SP - Kasey Champion

Practice: Building a minHeap 3 Minutes Practice: Building a minHeap Construct a Min Binary Heap by inserting the following values in this order: 5, 10, 15, 20, 7, 2 Min Priority Queue ADT removeMin() – returns the element with the smallest priority, removes it from the collection state behavior Set of comparable values - Ordered based on “priority” peekMin() – find, but do not remove the element with the smallest priority insert(value) – add a new element to the collection Min Binary Heap Invariants Binary Tree – each node has at most 2 children Min Heap – each node’s children are larger than itself Level Complete - new nodes are added from left to right completely filling each level before creating a new one 5 2 10 7 15 5 2 percolateUp! 20 7 10 2 15 percolateUp! percolateUp! CSE 373 SP 18 - Kasey Champion

Administrivia HW 4 due Wednesday night HW 5 out Wednesday (partner project) Partner form due tonight How to get get a tech job with Kim Nguyen Today 4-5pm PAA CSE 373 19 sp - Kasey Champion

Midterm Grades Course Grade Breakdown Midterm: 20% Final Exam: 25% Individual Assignments: 15% Partner Projects: 40% Hashing Trees Tree Method Big O Code Modeling Design Decisions CSE 373 19 SP - Kasey Champion

Midterm Performance CSE 373 SP 18 - Kasey Champion

Implementing Heaps How do we find the minimum node? How do we find the last node? How do we find the next open space? How do we find a node’s left child? How do we find a node’s right child? How do we find a node’s parent? 𝑝𝑒𝑒𝑘𝑀𝑖𝑛()=𝑎𝑟𝑟[0] I A B D H C K E J F L G 𝑙𝑎𝑠𝑡𝑁𝑜𝑑𝑒()=𝑎𝑟𝑟[𝑠𝑖𝑧𝑒 −1] 𝑜𝑝𝑒𝑛𝑆𝑝𝑎𝑐𝑒()=𝑎𝑟𝑟[𝑠𝑖𝑧𝑒] 𝑙𝑒𝑓𝑡𝐶ℎ𝑖𝑙𝑑 𝑖 =2𝑖+1 Gives us fast insertions and removals Completeness forces height to be log(n) Gives us contiguious memory 𝑟𝑖𝑔ℎ𝑡𝐶ℎ𝑖𝑙𝑑 𝑖 =2𝑖+2 Fill array in level-order from left to right 𝑝𝑎𝑟𝑒𝑛𝑡 𝑖 = 𝑖−1 2 1 2 3 4 5 6 7 8 9 10 11 12 13 A B C D E F G H I J K L CSE 373 19 SP - Kasey Champion

Heap Implementation Runtimes char peekMin() timeToFindMin Tree Array char removeMin() findLastNodeTime + removeRootTime + numSwaps * swapTime void insert(char) findNextSpace + addValue + numSwaps * swapTime A Θ(1) Θ(1) B C n + 1 + log(n) * 1 Θ(n) D E F 1 + 1 + log(n) * 1 Θ(log(n)) 1 2 3 4 5 6 7 A B C D E F n + 1 + log(n) * 1 Θ(n) 1 + 1 + log(n) * 1 Θ(log(n)) CSE 373 SP 18 - Kasey Champion

Building a Heap Insert has a runtime of Θ(log(n)) If we want to insert a n items… Building a tree takes O(nlog(n)) Add a node, fix the heap, add a node, fix the heap Can we do better? Add all nodes, fix heap all at once! CSE 373 SP 18 - Kasey Champion

Cleaver building a heap – Floyd’s Method Facts of binary trees Increasing the height by one level doubles the number of possible nodes A complete binary tree has half of its nodes in the leaves A new piece of data is much more likely to have to percolate down to the bottom than be the smallest element in heap 1. Dump all the new values into the bottom of the tree Back of the array 2. Traverse the tree from bottom to top Reverse order in the array 3. Percolate Down each level moving towards overall root see lecture 16 slides for example / animations CSE 373 SP 18 - Kasey Champion