1 of 59

1

CS61B, Fall 2024 @ UC Berkeley

Slides credit: Josh Hug

Tries

Lecture 28

2 of 59

Tries (Conceptual)

Lecture 28, CS61B, Fall 2024

Tries (Conceptual)

Trie Implementation and�Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

3 of 59

Abstract Data Types vs. Specific Implementations

There are many ways to implement an abstract data type.

  • Today we’ll talk about a new way to build a set/map.

PQ

List

Set

Map

DisjointSets

Separate Chaining Hash Table

LinkedList

Resizing Array

Heap

LLRB

BST (Vanilla)

B-Trees (2-3 / 2-3-4)

Heap

Ordered Linked List

Balanced Tree

Resizing Array

LinkedList

Quick Find

Quick Union

Weighted QU

WQUPC

4 of 59

BST and Hash Table Set Runtimes

Runtimes for our Balanced Search Tree and Hash Table implementations were very fast.

If we know that our keys all have some common special property, we can sometimes get even better implementations.

Example: Suppose we know our keys are always single ASCII characters.

  • e.g. ‘a’, ‘g’, ‘!’

contains(x)

add(x)

Balanced BST

Θ(log N)

Θ(log N)

Resizing Separate�Chaining Hash Table

Θ(1)

assuming even spread

Θ(1)

on average,

assuming even spread

5 of 59

Special Case 1: Character Keyed Map

Suppose we know that our keys are always ASCII characters.

  • Can just use an array!
  • Simple and fast.

public class DataIndexedCharMap<V> {

private V[] items;

public DataIndexedCharMap(int R) {

items = (V[]) new Object[R];

}

public void put(char c, V val) {

items[c] = val;

}

public V get(char c) {

return items[c];

}

}

R is the number of possible characters, e.g. 128 for ASCII.

key type

get(x)

add(x)

Balanced BST

comparable

Θ(log N)

Θ(log N)

Resizing Separate Chaining Hash Table

hashable

Θ(1)

assuming even spread

Θ(1)

on average,

assuming even spread

data indexed array

chars

Θ(1)

Θ(1)

6 of 59

Special Case 2: String Keyed Map

Suppose we know that our keys are always strings.

  • Can use a special data structure known as a Trie.
  • Basic idea: Store each letter of the string as a node in a tree.

Tries will have great performance on:

  • get
  • add
  • special string operations�

key type

get(x)

add(x)

Balanced BST

comparable

Θ(log N)

Θ(log N)

Resizing Separate Chaining Hash Table

hashable

Θ(1)

assuming even spread

Θ(1)

on average,

assuming even spread

data indexed array

chars

Θ(1)

Θ(1)

Tries

Strings

?

?

7 of 59

Sets of Strings

Suppose we have a set containing “sam”, “sad”, “sap”, “same”, “a”, and “awls”.

  • Below, we see the BST and Hash Table representation.

sad

same

sap

awls

a

0

1

2

3

sad

awls

a

same

sap

sam

sam

BST

Hash Table

8 of 59

Tries: Each Node Stores One Character

For String keys, we can use a “Trie”. Key ideas:

  • Every node stores only one letter.
  • Nodes can be shared by multiple keys.

Above, we show the results of adding “sam” and sad”. Use your intuition to try to insert the remaining items “sap”, “same”, “a”, and “awls”.

s

a

m

d

9 of 59

Tries: Each Node Stores One Character

For String keys, we can use a “Trie”. Key ideas:

  • Every node stores only one letter.
  • Nodes can be shared by multiple keys.

Above, we show the results of adding “sam” and sad”. Use your intuition to try to insert the remaining items “sap”, “same”, “a”, and “awls”.

s

a

m

d

p

e

a

10 of 59

Tries: Each Node Stores One Character

For String keys, we can use a “Trie”. Key ideas:

  • Every node stores only one letter.
  • Nodes can be shared by multiple keys.

Try to figure out a way to make it clear that our set contains “sam”, “sad”, “sap”, “same”, “a”, and “awls”, but not “aw”, “awl”, “sa”, etc.

