Docsity

Understanding Binary Heaps: Min-Heap and Max-Heap, Summaries of Design

Partial preview of the text

Download Understanding Binary Heaps: Min-Heap and Max-Heap and more Summaries Design in PDF only on Docsity!

Priority Queues and Heaps

What pointer (aka piece of advice) would you give a

prospective CS106B student about how to be

successful in the class?

(put your answers in the chat)

vectors + grids

stacks + queues

sets + maps

Object-Oriented Programming

arrays

dynamic memory management

linked data structures

algorithmic testing analysis

recursive problem-solving

Roadmap

Life after CS106B!

C++ basics

Diagnostic

real-world algorithms

Core Tools

User/client

Implementation

Today’s

question

How can we make use of

multiple levels of

abstraction in our data

storage techniques to build

better ADTs?

Today’s

topics

1. Review (OurVector)

2. Priority Queues

3. Binary Heaps

What is OurVector?

● Goal: Implement own version of the Stanford C++ Vector

● Scope Constraints:

○ We will only implement a subset of the functionality that the Stanford Vector provides. ○ OurVector can only store integers and is not be configurable to store other types ○ When we left off on Tuesday, OurVector was limited to storing a fixed number of elements. For now, if we run out space we just throw an error.

OurVector Header File

class OurVector { public: OurVector(); ~OurVector(); void add(int value); void insert(int index, int value); int get(int index); void remove(int index); int size(); bool isEmpty(); private: int* elements; int allocatedCapacity; int numItems; };

Review: Constructors and Destructors

● The constructor is the special method that gets called when a new instance of a class is declared. In this method, we initialize all of our member variables to the appropriate values, including allocating any necessary memory. ○ The constructor for a class named ClassName has signature ClassName(args); ● The destructor is the special method that gets called when an instance of a class goes out of scope and thus is destroyed. In this method, we most often are responsible for freeing any dynamically allocated memory used by the instance. ○ The destructor for a class named ClassName has signature ~ClassName();

Review: OurVector internal state

0 1 2 3 4 5 6 7

elements

allocated Capacity

numItems

0x1234abef

8

4

// client code OurVector vec; vec.add(106); vec.add(42); vec.add(-3); vec.add(27); vec.remove(1); vec.insert(0, 198);

Dynamic Array Growth

A Day in the Life of a Hermit Crab

● Hermit crabs are interesting animals. They live in scavenged shells that they find on the seafloor. Once in a shell, this is their lifestyle (with a bit of poetic license): ○ Grow until they have outgrown their current shell. Then, follow these 5 steps. ■ Find another, larger shell. ■ Move all their stuff into the new shell. ■ Leave the old shell on the seafloor. ■ Update their address with the Hermit Crab Postal Service. ■ Make note of their new shell's spacious capacity by posting on Hermit Crab Instagram. ● While this is purposefully a bit of a silly analogy, this process models almost exactly what we need to do in order to dynamically resize our internal data storage mechanism.

0 1 2 3

elements

allocated Capacity

numItems

0x1234abef

4

4

0 1 2 3

elements

allocated Capacity

numItems

0x1234abef

4

4

  1. Create a new, larger array. Usually we choose to double the current size.

0 1 2 3

elements

allocated Capacity

numItems

0x1234abef

4

4

  1. Create a new, larger array. Usually we choose to double the current size.
  2. Copy the old array elements to the new array.

0 1 2 3 4 5 6 7

0 1 2 3

elements

allocated Capacity

numItems

0x1234abef

4

4

  1. Create a new, larger array. Usually we choose to double the current size.
  2. Copy the old array elements to the new array.

0 1 2 3 4 5 6 7