Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
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.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Important Problem Types and Fundamental Data Structures
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Priority Queue What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
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 Queues Dr. David Matuszek
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
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.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
CSE373: Data Structures & Algorithms Priority Queues
Programming Abstractions
October 30th – Priority QUeues
Programming Abstractions
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.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
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 Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Programming Abstractions
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Priority Queues & Heaps
CSE 12 – Basic Data Structures
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Presentation transcript:

Programming Abstractions Cynthia Lee CS106X

Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS NOT ON THE MIDTERM 2

Some priority queue implementation options Unsorted linked list  Insert new element in front: O(1)  Remove by searching list: O(N) Sorted linked list  Always insert in sorted order: O(N)  Remove from front: O(1) datanext 75 datanext 8 head datanext 20 NULL datanext 8 datanext 20 head datanext 75 NULL

We want the best of both Fast add AND fast remove/peek We will investigate trees as a way to get the best of both worlds Priority queue implementations += Fast add Fast remove/peek

Binary Heaps

 Although the Stack section of memory is a Stack like the ADT, the Heap section of memory has nothing to do with the Heap structure.  Probably just happened to reuse the same word Heap: not to be confused with the Heap! Heap Stack 0x0 Source: Author: Peter Kazanjy] = ≠ Stack ADT Heap data structure

Binary trees

A binary tree “In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".” (Thanks, Wikipedia!)

How many of these are valid binary trees? “In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".” (Thanks, Wikipedia!)

A node struct for binary trees Similar to a linked list node, it contains a pointer to data, and a pointer to the nearby elements A binary node tree has two child pointers, left and right struct TreeNode { int data; TreeNode* left; TreeNode* right; };

Heaps!

Binary Heaps* Binary heaps are one kind of binary tree They have a few special restrictions, in addition to the usual binary tree:  Must be complete › No “gaps”—nodes are filled in left-to-right on each level (row) of the tree  Ordering of data must obey heap property › Min-heap version: a parent’s data is always ≤ both its children’s data › Max-heap version: a parent’s data is always ≥ both its children’s data * There are other kinds of heaps as well. For example, binomial heap is extra credit on your assignment.

How many of these could be valid binary heaps? A.0-1 B.2 C.3 D.4 E.5-8

How many of these are valid min-binary-heaps?

Binary heap in an array

We actually do NOT typically use a node object to implement heaps Because they have the special added constraint that they must be complete, they fit nicely into an array

Two approaches: Binary heap in an array Wait, but the homework handout starts storing the elements at array index 1! › Either way is ok for the assignment. › You should understand both ways, so we’re teaching both ways (one in handout and one in lecture) OR 0-based 1-based

Heap in an array For (0-baesd) tree of height h, array length is 2 h -1 For a node in array index i:  Parent is at array index: A. i – 2 B. i / 2 C. (i – 1)/2 D. 2i

Fact summary: Binary heap in an array For tree of height h, array length is 2 h -1 For a node in array index i:  Parent is at array index: (i – 1)/2  Left child is at array index: 2i + 1  Right child is at array index: 2i + 2 For tree of height h, array length is 2 h For a node in array index i:  Parent is at array index: i /2  Left child is at array index: 2i  Right child is at array index: 2i based: 1-based:

Binary heap insert and delete

Binary heap insert + “bubble up” Size=8, Capacity= … ??…? Size=9, Capacity= … ?…?

[Binary heap insert reference page]

Binary heap delete + “trickle down” Size=8, Capacity= … ??…? Size=9, Capacity= … ?…?

[Binary heap delete + “trickle-down” reference page]