Trie a learning!!

Trie

What is a Trie 

A trie is a data structure used to store multiple words and makes it easy to iterate and find a word inside it.
It can be used instead of storing Strings inside an Arraylist and then using contains function. It is helpful if we want to store multiple words and find them easily.

How does it work

It is a basic tree strucure having multiple child nodes, each node represents a character and have child nodes which also represent characters.

like if we want to store a single word "and" the tree will be like

a->n->d   but if we want to add a word ant inside the trie then it will be like
a->n->d
      \->t    
the node n will have multiple child nodes. Each node can also represent a flag end_of_word which denotes if the current node can be considered end of a word or not. If it is true then all the characters from root to this node can form a word.

Implementation


Single class Implementation

Comments

Popular posts from this blog

Lowest common ancestor in a binary Tree

Interview

30. Substring with Concatenation of All Words