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

Post on 04-Jul-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSE 12 – Basic

Data StructuresDr. Cynthia Bailey Lee

Today’s Topics

1. Map interface revisited

Quick const tips (common troubles with PQ)

Templates (similar to Java generics)

2. TreeMap

3. HashMap

2

Review: the Map

Interface

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!

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

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

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;

}

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

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?

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;

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

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

Options

Some kind of tree (next week!)

Linked list

Array

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!

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?

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

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

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

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

TreeMapAn implementation of the Map (or “Dictionary”)

interface that has guaranteed log(n) worst case.

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

Binary Search Tree

top related