Trie

Photo by Sabrina Mazzeo on Unsplash
Photo by Sabrina Mazzeo on Unsplash
Trie is a k-ary search tree. It can store a large amount of strings and provide efficient string search.

Trie is a k-ary search tree. It can store a large amount of strings and provide efficient string search.

Trie & TrieNode

Trie generally has two data structures, one is Trie and the other is TrieNodeTrieNode is used to construct the entire tree, and Trie will only be one. TrieNode.terminates means that the path from root to this node is a word. In addition, TrieNode.character is not necessary, because the character represented by the node can be known from the children of the parent. Root does not represent any characters.

public class Trie {
    private final TrieNode root = new TrieNode();

    public Trie(ArrayList<String> list) {
        for (String word : list) {
            root.addWord(word);
        }
    }

    public Trie(String[] list) {
        for (String word : list) {
            root.addWord(word);
        }
    }

    public boolean contains(String prefix, boolean exact) {
        TrieNode lastNode = root;
        for (int i = 0; i < prefix.length(); i++) {
            lastNode = lastNode.getChild(prefix.charAt(i));
            if (lastNode == null) {
                return false;
            }
        }
        return !exact || lastNode.terminates();
    }

    public boolean contains(String prefix) {
        return contains(prefix, false);
    }

    public void addWord(String word) {
        root.addWord(word);
    }
}
public class TrieNode {
    private final HashMap<Character, TrieNode> children = new HashMap<>();
    private boolean terminates = false;
    private char character;

    public TrieNode() {
    }

    public TrieNode(char character) {
        this.character = character;
    }

    public char getChar() {
        return character;
    }

    public void addWord(String word) {
        if (word == null || word.isEmpty()) {
            return;
        }

        char firstChar = word.charAt(0);
        TrieNode child = getChild(firstChar);
        if (child == null) {
            child = new TrieNode(firstChar);
            children.put(firstChar, child);
        }

        if (word.length() > 1) {
            child.addWord(word.substring(1));
        } else {
            child.setTerminates(true);
        }
    }

    public TrieNode getChild(char c) {
        return children.get(c);
    }

    public boolean terminates() {
        return terminates;
    }

    public void setTerminates(boolean terminates) {
        this.terminates = terminates;
    }
}

Complexity

The time complexity required to build a dictionary is O(\sum(w_{i})), where w_{i} is the length of word[i].

The time complexity required to search and insert a word is O(w_{i}).

The space complexity is the number of all nodes, so it is O(\sum(w_{i})).

Example

The example below shows what happens inside the Trie when a word is inserted. Green means the node’s terminates is set to true.

Reference

  • Gayle Laakmann McDowell, Cracking the Coding Interview, 6th Edition.
  • Trie, Wikipedia.
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Photo by Pedro Lastra on Unsplash
Read More

Breadth First Search (BFS)

Breadth-first search (BFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores all adjacent vertices of that vertex. Next, explores the adjacent vertices of these adjacent vertices in sequence. In this way, it explores level by level until the result is found, or all vertices are explored.
Read More
Photo by Léonard Cotte on Unsplash
Read More

Depth First Search (DFS)

Depth-first search (DFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores an adjacent vertex of that vertex. Next, it explores an adjacent vertex of this vertex. In this way, the exploration continues until a certain vertex has no adjacent vertices, it will return to the previous vertex, and explores the next adjacent vertex of the vertex. It will continue to explore like this until it finds a result, or explores all vertices.
Read More
Photo by Anthony DELANOIX on Unsplash
Read More

Union Find

Union Find data structure is also known as disjoint set data structure. It is a data structure which stores a collection of disjoint sets. It provides two operations, one for merging two sets and one for finding the set with a given element.
Read More