s

a

m

d

p

e

a

w

l

s

11 of 59

Tries: Each Node Stores One Character

For String keys, we can use a “Trie”. Key ideas:

  • Every node stores only one letter.
  • Nodes can be shared by multiple keys.

Try to figure out a way to make it clear that our set contains “sam”, “sad”, “sap”, “same”, “a”, and “awls”, but not “aw”, “awl”, “sa”, etc.

s

a

m

d

p

e

a

w

l

s

s

12 of 59

Tries: Search Hits and Misses

Suppose we insert “sam”, “sad”, “sap”, “same”, “a”, and “awls”.

  • contains(“sam”):
  • contains(“sa”):
  • contains(“a”):
  • contains(“saq”):

s

a

m

d

p

e

a

w

l

s

Two ways to have a search “miss”:

  • If the final node is white.
  • If we fall off the tree, e.g. contains(“sax”).

true, blue node

false, white node

true, blue node

false, fell off tree

“hit”

“miss”

13 of 59

Trie Maps

Tries can also be maps, of course.

For an animated demo of the creation of this map, see this demo from our optional Algorithms textbook.

e.g. maps “by” to 4.

y 4

b

s

e

a 6

l

l

s 1

h

e 0

l

l

s 3

o

r

e 7

t

h

e 5

14 of 59

Tries: A Digit-by-Digit Set Representation

s

a

m

d

p

e

a

w

l

s

sad

same

sap

awls

a

0

1

2

3

sad

awls

a

same

sap

sam

sam

BST

HashSet

Trie

15 of 59

Tries

Trie:

  • Short for Retrieval Tree.
  • Inventor Edward Fredkin suggested it should be pronounced “tree”, but almost everyone pronounces it like “try”.

16 of 59

Trie Implementation and Performance

Lecture 28, CS61B, Fall 2024

Tries (Conceptual)

Trie Implementation and Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

17 of 59

Very Basic Trie Implementation

The first approach might look something like the code below.

  • Each node stores a letter, a map from c to all child nodes, and a color.

s

a

m

d

p

e

a

w

l

s

public class TrieSet {

private static final int R = 128; // ASCII

private Node root; // root of trie

private static class Node {

private char ch;

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey = b;

next = new DataIndexedCharMap<>(R);

}

}

}

Since we know our keys are characters, can use a DataIndexedCharMap.

18 of 59

Zooming in On a Node

Each DataIndexedCharMap is an array of 128 possible links, mostly null.

a

w

...

...

private static class Node {

private char ch;

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey = b;

next = new DataIndexedCharMap<>(R);

}

19 of 59

Zooming in On a Node

Better drawing of a DataIndexedCharMap based trie is shown to the right.

a

w

private static class Node {

private char ch;

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey = b;

next = new DataIndexedCharMap<>(R);

}

a

w

w

...

128 links, with one used, and 127 equal to null.

20 of 59

Very Basic Trie Implementation

If we use a DataIndexedCharMap to track children, every node has R links.

private static class Node {

private char ch;

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey = b;

next = new DataIndexedCharMap<>(R);

}

s

a

d

a

w

l

a

s

a

d

w

l

...

...

...

...

...

...

...

...

public class DataIndexedCharMap<V> {

private V[] items;

public DataIndexedCharMap(int R) {

items = (V[]) new Object[R];

}

...

}

21 of 59

Very Basic Trie Implementation

Observation: The letter stored inside each node is actually redundant.

  • Can remove from the representation and things will work fine.

public class TrieSet {

private static final int R = 128; // ASCII

private Node root; // root of trie

private static class Node {

private char ch;

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey = b;

next = new DataIndexedCharMap<>(R);

}

}

}

a

s

a

d

w

l

...

...

...

...

...

...

...

...

22 of 59

Trie Performance in Terms of N

Given a Trie with N keys. What is the: [N = 6]

  • Add runtime?
  • Contains runtime?

a

s

a

d

w

l

...

...

...

...

...

...

...

...

s

m

p

e

...

...

23 of 59

Trie Performance in Terms of N

