CS106X – Programming Abstractions in C++

36
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 http://peerinstruction4cs.or g .

description

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  http://peerinstruction4cs.org. - PowerPoint PPT Presentation

Transcript of CS106X – Programming Abstractions in C++

Page 1: CS106X –  Programming Abstractions in C++

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 

http://peerinstruction4cs.org.

Page 2: CS106X –  Programming Abstractions in C++

2

Today’s Topics: Heaps!1. Linked list coding interview questions2. Priority queues

Binary trees Binary heaps

Page 3: CS106X –  Programming Abstractions in C++

Coding interview question

Page 4: CS106X –  Programming Abstractions in C++

Suppose we have a pointer (current) to the node containing the item to be removed.

30 22 5 42

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

Page 5: CS106X –  Programming Abstractions in C++

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

30 22 5 42

Page 6: CS106X –  Programming Abstractions in C++

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)?

Page 7: CS106X –  Programming Abstractions in C++

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

Page 8: CS106X –  Programming Abstractions in C++

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

Page 9: CS106X –  Programming Abstractions in C++

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 http://commons.wikimedia.org/wiki/File:Messy_Room.JPG

Page 10: CS106X –  Programming Abstractions in C++

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: http://commons.wikimedia.org/wiki/File:Wall_Closet.jpg

Page 11: CS106X –  Programming Abstractions in C++

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

+

Page 12: CS106X –  Programming Abstractions in C++

Binary trees

Page 13: CS106X –  Programming Abstractions in C++

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!)

Page 14: CS106X –  Programming Abstractions in C++

How many of these are valid binary trees?

A. 0-3B. 4C. 5

D. 6E. 7-8

Page 15: CS106X –  Programming Abstractions in C++

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

data:

left:

right:

Page 16: CS106X –  Programming Abstractions in C++

A node object for binary trees

data:

left:

right:

data:

left:

right:

data:

left:

right:

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

Page 17: CS106X –  Programming Abstractions in C++

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)

Page 18: CS106X –  Programming Abstractions in C++

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.

Page 19: CS106X –  Programming Abstractions in C++

How many of these could be valid binary heaps?

A. 0-1B. 2C. 3

D. 4E. 5-8

Page 20: CS106X –  Programming Abstractions in C++

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

A. 0B. 1C. 2D. 3

Page 21: CS106X –  Programming Abstractions in C++

In how many places could the smallest number in this min-binary-heap be located ?

A. 0-2B. 3-4C. 5-6D. 7-8

Page 22: CS106X –  Programming Abstractions in C++

Binary heap in an array

Page 23: CS106X –  Programming Abstractions in C++

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

data:

left:

right:

Page 24: CS106X –  Programming Abstractions in C++

Heap in an array

For tree of height h, array length is 2h-1 For a node in array index i:

Parent is at array index: A. i – 2B. i / 2C. (i – 1)/2D. 2i

Page 25: CS106X –  Programming Abstractions in C++

Binary heap in an array

For tree of height h, array length is 2h-1 For a node in array index i:

Left child is at array index:A. i +1B. i + 2C. 2i D. 2i + 1

Page 26: CS106X –  Programming Abstractions in C++

Binary heap in an array

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

Page 27: CS106X –  Programming Abstractions in C++

Binary heap insert and delete

Page 28: CS106X –  Programming Abstractions in C++

Binary heap insert + “bubble up”

Page 29: CS106X –  Programming Abstractions in C++

Binary heap insert

Page 30: CS106X –  Programming Abstractions in C++

Binary heap delete + “trickle-down”

Page 31: CS106X –  Programming Abstractions in C++

Binary heap delete + “trickle-down”

Page 32: CS106X –  Programming Abstractions in C++

TRUE OR FALSE There is only one configuration of a valid

min-heap containing the elements {34, 22, 3, 9, 18}

A. TRUEB. FALSE

Page 33: CS106X –  Programming Abstractions in C++

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}

Page 34: CS106X –  Programming Abstractions in C++

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}

Page 35: CS106X –  Programming Abstractions in C++

How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34}?

A. 1-2B. 3-4C. 5-8D. 5!E. Other/none/more

Page 36: CS106X –  Programming Abstractions in C++

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