Tries - PowerPoint PPT Presentation

About This Presentation
Title:

Tries

Description:

The branching is determined by a partial key value. ... web search engine (search by index word) network router (search by IP address) 5 ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 12
Provided by: vdou8
Category:
Tags: engine | is | phonebook | search | tries | what

less

Transcript and Presenter's Notes

Title: Tries


1
Tries
  • When searching for the name Smith in a phone
    book, we first locate the group of names
    starting with S, then within those we search
    for m, etc.
  • Idea Perform a search based on a prefix of the
    key, rather than a comparison of the whole key.
  • The branching is determined by a partial key
    value.
  • Each node branches out to as many nodes as there
    are characters.

2
Tries
  • A Trie is a multi-way tree for storing strings in
    which
  • there is one node for every common prefix
  • the strings are stored in the leaves
  • The order of the trie is m, where m is the size
    of the alphabet.

3
Tries
  • Example The set of strings "mat", "mad, "am",
    "bad" stored in a BST and in a trie

m
mat
a
b
am
mad
m
a
a
am
bad
d
t
d
mat
mad
bad
4
Tries
  • Advantage
  • The height of the tree depends on the length of
    the keys
  • A trie can be used to store very large sets but
    the height (and therefore the search time) is
    very short.
  • Applications
  • spell checker
  • web search engine (search by index word)
  • network router (search by IP address)

5
Example (slightly compressed...)
Alphabet A, P, S, T Words A, APT, AT,
PAT, PASS, PAST, PS, SAP, SAT, TAP
6
Tries
  • Observation Many nodes have few non-null
    pointers.
  • We would like to save space
  • Idea 1 Briandais tree
  • Store only the pointers that are used
  • Maintain all siblings in a linked list
  • Disadvantages
  • the list needs to be traversed linearly
  • we still use extra space for the pointers

7
Tries
  • Observation Many nodes have few non-null
    pointers.
  • We would like to save space
  • Idea 2 Compressed Trie
  • Interleave the arrays in the nodes
  • Minor disadvantage
  • an unsuccessful search may take more steps to end.

8
Compressed Tries
A
P
S

T
node 1
?
?
?
A
P
S

T
A
P
S

T
node 3
?
?
?
?
?
node 2
A
AT
PS
PA
APT
node 1 node 2 node 3
p1 stands for pointer to node 1
p2
p3
?
A
APT
AT
PA
PS
9
PATRICIA trees
  • Observation
  • Sometimes the actual set of keys is a small
    subset of the potential set of keys.
  • This may result in a large number of nodes that
    only have one descendant.
  • We would like to save some space
  • Idea
  • Make the tree more compact by collapsing long
    chains.
  • The resulting tree is called a PATRICIA tree

Practical Algorithm To Retrieve Information
Coded In Alphanumeric
10
PATRICIA trees
  • Idea
  • Collapse chains of nodes that have only one child
  • For each branch indicate how many characters
    should be skipped (i.e. what the length of the
    collapsed chain is)

11
Patricia tree example
Write a Comment
User Comments (0)
About PowerShow.com