Given a Trie with N keys. What is the: [N = 6]

  • Add runtime? Θ(1)
  • Contains runtime? Θ(1)

a

s

a

d

w

l

...

...

...

...

...

...

...

...

s

m

p

e

...

...

Runtimes independent of number of keys!

Or in terms of L, the length of the key:

  • Add: Θ(L)
  • Contains: O(L)

24 of 59

Trie Performance in Terms of N

When our keys are strings, Tries give us slightly better performance on contains and add.

One downside of the DataIndexedCharMap-based Trie is the huge memory cost of storing R links per node.

  • Wasteful because most links are unused in real world usage.

Runtimes treating length of keys as a constant

key type

get(x)

add(x)

Balanced BST

comparable

Θ(log N)

Θ(log N)

Resizing Separate Chaining Hash Table

hashable

Θ(1)

assuming even spread

Θ(1)

on average,

assuming even spread

data indexed array

chars

Θ(1)

Θ(1)

Tries

Strings

Θ(1)

Θ(1)

25 of 59

Alternate Child Tracking Strategies

Lecture 28, CS61B, Fall 2024

Tries (Conceptual)

Trie Implementation and�Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

26 of 59

Trie Performance in Terms of N

Using a DataIndexedCharMap is very memory hungry.

  • Every node has to store R links, most of which are null.

a

s

a

d

w

l

private static class Node {

private boolean isKey;

private DataIndexedCharMap<Node> next;

private Node(char c, boolean b, int R) {

ch = c; isKey= b;

next = new DataIndexedCharMap<>(R);

}

27 of 59

The DataIndexedCharMap Trie

Can use ANY kind of map from character to node, e.g.

  • BST
  • Hash Table

isKey:

links:

F

...

97

98

100

99

...

isKey:

links:

F

...

97

98

100

99

...

isKey:

links:

T

...

97

98

100

99

...

a

d

isKey:

links:

T

...

97

98

100

99

...

c

Fundamental problem, our arrays are ‘sparse’.

28 of 59

Alternate Idea #1: The Hash-Table Based Trie

isKey:

F

a

d

c

links:

isKey:

F

links:

isKey:

T

links:

c

a

0

1

2

3

d

0

1

2

3

0

1

2

3

29 of 59

Alternate Idea #2: The BST-Based Trie

isKey:

F

a

d

c

‘c’

‘a’

links:

isKey:

F

‘d’

links:

isKey:

T

links:

isKey:

T

links:

30 of 59

The Three Trie Implementations

When we implement a Trie, we have to pick a map to our children

  • DataIndexedCharMap: Very fast, but memory hungry.
  • Hash Table: Almost as fast, uses less memory.
  • Balanced BST: A little slower than Hash Table, uses similar amount of memory?

dictionary from letter to Node

DataIndexedCharMap

Hash Table

Balanced BST

this.next

31 of 59

Performance of the DataIndexedCharMap, BST, and Hash Table Trie

Using a BST or a Hash Table to store links to children will usually use less memory.

  • DataIndexedCharMap: 128 links per node.
  • BST: C links per node, where C is the number of children.
  • Hash Table: C links per node.
  • Note: Cost per link is higher in BST and Hash Table.

Using a BST or a Hash Table will take slightly more time.

  • DataIndexedCharMap is Θ(1).
  • BST is O(log R), where R is size of alphabet.
  • Hash Table is O(R), where R is size of alphabet.

Since R is fixed (e.g. 128), can think of all 3 as Θ(1).

s

a

m

d

p

e

a

w

l

s

32 of 59

Trie Performance in Terms of N

When our keys are strings, Tries give us slightly better performance on contains and add.

