Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of...

50
Tries

Transcript of Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of...

Page 1: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

Page 2: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

• We have seen a few data structures that handle the mapping of key-value pairs.• Arrays: The key is the element index, the value is the data at that

location.• Hash tables: The key is the hash code of the data, the value is a linked

list of data hashing to that hash code.

• What about a slightly different kind of data structure where the key is guaranteed to be unique, and the value could be as simple as a Boolean that tells you whether the data exists in the structure?

Page 3: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

• Tries combine structures and pointers together to store data in an interesting way.

• The data to be searched for in the trie is now a roadmap. • If you can follow the map from beginning to end, the data exists in

the trie.

• If you can’t, it doesn’t.

• Unlike with a hash table, there are no collisions, and no two pieces of data (unless they are identical) have the same path.

Page 4: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

• Let’s map key-value pairs where the keys are four-digit years (YYYY) and the values are names of universities founded during those years.

• In a trie, the paths from a central root node to a leaf node (where the school names would be), would be labeled with digits of the year.

• Each node on the path from root to leaf could have 10 pointers emanating from it, one for each digit.

Page 5: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

• To insert an element into the trie, simply build the correct path from the root to the leaf.

Page 6: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

typedef struct _trie

{

char university[20];

struct _trie* paths[10];

}

trie;

Page 7: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

typedef struct _trie

{

char university[20];

struct _trie* paths[10];

}

trie;

Page 8: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

Page 9: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “” Insert “Harvard”, founded 1636

Page 10: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

0 1 2 3 4 5 6 7 8 9

Insert “Harvard”, founded 1636

Page 11: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

Page 12: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

0 1 2 3 4 5 6 7 8 9

Page 13: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

6

“”

Page 14: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

6

“”

0 1 2 3 4 5 6 7 8 9

Page 15: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

6

“”

3

“”

Page 16: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

6

“”

3

“”

6

“”

Page 17: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Harvard”, founded 1636

“”

6

“”

3

“”

6

“Harvard”

Page 18: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6

“”

3

“”

6

“Harvard”

Page 19: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6

“”

3

“”

6

“Harvard”

Page 20: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6

“”

3

“”

6

“Harvard”

Page 21: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6

“”

3

“”

6

“Harvard”

Page 22: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6 7

“”

3

“”

6

“Harvard”

“”

Page 23: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

Page 24: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“”

Page 25: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Yale”, founded 1701

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 26: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 27: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 28: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 29: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 30: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0

“”

1

“Yale”

Page 31: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

Page 32: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“”

Page 33: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

Insert “Dartmouth”, founded 1769

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Page 34: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Page 35: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries

• To search for an element in the trie, use successive digits to navigate from the root, and if you can make it to the end without hitting a dead end (a NULL pointer), you’ve found it.

Page 36: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Page 37: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 38: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 39: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 40: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 41: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 42: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 43: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Harvard”, founded 1636

Page 44: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Page 45: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746

Page 46: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746

Page 47: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746

Page 48: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746

Page 49: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746

Page 50: Tries - CS50cdn.cs50.net/2017/fall/shorts/tries/tries.pdf · (YYYY) and the values are names of universities founded during those years. •In a trie, the paths from a central rootnode

Tries “”

1

“”

6 7

“”

3

“”

6

“Harvard”

“”

0 6

“”

1

“Yale”

“”

9

“Dartmouth”

Search for “Princeton”, founded 1746