Welcome to CS50 section! This is Week 5. Please open your CS50 IDE - - PowerPoint PPT Presentation

welcome to cs50 section this is week 5
SMART_READER_LITE
LIVE PREVIEW

Welcome to CS50 section! This is Week 5. Please open your CS50 IDE - - PowerPoint PPT Presentation

Welcome to CS50 section! This is Week 5. Please open your CS50 IDE and run this in your console: cd ~/workspace/cs50-section git reset --hard git pull If new to this section, visiting, or want to start over , run this in your


slide-1
SLIDE 1

Welcome to CS50 section! This is Week 5.

Please open your CS50 IDE and run this in your console: cd ~/workspace/cs50-section ↵ git reset --hard ↵ git pull

If new to this section, visiting, or want to “start over”, run this in your console: rm -r -f ~/workspace/cs50-section/ ↵ cd ~/workspace ↵ git clone https://github.com/bw/cs50-section.git

The next pset is pset 5, after which you will have a take-home midterm. Afterwards, one week off to take a break (or participate in the coding contest!)

slide-2
SLIDE 2

Know before attempting pset 5:

  • Structs

○ Defining structs ○ Static v dynamic creation ○ Accessing fields

  • Linked lists
  • Hash tables

○ Tries

  • Stacks and queues
slide-3
SLIDE 3

Structs (aka structures)

  • Encapsulate data of different types together
  • Think “object oriented programming”

typedef struct { char name[40]; char github[20]; int year; } student;

slide-4
SLIDE 4

Structs (aka structures)

  • What’s the datatype of this struct?

typedef struct { char name[40]; char github[20]; int year; } student;

slide-5
SLIDE 5

Structs (aka structures)

  • What’s the datatype of this struct?

struct student typedef struct { char name[40]; char github[20]; int year; } student;

slide-6
SLIDE 6

Structs (aka structures)

  • Created within the global scope
  • To create a variable of type “struct student”:

struct student brandon;

  • To assign fields using the dot operator:

strcpy(brandon.name, “Brandon Wang”); strcpy(brandon.github, “bw”); brandon.year = 2019;

slide-7
SLIDE 7

Structs (aka structures)

Before moving forward, be comfortable with--

  • Conceptual understanding of a struct

○ Good use cases for structs?

  • How and where to define a struct
  • How to add/modify fields of a struct
  • How to modify string fields of a struct
slide-8
SLIDE 8

Linked lists

Before pset 5, review--

  • Creating a linked list
  • Inserting into a linked list

○ At the head ○ At the tail ○ In the middle

  • Deleting from a linked list
  • Deleting an entire linked list
  • Iterating over a linked list
slide-9
SLIDE 9

Linked lists

Conceptually, what’s a linked list?

slide-10
SLIDE 10

Linked lists

Conceptually, what’s a linked list? Programmatically, what’s a linked list?

slide-11
SLIDE 11

Linked lists

Conceptually, what’s a linked list? Programmatically, what’s a linked list?

typedef struct node { // just some form of data; could be a char* or whatever int i; // pointer to next node; have to include `struct` since this is a recursive definition struct node *next; } node;

slide-12
SLIDE 12

Linked lists

Conceptually, what’s a linked list? Programmatically, what’s a linked list?

typedef struct node { // just some form of data; could be a char* or whatever int i; // pointer to next node; have to include `struct` since this is a recursive definition struct node *next; } node;

slide-13
SLIDE 13

Tricky things about linked lists

  • How do we iterate over a linked list?
  • How do we insert/delete a node?

○ Be careful of node orphaning

slide-14
SLIDE 14

Tackling pset 5

Overarching decision you must make: What data structure do I use?

  • Hash table
  • Trie

“you should not encourage any student to pursue any implementation”

slide-15
SLIDE 15

Hash tables

  • Associative array
  • Position of each element determined by a “hash function”

Hash functions:

  • Take an input and generate a reproducible output
  • Ideally: constant time output and few collisions
slide-16
SLIDE 16

Hash tables

Best of both worlds approach: Combines “random access ability” of an array with the “dynamism” of a linked list Assuming we define our hash table well:

  • Insertions tend towards O(1)
  • Deletions tend towards O(1)
  • Lookups tend towards O(1)
slide-17
SLIDE 17

Hash tables

What defines a good hash function? Ideally--

  • Be deterministic
  • Use only the data being hashed
  • Use all of the data being hashed
  • Uniformly distribute data

For some hash function applications:

  • Generate very different results for very similar (but different) data
slide-18
SLIDE 18

Example hash function

unsigned int hash(char* str) { intsum = 0; for (intj = 0; str[j] != ‘\0’; j++) { sum += str[j]; } return sum % HASH_MAX; }

slide-19
SLIDE 19

Hash tables → Collisions

Collisions occur when two pieces of data yield the same code. If storing data, we want both pieces of data. So we need to get both elements in the hash table.

slide-20
SLIDE 20

Hash tables → Collisions

  • Handling collisions

○ Linear probing

■ Clustering

○ Chaining via linked lists

slide-21
SLIDE 21

Tries

slide-22
SLIDE 22

Tries

Tries: roadmaps for hash tables.

  • If you can follow the map from beginning to end, the data exists.
  • If you can’t, it doesn’t exist.
  • No collisions possible/allowed
slide-23
SLIDE 23

Tries

See CS50-standard slides for example scenario.

slide-24
SLIDE 24

Stacks and queues

We will talk about these conceptually today. Code snippets available after section (brandon.wang/cs50)

slide-25
SLIDE 25

Stacks and queues

We will talk about these conceptually today. Code snippets available after section (brandon.wang/cs50) What do stacks and queues do?

  • Maintains data in an organized way
  • Optimized for a specific type of data access
  • Really bad for other forms of data access
  • Usually we implement as an array or linked list
slide-26
SLIDE 26

Stacks

Stacks are last in, first out (LIFO). Allowed operations:

  • Push

○ Add an element to the top of the stack

  • Pop

○ Grab the most recently added element from the top.

slide-27
SLIDE 27

Stacks

typedef struct stack { VALUE array[CAPACITY]; int top; } stack;

slide-28
SLIDE 28

Stacks

typedef struct stack { VALUE array[CAPACITY]; int top; } stack; How would we push/pop elements?

slide-29
SLIDE 29

Queues

Queues are first in, first out (FIFO). Allowed operations:

  • Enqueue

○ Add an element to the end of the queue

  • Dequeue

○ Remove the (oldest) element from the front of the queue

slide-30
SLIDE 30

Queues

Best implemented as a linked list. typedef struct queue { VALUE val; struct queue *prev; struct queue *next; } queue; Maintain pointers to head AND tail of the list.

slide-31
SLIDE 31

Data structures summary

slide-32
SLIDE 32

Data structures

Four primary ways we’ve looked at in CS50:

  • Arrays
  • Linked lists
  • Hash tables
  • Tries
slide-33
SLIDE 33

Arrays

  • Insertion
  • Deletion
  • Lookup
  • Ease of sorting
  • Size
slide-34
SLIDE 34

Linked lists

  • Insertion
  • Deletion
  • Lookup
  • Ease of sorting
  • Size
slide-35
SLIDE 35

Hash tables

  • Insertion
  • Deletion
  • Lookup
  • Ease of sorting
  • Size
slide-36
SLIDE 36

Tries

  • Insertion
  • Deletion
  • Lookup
  • Ease of sorting
  • Size
slide-37
SLIDE 37

That’s all for today!