You are on page 1of 25

UNIT5-PART II

• TRIES
• MULTIDIMENSIONAL TREES
Trie data structure

• A trie is a tree-like data structure that implements the dictionary ADT.


• The word “trie” was suggested by Edward Fredkin and is short for “retrieval tree”.
• In a trie, keys are usually strings from some alphabet of fixed size: 
• A={a​0​,a​1​,...,a​m1​​}.
• Unlike in a tree, nodes in a trie, does not store keys. Rather, nodes represent single characters that
can be part of the keys.
• We can therefore start at the root of a trie and follow the edges through the nodes representing s0, s1,
..., sh1
• When we reach the leaf, we will get the string S = s0 s1 ... sh1
• Thus, we can think of a trie as a tree whose each path is a string
• We can think of every internal node of a trie as standing for some prefix of all the strings in the
leaves below it: specifically, an internal node at level kk stands for the first kk characters of each
string below it.
Trie data structure

• Trie is a sorted tree-based data-structure that stores the set of strings.


• Trie is also known as the digital tree or prefix tree. The position of a
node in the Trie determines the key with which that node is connected.
• It has the number of pointers equal to the number of characters of the
alphabet in each node.
• It can search a word in the dictionary with the help of the word's prefix.
• For example, if we assume that all strings are formed from the letters 'a'
to 'z' in the English alphabet, each trie node can have a maximum
of 26 points.
• Prefix : What is prefix:
• The prefix of a string is nothing but any n letters n≤|
S| that can be considered beginning strictly from the
starting of a string. For example , the word “abacaba”
has the following prefixes:
•a
ab
aba
abac
abaca
abacab
Properties of the Trie for a set of the string:
• The root node of the trie always represents the null node.
• Each child of nodes is sorted alphabetically.
• Each node can have a maximum of 26 children (A to Z).
• Each node (except the root) can store one letter of the alphabet.
Why use Trie Data Structure?
• When we talk about the fastest ways to retrieve values from a data structure, hash
tables generally comes to our mind. Though very efficient in nature but still very less
talked about as when compared to hash tables, trie's are much more efficient than hash
tables and also they possess several advantages over the same. Mainly:
• There won't be any collisions hence making the worst performance better than a hash
table that is not implemented properly.
• No need for hash functions.
• Lookup time for a string in trie is O(k) where k = length of the word.
• It can take even less than O(k) time when the word is not there in a trie.
Trie Applications: When and Why Use Tries
• Logarithmic performance and linear memory isn’t bad. But there are a
few more characteristics of our application domain that can lead us to
better performance:
• We can safely assume that all words are lowercase.
• We accept only a-z letters—no punctuation, no hyphens, no accents, etc.
• The dictionary contains many inflected forms: plurals, conjugated verbs,
composite words (e.g., house –> housekeeper). Therefore, many words
share the same stem.
• Words have a limited length. For example, if we are working on a 4x4
board, all words longer than 16 chars can be discarded.
Applications of Trie
1. Spell Checker
• Spell checking is a three-step process.
• First, look for that word in a dictionary, generate possible suggestions,
and then sort the suggestion words with the desired word at the top.
• Trie is used to store the word in dictionaries. The spell checker can
easily be applied in the most efficient way by searching for words on a
data structure.
• Using trie not only makes it easy to see the word in the dictionary, but
it is also simple to build an algorithm to include a collection of relevant
words or suggestions.
2. Auto-complete
• Auto-complete functionality is widely used on text editors,
mobile applications, and the Internet. It provides a simple
way to find an alternative word to complete the word for
the following reasons.
• It provides an alphabetical filter of entries by the key of the
node.
• We trace pointers only to get the node that represents the
string entered by the user.
• As soon as you start typing, it tries to complete your input.
3. Browser history
• It is also used to complete the URL in the browser. The
browser keeps a history of the URLs of the websites you've
visited.
Advantages of Trie:
• It can be insert faster and search the string than hash
tables and binary search trees.
• It provides an alphabetical filter of entries by the key
of the node.
Disadvantages of Trie
• It requires more memory to store the strings.
• It is slower than the hash table.( creation )
The diagram below depicts a trie representation for the bell, bear, bore, bat, ball,
stop, stock, and stack.
Types of Tries
some of the types of tries includes:
1.Standard tries
2. compressed tries
3. Suffix tries
etc ………
Standard and compressed tries example
bell, best, bill, bin, boy, stone, stop
Creating a Trie
Exercise: bell, bear, bore, bat, ball, stop, stock, and stack.
MULTIDIMESIONAL TREES- K-D TREES
In computer science, a k-d tree (short for k-dimensional tree) is
a space-partitioning data structure for organizing points in a k-
dimensional space.
 k-d trees are a useful data structure for several applications,
