string data structures - stanford university...says that, given two strings w and x, that w is a...

139
String Data Structures

Upload: others

Post on 29-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

String Data Structures

Page 2: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Why do we need string data structures?

Page 3: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Why do we need string data structures?

Page 4: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Why do we need string data structures?

Page 5: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Why do we need string data structures?

Page 6: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

String Data StructuresBefore Computers

Page 7: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Can binary search for a word in time O(log n).

Can interpolation search in average time O(log log n).

Page 8: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Array accesses take time O(1).

Jump to the drawer, then do an O(d) lookup, whered is the number of elements in the drawer.

Page 9: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Miriam-Webster’s physical “Backward Index:”All English words, written in reverse, in sorted order.

Why would you want this?

Page 10: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Find all words ending in “iatrics.”

Time required: O(log n + k).

Page 11: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

String Data StructuresWith Computers

Page 12: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

aaboutad

adageadagiobarbardbarnbedbetbetacancanecat

dikdikdiktat

Page 13: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

aaboutad

adageadagio

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

Page 14: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

aaboutad

adageadagio

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

The double circle means

“this is a word.”

The double circle means

“this is a word.”

Page 15: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

aboutad

adageadagio

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

Because we’ve remembered that “a” is a word, we’ll remove it from our list.

Because we’ve remembered that “a” is a word, we’ll remove it from our list.

Page 16: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

aboutad

adageadagio

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

Page 17: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

b d

adadageadagio

Page 18: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

b d

adadageadagio

Page 19: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

b d

adadageadagio

Page 20: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarnbedbetbeta

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

Page 21: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about barbardbarn

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

Page 22: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about barbardbarn

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

Page 23: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarn

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

Page 24: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

barbardbarn

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

Page 25: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

bardbarn

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

Page 26: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

bard

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

barn

Page 27: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

bard

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

barn

Page 28: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

Page 29: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

a

Page 30: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about cancanecat

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

a

Page 31: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

cancane

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

a

n t

cat

Page 32: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

cancane

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

a

n t

cat

Page 33: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

about

cane

dikdikdiktat

a b c d

b d

adageadagio

a e

bedbetbeta

r

d n

a

n t

Page 34: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

a b c d

b

o

u

t

d

a

g

e i

o

a

r

d n

e

d t

a

a

n

e

t

i

k

a

t

i

k

d t

Page 35: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

a b c d

b

o

u

t

d

a

g

e i

o

a

r

d n

e

d t

a

a

n

e

t

i

k

a

t

i

k

d t

This data structure is called a trie. It’s pronounced “try.” It

comes from the word “retrieval.” This is a terrible pun and we’re

stuck with it forever.

This data structure is called a trie. It’s pronounced “try.” It

comes from the word “retrieval.” This is a terrible pun and we’re

stuck with it forever.

Page 36: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Using a Trie

Page 37: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 38: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 39: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 40: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 41: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 42: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 43: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 44: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 45: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 46: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 47: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 48: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 49: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 50: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 51: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 52: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 53: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 54: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 55: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 56: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 57: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 58: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 59: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 60: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 61: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 62: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 63: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 64: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 65: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 66: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 67: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 68: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 69: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 70: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 71: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 72: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 73: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 74: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 75: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 76: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 77: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 78: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 79: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 80: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 81: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 82: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 83: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 84: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 85: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 86: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 87: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 88: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 89: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 90: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 91: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 92: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 93: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 94: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 95: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 96: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 97: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 98: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 99: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 100: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 101: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 102: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 103: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 104: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 105: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 106: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 107: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 108: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 109: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 110: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Other Operations on Tries

● Find all strings in the trie that start with a given prefix.● How might you implement this?

● Print all strings in sorted order.● How might you implement this?

● Find the first string that’s alphabetically before or after another.● How might you implement this?

Page 111: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Useful Perspective

Page 112: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

...an empty list,represented bynullptr, or...

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 113: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Binary Search Tree Is Either...an empty tree, represented by nullptr, or...

x

<x >x

... a single node,whose left subtree

is a BST of smaller values ...

