Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Prefix trees:
Comparison between
Trie, Ternary Search
Tree and Radix Tree
 Feb. 26, 2016
 Data Structures
 C++  Prefix Tree  Trie 
Ternary Search Tree  Radix Tree

Table of Contents
Table of Contents
Introduction
Introduction
Trie
Ternary Search Tree
Radix Tree
Comparison
Insert
Search
Found
Not found
Oscar Forner Erase
I am a software engineer at VCA Found
Technology. Passionate about Not found
GNU/Linux and Open Source.
Interested in system Get keys
programming languages, Get keys prefix
operating systems and compilers. Longest common path

Conclusion
 Blog
 Projects Introduction
 Resume In this post I talk about three data
structures I implemented to
     compare their performance
different scenarios. The three data
in

© 2018 Oscar Forner Martinez. structures are Trie, Ternary Search


Tree and Radix Tree.

1 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

All the code for the data


structures as well as the tested
scenarios are available in this
repo in GitHub.

Introduction
What is a prefix tree? It is an
ordered tree data structure that is
used to store a dynamic set of
elements where the key is a string.
All the data structures that I
implemented have the following
methods:

Operation Description
Removes all the
clear content of the data
structure
Returns the value
stored in the data
find
structure for a
specific key provided
Adds a new pair of
insert key and value to the
data structure
Returns the amount
Oscar Forner size of elements stored in
the data structure
I am a software engineer at VCA
Technology. Passionate about Prints the content of
GNU/Linux and Open Source. show the data structure in
Interested in system the dot format
programming languages, Removes the value
operating systems and compilers. stored in the data
erase
structure for a
 Blog specific key provided
 Projects Returns true if there
 Resume contains is a value associated
to the key provided

     Returns a std::vector
with all the keys in
keys the data structure
© 2018 Oscar Forner Martinez.
that have the

2 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Operation Description
provided prefix. If no
prefix is provided, it
returns a std::vector
with all the keys in
the data structure.
Returns the longest
common prefix of all
lcp
keys stored in the
data structure.

Trie
Unlike a binary search tree, none of
the nodes in the tree stores the key
associated with that node. Instead,
its position in the tree defines
the key with which it is associated.
All the descendants of a node have
a common prefix of the string
associated with that node, and the
root is associated with the empty
string. Values are not necessarily
associated with every node. Rather,
values tend only to be
associated with leaves, and with
some inner nodes that correspond to
keys of interest.
Oscar Forner
Example of a Trie that contains the
I am a software engineer at VCA keys: "He", "Hello", "Kthulu", "No",
Technology. Passionate about "Wololo", "World", "Worst", "he":
GNU/Linux and Open Source.
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume

    
© 2018 Oscar Forner Martinez.

3 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Ternary Search Tree


In a Ternary Search Tree nodes
are arranged in a manner similar to
a binary search tree, but with up
to three children rather than the
binary tree's limit of two. Like other
prefix trees, a ternary search tree
can be used as an associative map
structure with the ability for
incremental string search.
However, ternary search trees are
more space efficient compared to
standard prefix trees, at the cost of
speed.

Example of a Ternary Search Tree


that contains the keys: "He", "Hello",
"Kthulu", "No", "Wololo", "World",
"Worst", "he":

Oscar Forner
I am a software engineer at VCA
Technology. Passionate about
GNU/Linux and Open Source.
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume

    
© 2018 Oscar Forner Martinez.

4 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Radix Tree
Radix Tree is a space-optimized
Trie in which each node that is the
only child is merged with its
parent. The result is that the
number of children of every
internal node is at least the
radix *r of the radix trie, where r
is a positive integer and a power
x of 2, having x* >= 1. Unlike in
regular tries, edges can be
labeled with sequences of
elements as well as single
elements. This makes radix trees
much more efficient for small
sets (especially if the strings are
long) and for sets of strings that
share long prefixes.

Example of a Radix Tree that


contains the keys: "He", "Hello",
"Kthulu", "No", "Wololo", "World",
"Worst", "he":

Oscar Forner
I am a software engineer at VCA
Technology. Passionate about
GNU/Linux and Open Source.
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume
Comparison
     In this section I show a comparison
of the performance for the three
© 2018 Oscar Forner Martinez. data structures in different
scenarios. Note: sometimes the

5 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Trie runs out of memory.


Therefore, there is no data on
these cases.

Insert

Search

Found

Oscar Forner
I am a software engineer at VCA
Technology. Passionate about
GNU/Linux and Open Source. Not found
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume

    
© 2018 Oscar Forner Martinez.
Erase

6 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Found

Not found

Get keys

Oscar Forner
I am a software engineer at VCA
Technology. Passionate about
GNU/Linux and Open Source.
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume Get keys prefix

    
© 2018 Oscar Forner Martinez.

7 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

Longest common path

Conclusion
The Radix Tree is in average the
best of the three data structures.
Nevertheless, I found out a paper
where it explains how to implement
a Balanced Ternary Search Tree,
because one of the weakness of the
Ternary Search Tree is that its
Oscar Forner shape depends of the order of the
insertions. I have plenty of work
I am a software engineer at VCA to do regarding this topic but I
Technology. Passionate about wanted to share with you some of
GNU/Linux and Open Source. the preliminar results. Anyway, I am
Interested in system working on writing a paper
programming languages, explaining with more detail this
operating systems and compilers. experiment and the way I conducted
the expertiment.
 Blog
 Projects
 Resume

    
© 2018 Oscar Forner Martinez.

8 of 9 14/04/19, 2:51 am
Oscar Forner's personal website https://oscarforner.com/2016/02/26/Prefix_trees...

FFF a mocking Linux Kernel


frameowork Module 
for C example.
Rickroll prank.

Temporary disabled
comments until I found a
replacement for Disqus

Oscar Forner
I am a software engineer at VCA
Technology. Passionate about
GNU/Linux and Open Source.
Interested in system
programming languages,
operating systems and compilers.

 Blog
 Projects
 Resume

    
© 2018 Oscar Forner Martinez.

9 of 9 14/04/19, 2:51 am

You might also like