such as searches involving a multidimensional search key
(e.g. range searches and nearest neighbor searches) and
creating point clouds.
 k-d trees are a special case of binary space partitioning trees.
k-d tree
• The k-d tree is a binary tree in which every node is a k-dimensional point.
• Every non-leaf node can be thought of as implicitly generating a
splitting hyperplane that divides the space into two parts, known as half-spaces.
• Points to the left of this hyperplane are represented by the left subtree of that
node and points to the right of the hyperplane are represented by the right
subtree.
• The hyperplane direction is chosen in the following way: every node in the tree is
associated with one of the k dimensions, with the hyperplane perpendicular to
that dimension's axis.
• So, for example, if for a particular split the "x" axis is chosen, all points in the
subtree with a smaller "x" value than the node will appear in the left subtree and
all points with a larger "x" value will be in the right subtree.
• In such a case, the hyperplane would be set by the x value of the point, and
its normal would be the unit x-axis.
K-D TREE

k-d tree decomposition for the point set


(2,3), (5,4), (9,6), (4,7), (8,1), (7,2).
2-D TREE
• For the sake of simplicity, let us understand a 2-D Tree with an example.
• The root would have an x-aligned plane, the root’s children would both have y-aligned
planes, the root’s grandchildren would all have x-aligned planes, and the root’s great-
grandchildren would all have y-aligned planes and so on.
• Generalization:
Let us number the planes as 0, 1, 2, …(K – 1). From the above example, it is quite clear that
a point (node) at depth D will have A aligned plane where A is calculated as:
• A = D mod K
• How to determine if a point will lie in the left subtree or in right subtree?
• If the root node is aligned in plane A, then the left subtree will contain all points whose
coordinates in that plane are smaller than that of root node. Similarly, the right subtree will
contain all points whose coordinates in that plane are greater-equal to that of root node.
Creation of a 2-D Tree:
Consider following points in a 2-D plane:
(3, 6), (17, 15), (13, 15), (6, 12), (9, 1), (2, 7), (10, 19)
1.Insert (3, 6): Since tree is empty, make it the root node.
2.Insert (17, 15): Compare it with root node point. Since root node is X-aligned, the X-coordinate value
will be compared to determine if it lies in the left subtree or in the right subtree. This point will be Y-
aligned.
3.Insert (13, 15): X-value of this point is greater than X-value of point in root node. So, this will lie in
the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17, 15)
(Why?). Since, they are equal, this point will lie in the right subtree of (17, 15). This point will be X-
aligned.
4.Insert (6, 12): X-value of this point is greater than X-value of point in root node. So, this will lie in
the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17, 15)
(Why?). Since, 12 < 15, this point will lie in the left subtree of (17, 15). This point will be X-aligned.
5.Insert (9, 1):Similarly, this point will lie in the right of (6, 12).
6.Insert (2, 7):Similarly, this point will lie in the left of (3, 6).
7.Insert (10, 19): Similarly, this point will lie in the left of (13, 15).
Creation of a 2-D Tree:
(3, 6), (17, 15), (13, 15), (6, 12), (9, 1), (2, 7), (10, 19)
• How is space partitioned?
All 7 points will be plotted in the X-Y plane as follows:
1. Point (3, 6) will divide the space into 2. Point (2, 7) will divide the space to the left of line X = 3 into two parts horizontally.
Draw line Y = 7 to the left of line X = 3.

two parts: Draw line X = 3


4.Point (6, 12) will divide the space below line Y =
3. Point(17, 15) will divide the space to the right of line 15 and to the right of line X = 3 into two parts.
X = 3 into two parts
Draw line X = 6 to the right of line X = 3 and below
horizontally. line Y = 15.
Draw line Y = 15 to the right of line X = 3.

      
5.Point (13, 15) will divide the space below line Y = 15 and
6.Point (9, 1) will divide the space
to the right of line between lines X = 3, X = 6 and Y = 15
X = 6 into two parts.
Draw line X = 13 to the right of line X = 6 and below line Y into two parts.
= 15. Draw line Y = 1 between lines X = 3 and
X = 6.

      
7. Point (10, 19) will divide the space to the right of line X = 3 and above line Y = 15 into two parts.
Draw line Y = 19 to the right of line X = 3 and above line Y = 15.

You might also like