CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Project 3, Standard Template Library (STL) Bryce Boe 2013/11/25 CS24, Fall 2013.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
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.
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.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
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.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Important Problem Types and Fundamental Data Structures
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
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 What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
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 QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
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.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
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.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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,
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Data Structure By Amee Trivedi.
Programming Abstractions
Priority Queues and Heaps
Programming Abstractions
Programming Abstractions
Hashing Exercises.
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.
Programming Abstractions
Priority Queues & Heaps
CSE 12 – Basic Data Structures
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: Heaps! 1. Linked list coding interview questions 2. Priority queues  Binary trees  Binary heaps 2

Coding interview question

Suppose we have a pointer (current) to the node containing the item to be removed What additional information do we need to successfully remove the node? A) Nothing additional. B) A pointer to the node immediately prior to the to-be- deleted node. C) A pointer to the node immediately after the to-be-deleted node. D) Both B and C. NULL head current (we want to remove this one) Remove

Suppose we have a pointer (current) to the node containing the item to be removed. NULL head current (we want to remove this one) NINJA CODER EDITION

Other classic interview questions  How can you remove a node of a linked list given a pointer to the node itself (and not the previous one)?  Write code to reverse a linked list in place.  How can you detect if there is a loop in a linked list (i.e. list is improperly formed), using constant space (O(1) amount of space)?

Priority Queue Emergency Department waiting room operates as a priority queue: patients are sorted according to seriousness, NOT how long they have waited.

Some priority queue implementation options  Unsorted linked list  Insert new element in front  Remove by searching list for highest- priority item  Sorted linked list  Always insert new elements where they go in priority-sorted order  Remove from front

Unsorted linked list  Add is FAST  Just throw it in the list at the front  O(1)  Remove/peek is SLOW  Hard to find item the highest priority item— could be anywhere  O(N) Priority queue implementations This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Keyah Cheatum CommonsAttribution-Share Alike 3.0 Unported Keyah Cheatumhttp://commons.wikimedia.org/wiki/File:Messy_Room.JPG

Sorted linked list  Add is SLOW  Need to step through the list to find where item goes in priority- sorted order  O(N)  Remove/peek is FAST  Easy to find item you are looking for (first in list)  O(1) Priority queue implementations Released to the public domain:

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 +

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? A. 0-3 B. 4 C. 5 D. 6 E. 7-8

A node object for binary trees  Similar to a linked list node, it contains a pointer to data, and a pointer to the next elements  Whereas a linked list node has just one next pointer, a binary node tree has two child pointers, left and right

A node object for binary trees  Similar to a linked list node, it contains a pointer to data, and a pointer to the next elements  Whereas a linked list node has just one next pointer, a binary node tree has two child pointers, left and right

Heaps! IMPORTANT NOTE: Despite the shared name, Heaps have nothing to do with the Heap part of memory (Binary Heap = a partially-ordered tree data structure; Heap is in contrast to Stack as a part of memory)

Binary Heaps*  Binary heaps are one kind of binary tree  They have a few special restrictions, in addition to the usual binary tree ADT:  Must be complete  Ordering of data must obey heap property  Min-heap version: a parent’s data is always ≤ its children’s data  Max-heap version: a parent’s data is always ≥ its children’s data * There are other kinds of heaps as well. For example, binomial heap is 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? A. 0 B. 1 C. 2 D. 3

In how many places could the smallest number in this min -binary-heap be located ? A. 0-2 B. 3-4 C. 5-6 D. 7-8

Binary heap in an array

 We actually do NOT typically use a node object to implement heaps  Because they must be complete, they fit nicely into an array, so we usually do that

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: A. i – 2 B. i / 2 C. (i – 1)/2 D. 2i

Binary heap in an array  For tree of height h, array length is 2 h -1  For a node in array index i:  Left child is at array index: A. i +1 B. i + 2 C. 2i D. 2i + 1

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

Binary heap insert and delete

Binary heap insert + “bubble up”

Binary heap insert

Binary heap delete + “trickle-down”

TRUE OR FALSE  There is only one configuration of a valid min-heap containing the elements {34, 22, 3, 9, 18} A. TRUE B. FALSE

Heap outcomes by insert order Create a MIN heap by inserting the elements, one by one, in the order given below for the second letter of your first name:  A-F: {3, 9, 18, 22, 34}  G-L: {3, 22, 18, 9, 34}  M-R: {9, 22, 18, 3, 34}  S-Z: {18, 22, 3, 9, 34}

Heap outcomes by insert order Create a MIN heap by inserting the elements, one by one, in the order given below for the first letter of your last name:  A-F: {18, 9, 34, 3, 22}  G-L: {3, 18, 22, 9, 34}  M-R: {22, 9, 34, 3, 18}  S-Z: {34, 3, 9, 18, 22}

How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34}? A. 1-2 B. 3-4 C. 5-8 D. 5! E. Other/none/more

Time cost  What is the worst-case time cost for each heap operation: Add, Remove, Peek? A. O(n), O(1), O(1) B. O(logn), O(logn), O(1) C. O(n), O(logn), O(logn) D. Other/none/more