# Trie Data Structure

In Computer Science, a trie is a data structure that is also known as a digital search tree or a prefix tree. It can be used for fast retrieval on large data sets such as looking up words in a dictionary. The term trie was invented from the phrase ‘Information Retrieval’ by Fredkin(1960).

As a prefix tree, the trie is an ordered tree used to represent a set of words (or strings) over a finite alphabet set. It allows words with common prefixes to use the same prefix data and stores only the end of the words to indicate different words. Each node in the trie is labeled with one character except the root node which is an empty node. The maximum number of children that a particular node in the trie can have is equal to the number of characters in the alphabet set used for the trie. The children of a node are alphabetically ordered and sibling nodes must represent distinct characters. The node which contains the last character of a word is called a leaf node.  The path from the root to a leaf node represents a word in the trie.

Consider the set C = { ‘A’, ’C’, ’G’, ’T’}, then a trie for this set can potentially have 4 children for each node; one corresponding to each letter. The following figure shows a trie for 6 different strings composed from C. The strings are: ‘AAAA’, ‘ACGT’, ‘CGA’, ‘GTA’, TA’, and ‘TTTT’. Note that ‘A’, ‘AA’, ‘AAA’, ‘AC’, ‘CG’, ‘ACG’, ‘C’, ‘G’, ‘GT’, ‘T’, ‘TT’, ‘TTT’ are all common prefixes. Also, the height of the trie is the same as the longest word in the trie which is 4 in this case. A standard trie data structure can be developed from a single node class with 3 attributes:

1. A character ch to store the letter representing the node
2. An array of nodes children to represent the children of the node, and
3. A Boolean leafNode to mark the end of the word.

To implement a trie for the previous example, we start with the root node of the trie. Therefore, we instantiate a node object root  by using the following Node constructor:

We also need to determine the position for each character in the array so that they are alphabetically ordered. I.e.

Two main functions are needed with a trie data structure: Search and Insert. The following pseudo codes provide the algorithms for both functions.

Void INSERT (String word)

1. Make current node points to the root of the trie.
2. For each character c in word:
1. Get the pre-determined position for c in the children array
2. If the children node of the current node has nothing at that position, insert c
3. Make current node points to its child node whose index is the same as position.
3. Set leafNode to true to indicate insertion of word into the trie.

Boolean SEARCH (String word)

1. Make current node points to the root of the trie.
2. For each character c in word:
1. If there is nothing in current node, return false.
2. Get the pre-determined position for c in the children array
3. Make current node points to its child node whose index is the same as position.
3. If current node is not empty and leafNode is false, return false.
4. Return true to indicate that word was found in the trie.

The trie data structure is often overlooked by developers because of the time it takes to build. For a text of length n, inserting words into the trie requires (n2) time overall. Space needed for a trie is also quadratic which can be another drawback. On the other hand, searching a trie is fast. In the worst case it is  O(m)where m is the length of the word to search for.

Categories: Uncategorized
{"ft_lang_label":"__label__en","ft_lang_prob":0.8934722,"math_prob":0.9294176,"size":3786,"snap":"2020-45-2020-50","text_gpt3_token_len":913,"char_repetition_ratio":0.14463247,"word_repetition_ratio":0.107801415,"special_character_ratio":0.25118858,"punctuation_ratio":0.11010363,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9582733,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-26T15:48:58Z\",\"WARC-Record-ID\":\"<urn:uuid:1a570e52-93d0-4f45-86d4-3bb2703f86e9>\",\"Content-Length\":\"102800\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fa5bf528-3827-411c-8736-196b7b2e636a>\",\"WARC-Concurrent-To\":\"<urn:uuid:94557259-585b-4d50-8782-aadbf515a27e>\",\"WARC-IP-Address\":\"130.209.225.32\",\"WARC-Target-URI\":\"https://bioinformatics.cvr.ac.uk/trie-data-structure/\",\"WARC-Payload-Digest\":\"sha1:GV5G4F5II3KYOHDEAAONQICSRA732DPV\",\"WARC-Block-Digest\":\"sha1:6G7M6VXFEENZ4L7WOBQFYTQQLIZ5LRWB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107891428.74_warc_CC-MAIN-20201026145305-20201026175305-00401.warc.gz\"}"}