... and whose right subtree is a BST of larger values.

Page 114: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Trie is Either...an empty trie, represented by nullptr, or...

Page 115: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Trie is Either...an empty trie, represented by nullptr, or...

q x

z

a single node, which might be marked as a

word...

... with some number of child tries labeled by

letters.…

Page 116: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

struct Cell { Type value; Cell* next;};

struct Node { Type value; Node* left; Node* right;};

struct Name? {

/* ? */

};

Singly-Linked List Binary Search Tree Trie

Assignment 6: Think through these design

decisions!

Page 117: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Time-Out for Announcements!

Page 118: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it
Page 119: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Assignment 6

● Assignment 6 (MiniBrowser) goes out today. It’s due one week from Friday at the start of class.● Play around with linked lists and tree data

structures!● Build integral pieces of a larger system!● See why all this stuff matters.

● YEAH hours will be held today at 5:00PM in 380-380Y. Slides will be posted.

Page 120: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Back to CS106B!

Page 121: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Twists on Tries(a sneak peek of beautiful CS concepts!)

Page 122: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Twist: Finite Automata

Page 123: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

a b c d

b

o

u

t

d

a

g

e i

o

a

r

d n

e

d t

a

a

n

e

t

i

k

a

t

i

k

d t

General rule to look up a string:

1. Start at some node.

2. Follow links based on theletters you read.

3. The string is there if youdon’t get stuck and land at adouble circle.

General rule to look up a string:

1. Start at some node.

2. Follow links based on theletters you read.

3. The string is there if youdon’t get stuck and land at adouble circle.

Page 124: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

g r a n dm a

p astart

grandmagrandpagreat-grandmagreat-great-grandpagreat-great-great-great-grandpa…

Breaking the Rules

Page 125: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

g r a n dm a

p astart

e

at

-

This isn’t a tree, but we can still follow the same

rules!

This isn’t a tree, but we can still follow the same

rules!

grandmagrandpagreat-grandmagreat-great-grandpagreat-great-great-great-grandpa…

Breaking the Rules

Page 126: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

start * //

a *a

*

/

What sorts of strings does this weird

thingy contain?

What sorts of strings does this weird

thingy contain?

/*aaaa*//***aaaa***//*aaa*aaa*//*********/

Page 127: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Finite Automata

● A finite automaton is a generalization of a trie.

● It’s not necessarily a tree; there can be circular paths, places where branches come together, etc.

● Finite automata power many compilers and pattern-matching tools.

● Want to learn more? Take CS103!

Page 128: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Twist: Suffix Trees

Page 129: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Cancer cells often have multiple repeated copies the same gene.

Given a cancer genome (length ~3,000,000,000 letters)and a gene, count the occurrences of that gene.

Page 130: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b b e r t i i b b eg t

b e

Page 131: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b

b e

b e r t i i b b eg t

Page 132: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b

b e

b e r t i i b b eg t

Page 133: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b b e r t i i b b eg t

b e

Page 134: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b b e r t i i bg

b e

b e t

Page 135: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x

f l i b b e r t i i bg

b e

b e t

Page 136: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

A Fundamental Theorem

● The fundamental theorem of stringology says that, given two strings w and x, that

w is a substring of xif and only if

w is a prefix of a suffix of x● Recall: Tries make it really easy to check

if something is a prefix of any number of strings.

● Idea: Store all the suffixes of a string in a trie!

Page 137: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

e n o s

o

n

s

e

s

n

e

s

n

e

s

n

e

s

e

e

n

e

s

n

e

s

n

e

s

nonsense

Page 138: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Suffix Trees

● With a lot of creativity, it’s possible to compress the trie shown earlier to have only O(n) nodes.

● This is called a suffix tree and is a workhorse of a data structure.

● Want to learn more? Take CS166!

Page 139: String Data Structures - Stanford University...says that, given two strings w and x, that w is a substring of x if and only if w is a prefix of a suffix of x Recall: Tries make it

Next Time

● The Magic of Hash Functions● A beautiful mathematical idea with

incredible power.● Hash Tables

● Surpassing BST performance!