cse 12 basic data structures - stanford universitystanford.edu › class › archive › cs ›...

22
CSE 12 – Basic Data Structures Dr. Cynthia Bailey Lee

Upload: others

Post on 04-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

CSE 12 – Basic

Data StructuresDr. Cynthia Bailey Lee

Page 2: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Today’s Topics

1. Map interface revisited

Quick const tips (common troubles with PQ)

Templates (similar to Java generics)

2. TreeMap

3. HashMap

2

Page 3: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Review: the Map

Interface

Page 4: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Map Interface

Want to hold (key, value) associations

Want to be able to “look up” values quickly using

the key

“Siri, what is Maria’s phone number?”

key = “Maria”, value = 650-555-0176

We want key and value to be customizable

Unlike your PQ assignment where all keys were string

You’ve been using customizable classes like this:

Map<int,string>

Now we will learn how to make one!

Page 5: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Map Interface: hash-map.h

template <typename Key, typename Value> // CUSTOM!

class HashMap {

public:

HashMap(int sizeHint);

~HashMap();

bool isEmpty() const { return size() == 0; }

int size() const { return count; }

bool containsKey(const Key& key) const;

void put(const Key& key, const Value& value);

Value get(const Key& key) const;

Value& operator[](const Key& key);

bool MYSTERY(const Key& key1, const Key& key2);

Page 6: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Will this compile?

bool HashMap::MYSTERY(const Key& key1,

const Key& key2){

return key1 == key2;

}

A. YES!

B. No, there is a problem with const

C. No, there is a problem with Key

D. No, there is some other problem

E. Other/none/more

Page 7: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Will this compile?bool HashMap::MYSTERY(const Key& key1,

const Key& key2){

return key1 == key2;

}

Syntax detail: We can’t actually just say “HashMap::MYSTERY”

We need to note that this is a template class:

template <typename Key, typename Value>

bool HashMap<Key,Value>::MYSTERY

(const Key& key1, const Key& key2){

return key1 == key2;

}

Page 8: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Map Interface: hash-map.h…

private:

struct node {

Key key;

Value value;

node *next;

};

node **buckets;

int numBuckets;

int count;

int hash(const Key& key) const;

node *ensureNodeExists(const Key& key);

const node *findNode(const Key& key) const;

};

#include "hash-map-impl.h“ // WHY??

Page 9: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

bool containsKey(const Key& key)

const;

const on the key means that the

containsKey method promises not to change

key—user entrusts us with key reference only

because of this contract

const at the end means containsKey

method promises not to change the data of

the HashMap class

Why do we care about this promise? We ARE

the HashMap class! Why would we promise not

to change our own private data?

Page 10: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

const

class PQVec {

public:

void myconstmethod() const;

private:

Vector<int> myvec;

};

void PQVec::myconstmethod() const {

myvec.add(5); //will not compile

myvec.size(); //will compile because

//size() is const

}

vector:int size() const;

Page 11: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Hash Tables (HashMaps)Implementing the Map interface with Hash Tables

Page 12: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Imagine you want to look up your

neighbors’ names, based on their house

number (all on your same street)

House numbers: 10565 through 90600

(roughly 1000 houses—there are varying

gaps in house numbers between houses)

Names: one last name per house

Page 13: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Options

Some kind of tree (next week!)

Linked list

Array

Page 14: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Hash Table is just a modified,

more flexible array

Keys don’t have to be integers 0-(size-1)

(Ideally) avoids big gaps like our gap from

0 to 10565 in the house numbers and

between numbers

Hash function is what makes this possible!

Page 15: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Closed AddressingArray

index

Value

0

1

2

3

4

5

6

7

8

Where does

key=“Annie”

value=10 go if

hashkey(“Annie”)

= 3?

Where does

key=“Solange”

value=12 go if

hashkey(“Solange”)

= 5?

Page 16: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Map Interface: hash-map.h…

private:

struct node {

Key key;

Value value;

node *next;

};

node **buckets;

int numBuckets;

int count;

int hash(const Key& key) const;

node *ensureNodeExists(const Key& key);

const node *findNode(const Key& key) const;

};

#include "hash-map-impl.h“ // WHY??

Page 17: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Closed AddressingArray

index

Value

0

1

2

3

4

5

6

7

8

Assume hashkey(“Julian”) = 3. How many of the following cause a hash collision?

A. Try to insert key=“Annie”

B. Try to insert key=“Julian”

C. Try to insert value=10

D. Other/none/more

Page 18: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Closed AddressingArray

index

Value

0

1

2

3

4

5

6

7

8

Where does key=“Annie” value=55 go if hashkey(“Annie”) = 3?

A. 55 overwrites 10 at 3

B. A link list node is added at 3

C. Other/none/ more than one

Page 19: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Hash key collisions

Hash function takes key and maps it to an integer

Sometimes will map two DIFFERENT keys to the same integer

This is a “collision”

We can NOT overwrite the value the way we would if it really were the same key

Need a way of storing multiple values in a given “place” in the hash table

Page 20: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

TreeMapAn implementation of the Map (or “Dictionary”)

interface that has guaranteed log(n) worst case.

Page 21: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Implementing Map interface

with a Binary Search Tree (BST)

Usually we think of a hash table as the go-

to implementation of the Map interface

But Binary Search Trees are another

option

C++’s Standard Template Library (STL)

uses a Red-Black tree for their Map

Page 22: CSE 12 Basic Data Structures - Stanford Universitystanford.edu › class › archive › cs › cs106x › cs106x.1142 › ... · CSE 12 –Basic Data Structures Dr. Cynthia Bailey

Binary Search Tree