Programming Abstractions

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
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.
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.
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
Partially Ordered Data ,Heap,Binary Heap
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
CSE373: Data Structures & Algorithms
Data Structure By Amee Trivedi.
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority.
Chapter 12 – Data Structures
Programming Abstractions
Heaps (8.3) CSE 2011 Winter May 2018.
Chapter 15 Lists Objectives
Programming Abstractions
Priority Queues and Heaps
Programming Abstractions
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Heaps.
Tree 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.
structures and their relationships." - Linus Torvalds
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
structures and their relationships." - Linus Torvalds
Tree data structure.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
i206: Lecture 14: Heaps, Graphs intro.
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
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.
Hassan Khosravi / Geoffrey Tien
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
Priority Queues.
CS 367 – Introduction to Data Structures
Heaps By JJ Shepherd.
Important Problem Types and Fundamental Data Structures
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Chapter 9 The Priority Queue ADT
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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.
structures and their relationships." - Linus Torvalds
Heaps Chapter 6 Section 6.9.
Heaps.
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: Priority Queue Linked List implementation Sorted Unsorted Heap data structure implementation TODAY’S TOPICS NOT ON THE MIDTERM

Some priority queue implementation options data next 75 data next 8 data next 20 NULL head 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) data next 8 data next 20 data next 75 NULL head

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

Binary Heaps

Heap: not to be confused with the Heap! 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 Stack Stack ADT = Heap data structure Heap ≠ Source: http://www.flickr.com/photos/35237093334@N01/409465578/ Author: http://www.flickr.com/people/35237093334@N01 Peter Kazanjy] 0x0

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!) How many of these are valid binary trees?

A node struct for binary trees data: left: right: Similar to a linked list node, it contains 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; }; data: left: right: data: left: 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? 0-1 4 2 5-8 3

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

Binary heap in an array

Binary heap in an array priority: data: left: right: Binary heap is one special kind of binary tree, so we could use a node struct to represent it However, … 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 0-based 1-based OR

Heap in an array For a node in array index i: Q: The parent of that node is found where? A: at index: i – 2 i / 2 (i – 1)/2 2i

Fact summary: Binary heap in an array 0-based: 1-based: For tree of height h, array length is 2h-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 2h 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 + 1

Binary heap enqueue and dequeue

Binary heap enqueue (insert + “bubble up”) Size=8, Capacity=15 1 2 3 4 5 6 7 8 9 … 14 10 18 11 21 27 ? Size=9, Capacity=15 1 2 3 4 5 6 7 8 9 … 14 10 11 21 27 18 ?

[Binary heap insert reference page]

Binary heap dequeue (delete + “trickle down”) Size=9, Capacity=15 1 2 3 4 5 6 7 8 9 … 14 10 11 21 27 18 ? ?

Binary heap dequeue (delete + “trickle down”) Size=9, Capacity=15 1 2 3 4 5 6 7 8 9 … 14 10 11 21 27 18 ? Size=8, Capacity=15 1 2 3 4 5 6 7 8 9 … 14 10 27 11 21 18 ?

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

Summary analysis Comparing our priority queue options

Some priority queue implementation options data next 75 data next 8 data next 20 NULL head 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) data next 8 data next 20 data next 75 NULL head

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

Review: priority queue implementation options data next 75 data next 8 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) Binary heap Insert + “bubble up”: O( ) Delete + “trickle down”: O( ) data next 20 NULL head data next 8 data next 20 data next 75 NULL head

Fact summary: Binary heap in an array 0-based: 1-based: For tree of height h, array length is 2h-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 2h 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 + 1