  • Using BST or Hash Table will be slightly slower, but more memory efficient.
  • Would have to do computational experiments to see which is best for your application.

… but where Tries really shine is their efficiency with special string operations!

Runtimes treating length of keys as a constant

key type

get(x)

add(x)

Balanced BST

comparable

Θ(log N)

Θ(log N)

Resizing Separate Chaining Hash Table

hashable

Θ(1)

assuming even spread

Θ(1)

on average,

assuming even spread

data indexed array

chars

Θ(1)

Θ(1)

Tries (BST, Hash Table, Data Indexed Char Map)

Strings

Θ(1)

Θ(1)

33 of 59

Trie String Operations

Lecture 28, CS61B, Fall 2024

Tries (Conceptual)

Trie Implementation and�Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

34 of 59

String Specific Operations

Theoretical asymptotic speed improvement is nice. But the main appeal of tries is their ability to efficiently support string specific operations like prefix matching.

  • Finding all keys that match a given prefix: keysWithPrefix("sa")
  • Finding longest prefix of: longestPrefixOf("sample")

s

a

m

d

p

e

a

w

l

s

sad

same

sap

awls

a

sam

0

1

2

3

sad

awls

a

same

sap

sam

35 of 59

Prefix Matching Operations

Theoretical asymptotic speed improvement is nice. But the main appeal of tries is their ability to efficiently support string specific operations like prefix matching.

s

a

m

d

p

e

a

w

l

s

Examples:

  • Finding the longest prefix of a string: longestPrefixOf("sample")
    • Result: sam
  • Finding all keys that match a given prefix: keysWithPrefix("sa")
    • Result: [sad, sam, same, sap]

36 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect() returns ["a", "awls", "sad", "sam", "same", "sap"]

s

a

m

d

p

e

a

w

l

s

37 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect() returns ["a", "awls", "sad", "sam", "same", "sap"]

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

s

a

m

d

p

e

a

w

l

s

38 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect() returns ["a", "awls", "sad", "sam", "same", "sap"]

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

39 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = []

colHelp("a", x, )

40 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = ["a"]

colHelp("aw", x, )

colHelp("a", x, )

41 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = ["a"]

colHelp("a", x, )

colHelp("aw", x, )

colHelp("awl", x, )

42 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = ["a"]

colHelp("a", x, )

colHelp("aw", x, )

colHelp("awls", x, )

colHelp("awl", x, )

43 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = ["a", "awls"]

colHelp("a", x, )

colHelp("aw", x, )

colHelp("awls", x, )

colHelp("awl", x, )

44 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

colHelp("s", x, )

x = ["a", "awls"]

45 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

colHelp("s", x, )

colHelp("sa", x, )

x = ["a", "awls"]

46 of 59

Challenging Warmup Exercise: Collecting Trie Keys

Challenging Exercise: Give an algorithm for collecting all the keys in a Trie.

collect():

  • Create an empty list of results x.
  • For character c in root.next.keys():
    • Call colHelp(c, x, root.next.get(c)).
  • Return x.

colHelp(String s, List<String> x, Node n):

  • If n.isKey, then x.add(s).
  • For character c in n.next.keys():
    • Call colHelp(s + c, x, n.next.get(c))

s

a

m

d

p

e

a

w

l

s

x = ["a", "awls", "sad",

"sam", "same", "sap"]

colHelp("s", x, )

colHelp("sa", x, )

...

47 of 59

Usages of Tries

Challenge: Give an algorithm for keysWithPrefix.

  • Example: keysWithPrefix("sa") is ["sad", "sam", "same", "sap"].

s

a

m

d

p

e

a

w

l

s

48 of 59

Usages of Tries

Challenge: Give an algorithm for keysWithPrefix.

  • Example: keysWithPrefix("sa") is ["sad", "sam", "same", "sap"].

Algorithm:

  • Find the node α corresponding to the string (in pink).
  • Create an empty list x.
  • For character c in α.next.keys():
    • Call colHelp("sa" + c, x, α.next.get(c)).

Another common operation: LongestPrefixOf. See lab.

s

a

m

d

p

e

a

w

l

s

49 of 59

Autocomplete

Lecture 28, CS61B, Fall 2024

Optional, see videos if you're curious.

Tries (Conceptual)

Trie Implementation and�Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

50 of 59

The Autocomplete Problem

Example, when I type “how are” into Google, I get 10 results, shown to the right.

One way to do this is to create a Trie based map from strings to values

