Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Unordered Data Structures: Sets and Maps, Summaries of Data Structures and Algorithms

A way to bundle different types of information in C++ – like creating a custom data structure. Definition. Page 10. The GridLocation struct. ○ A pre-defined ...

Typology: Summaries

2022/2023

Uploaded on 05/11/2023

anandit
anandit 🇺🇸

4.8

(19)

255 documents

1 / 63

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unordered Data Structures:
Sets and Maps
What’s an example of “unordered data” that you’ve
encountered in your life?
(put your answers the chat)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f

Partial preview of the text

Download Unordered Data Structures: Sets and Maps and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Unordered Data Structures:

Sets and Maps

What’s an example of “unordered data” that you’ve

encountered in your life?

(put your answers 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

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

Implementation

vectors + grids

stacks + queues

sets + maps

User/client

Today’s

question

When is it appropriate to

use different types of

unordered data structures?

Review

(grids and queues and stacks, oh my!)

What is a grid?

● A 2D array, defined with a particular width and height

● Useful for spreadsheets, game boards, etc.

● Three ways to declare a grid ○ Grid gridName; ○ Grid gridName(numRows, numCols); ○ Grid gridName = {{r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2},...};

● We could use a combination of Vectors to simulate a 2D matrix, but a Grid is easier!

a0 a1 a

b0 b1 b

c0 c1 c

The GridLocation struct

● A pre-defined struct in the Stanford C++ libraries that makes it more convenient to store Grid locations

● To declare a struct, you can either assign each of its members separately or assign it when it’s created:

GridLocation origin = {0, 0}; GridLocation origin; origin.row = 0; origin.col = 0;

struct GridLocation { int row; int col; }

What is a queue?

● Like a real queue/line!

F irst person I n is the F irst person O ut (FIFO) ○ When you remove (dequeue) people from the queue, you remove them from the front of the line.

● Last person in is the last person served ○ When you insert (enqueue) people into a queue, you insert them at the back (the end of the line).

Ordered ADTs with accessible
indices

Types:

● Vectors (1D) ● Grids (2D)

Traits:

● Easily able to search through all elements ● Can use the indices as a way of structuring the data

Ordered ADTs where you can’t
access elements by index

Types:

● Queues (FIFO) ● Stacks (LIFO)

Traits:

● Constrains the way you can insert and access data ● More efficient for solving specific LIFO/FIFO problems

Activity:

towersOfHanoi()

Towers of Hanoi

● Rules: ○ Can only move one disk at a time ○ You cannot place a larger disk on top of a smaller disk

Discuss : How would

you solve this problem?

[breakout rooms]

Pseudocode

(1) Move disk 1 to destination (2) Move disk 2 to auxiliary (3) Move disk 1 to auxiliary (4) Move disk 3 to destination

(5) Move disk 1 to source (6) Move disk 2 to destination (7) Move disk 1 to destination

Code the solution for

three disks!

[breakout rooms]