Class TrieNode<V>

  • Type Parameters:
    V - the type of the value held by each node

    public final class TrieNode<V>
    extends java.lang.Object
    A node in a trie.
    • Constructor Summary

      Constructors 
      Constructor Description
      TrieNode()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Recursively removes all children.
      TrieNode<V> deleteIf​(java.lang.String path, java.util.function.Function<TrieNode<V>,​java.lang.Boolean> predicate)
      Deletes the path from the Trie if the given predicate is true.
      java.util.Iterator<TrieNode<V>> getCommonRoots()  
      java.util.stream.Stream<TrieNode<V>> getLeafChildren​(java.lang.String path)
      Get the terminal children of path (including path).
      V getValue()  
      boolean hasTerminal​(java.lang.String path, boolean includeChildren)
      Checks whether the path has terminal nodes as parents or children.
      TrieNode<V> insert​(java.lang.String path)
      Inserts a path into the trie.
      java.util.List<TrieNode<V>> search​(java.lang.String path)
      Traverses the trie along the path components until the traversal cannot proceed any more.
      java.util.Optional<TrieNode<V>> searchExact​(java.lang.String path)
      Find terminal component of the full path if one exists.
      void setValue​(V value)
      Set the value associated with this node.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TrieNode

        public TrieNode()
    • Method Detail

      • setValue

        public void setValue​(V value)
        Set the value associated with this node.
        Parameters:
        value - the value
      • getValue

        public V getValue()
        Returns:
        the value associated with this node
      • insert

        public TrieNode<V> insert​(java.lang.String path)
        Inserts a path into the trie. Each path component forms a node in the trie, root path "/" will correspond to the root of the trie.
        Parameters:
        path - a path with components separated by "/"
        Returns:
        the last inserted trie node or the last traversed trie node if no node is inserted
      • search

        public java.util.List<TrieNode<V>> search​(java.lang.String path)
        Traverses the trie along the path components until the traversal cannot proceed any more.
        Parameters:
        path - the target path
        Returns:
        the terminal nodes sorted by the time they are visited
      • searchExact

        public java.util.Optional<TrieNode<V>> searchExact​(java.lang.String path)
        Find terminal component of the full path if one exists.
        Parameters:
        path - the path
        Returns:
        the terminal component
      • hasTerminal

        public boolean hasTerminal​(java.lang.String path,
                                   boolean includeChildren)
        Checks whether the path has terminal nodes as parents or children.
        Parameters:
        path - the target path
        includeChildren - whether the check should succeed if the path has children terminal nodes
        Returns:
        the terminal nodes sorted by the time they are visited
      • deleteIf

        public TrieNode<V> deleteIf​(java.lang.String path,
                                    java.util.function.Function<TrieNode<V>,​java.lang.Boolean> predicate)
        Deletes the path from the Trie if the given predicate is true.
        Parameters:
        path - the target path
        predicate - a predicate to decide whether the node should be deleted or not
        Returns:
        the removed terminal node, or null if the node is not found or not terminal
      • getCommonRoots

        public java.util.Iterator<TrieNode<V>> getCommonRoots()
        Returns:
        the iterator of TrieNode that are terminals and have no terminal ancestors
      • getLeafChildren

        public java.util.stream.Stream<TrieNode<V>> getLeafChildren​(java.lang.String path)
        Get the terminal children of path (including path).
        Parameters:
        path - the path
        Returns:
        the terminal children
      • clear

        public void clear()
        Recursively removes all children.