  • Value represents how important Google thinks that string is.
  • Can store billions of strings efficiently since they share nodes.
  • When a user types in a string “hello”, we:
    • Call keysWithPrefix("hello").
    • Return the 10 strings with the highest value.

51 of 59

Autocomplete Example, for Top Three Matches

Suppose we have six strings with values shown below:

  • buck: 10
  • sad: 12
  • smog: 5
  • spit: 15
  • spite: 20
  • spy: 7 �

If the user types “s”, we:

  • Call keysWithPrefix("s").
    • sad, smog, spit, spite, spy
  • Return the three keys with highest value.
    • spit, spite, sad

s

a

m

d

p

o

b

u

c

k

g

y

i

t

e

10

12

5

15

20

7

52 of 59

Autocomplete Example, for Top Three Matches

Suppose we have six strings with values shown below:

  • buck: 10
  • sad: 12
  • smog: 5
  • spit: 15
  • spite: 20
  • spy: 7 �

If the user types “s”, we:

  • Call keysWithPrefix("s").
    • sad, smog, spit, spite, spy
  • Return the three keys with highest value.
    • spit, spite, sad

s

a

m

d

p

o

b

u

c

k

g

y

i

t

e

53 of 59

The Autocomplete Problem

One way to do this is to create a Trie based Dictionary that maps strings to values.

  • When a user types in a string hello, we:
    • Call keysWithPrefix("hello").
    • Return the ten strings with the highest value.

The approach above has one major flaw. If we enter a short string, the number of keys with the appropriate prefix will be too big.

  • We are collecting billions of results only to keep 10!
  • This is extremely inefficient.

54 of 59

A More Efficient Autocomplete

One way to address this issue:

  • Each node stores its own value, as well as the value of its best substring.

s

a

m

d

p

o

b

u

c

k

g

y

i

t

e

None

10

None

10

None

10

10

10

value = None

best = 20

None

20

None

12

12

12

None

5

None

5

5

5

None

20

None

20

15

20

20

20

20

7

7

55 of 59

A More Efficient Autocomplete

One way to address this issue:

  • Each node stores its own value, as well as the value of its best substring.

Search will consider nodes in order of “best”.

  • Consider ‘sp’ before ‘sm’.
  • Can stop when top 3 matches are all better than best remaining.

s

a

m

d

p

o

b

u

c

k

g

y

i

t

e

None

10

None

10

None

10

10

10

value = None

best = 20

None

20

None

12

12

12

None

5

None

5

5

5

None

20

None

20

15

20

20

20

20

7

7

56 of 59

Even More Efficient Autocomplete

Can also merge nodes that are redundant!

  • This version of trie is known as a “radix tree” or “radix trie”.
  • Won’t discuss.

s

ad

mog

p

buck

y

it

e

10

10

value = None

best = 20

None

20

12

12

5

5

None

20

15

20

20

20

20

7

7

57 of 59

Trie Summary

Lecture 28, CS61B, Fall 2024

Tries (Conceptual)

Trie Implementation and�Performance

Alternate Child Tracking Strategies

Trie String Operations

Autocomplete

Trie Summary

58 of 59

Tries

When your key is a string, you can use a Trie:

  • Theoretically better performance than hash table or search tree.
  • Have to decide on a mapping from letter to node. Three natural choices:
    • DataIndexedCharMap, i.e. an array of all possible child links.
    • Bushy BST.
    • Hash Table.
  • All three choices are fine, though hash table is probably the most natural.
  • Supports special string operations like longestPrefixOf and keysWithPrefix.
    • keysWithPrefix is the heart of important technology like autocomplete.
    • Optimal implementation of Autocomplete involves use of a priority queue!

Bottom line: Data structures interact in beautiful and important ways!

59 of 59

Domain Specific Sets and Maps

More generally, we can sometimes take special advantage of our key type to improve our sets and maps.

  • Example: Tries handle String keys. Allow for fast string specific operations.
  • Note: There are many other types of string sets/maps out there.
    • Suffix Trees (Link).
    • DAWG (Link).
    • Won’t discuss in our course.