Trie Operations Complexity

Now that we’ve seen the basic operations on how to work with a TRIE, we shall now see the space and time complexities in order to get a real feel of how good a TRIE data structure is. Lets take the two important operations INSERT and SEARCH to measure the complexity. INSERT operation first. Lets always take into account the worst case timing first and later convince ourselves of the practical timings. [Read More]

Trie : Searching in Trie

In the previous section we saw how to insert string into TRIE. In this section, we shall see how to perform a search in the TRIE when a string or key is passed. Consider the following TRIE as usual. The search alogirthm involves the following steps For each character in the string, see if there is a child node with that character as the content. If that character does not exist, return false If that character exist, repeat step 1. [Read More]

Trie : Inserting in Trie

In this section we shall see how the insert() method on the TRIE data structure works. We shall take a specific case and analyze it with pictorial representation. Before we begin, assume we already have an existing TRIE as shown below. Lets see the steps on how to insert a word “bate”. Any insertion would ideally be following the below algorithm. If the input string length is zero, then set the marker for the root node to be true. [Read More]

Implementing the TRIE ADT in java

We already saw the Node structure of the TRIE ADT had a content (char), a marker (boolean) and collection of child nodes (Collection of Node). It now has one more method called as subNode(char). This method takes a character as argument would return the child node of that character type should that be present. Node.java package com.vaani.ds.trie; import java.util.Collection; import java.util.LinkedList; /\*\* \* @author kc \*/ public class Node { char content; boolean marker; Collection<Node> child; public Node(char c){ child = new LinkedList<Node>(); marker = false; content = c; } public Node subNode(char c){ if(child! [Read More]

Trie ADT

TRIE is an interesting data-structure used mainly for manipulating with Words in a language. This word is got from the word retrieve. TRIE (pronounced as ‘try’) has a wide variety of applications in Spell checking Data compression Computational biology Routing table for IP addresses Storing/Querying XML documents etc., We shall see how to construct a basic TRIE data structure in Java. The main abstract methods of the TRIE ADT are, [Read More]

Scrabble Algorithms

If you are playing scrabble and you have n letters, what are all the possible words that you can make from these n letters? It looks as if it would involve permutations and combinations both of the letters or a union of permutations and combinations for n letters. Surprisingly, it is much simpler. Below are the possible words for letters ‘abc’. I have highlighted the patterns in them. a ab [Read More]

Trie examples

I mentioned Tries before here and here. I thought of two more popular examples. Google Suggestions (now in production) and Gmail autocomplete logic would be implemented as a Trie (prefix tree). I could think of two ways this could be done using a Trie. The first way is to DFS from the current prefix or node to get to all the possible words below. This approach is time-intensive due to its recursive nature and the user probably would not benefit when the Trie is large. [Read More]

Tries

Trie (pronounced as try even though it stands for tree and re_trie_val) is a prefix tree data structure. You can read more on it on topcoder and wiki. Trie mostly has a root node as an empty string ("") and each node has 26 child pointers letters (A to Z) at least. As we add words to it, the trie grows. Here is an example of how a trie looks (from www. [Read More]