Class TreeUtils

java.lang.Object
org.aksw.commons.collections.trees.TreeUtils

public class TreeUtils extends Object
  • Constructor Details

    • TreeUtils

      public TreeUtils()
  • Method Details

    • streamAncestors

      public static <T> Stream<T> streamAncestors(T path, Function<? super T, ? extends T> getParent)
      Return all ancestors of the given item, including itself. The last element is null.
    • getRoot

      public static <T> T getRoot(T item, Function<? super T, ? extends T> predecessor)
    • findSuccessor

      public static <T> T findSuccessor(T item, Function<? super T, ? extends T> successor, BiPredicate<? super T, ? super T> predicate)
      Parameters:
      item -
      successor -
      predicate - Receives the current item and its successor
      Returns:
    • findAncestor

      public static <T> T findAncestor(T node, Function<? super T, ? extends T> getParent, Predicate<? super T> predicate)
    • findRoot

      public static <T> T findRoot(T start, Function<? super T, ? extends T> getParent)
    • groupByParent

      public static <T> com.google.common.collect.Multimap<T,T> groupByParent(Tree<T> tree, Collection<T> nodes, com.google.common.collect.Multimap<T,T> result)
    • propagateBottomUpLabel

      public static <T> Set<T> propagateBottomUpLabel(Tree<T> tree, Predicate<T> predicate)
      Given a predicate, return the minimum set of nodes, for which all nodes in their subtree satisfy the predicate. The algo starts with the set X of leaf nodes satisfying the predicate, and moves upwards. If all children of a parent satisfy the predicate, the children are removed from X and the parent is added instead. Note: Uses IdentityHashSet
      Parameters:
      tree -
      predicate -
      Returns:
    • subTree

      public static <T> Tree<T> subTree(Tree<T> tree, T newRoot)
    • nodeCount

      public static <T> long nodeCount(Tree<T> tree)
    • depth

      public static <T> long depth(Tree<T> tree)
    • depth

      public static <T> long depth(Tree<T> tree, T node)
    • childIndexOf

      public static <T> int childIndexOf(TreeOps2<T> ops, T node)
    • replace

      public static <T> Tree<T> replace(Tree<T> tree, T node, T replacement)
    • indexOf

      public static <T> int indexOf(List<T> list, T find, BiPredicate<? super T, ? super T> isEquiv)
    • replaceNode

      public static <T> T replaceNode(Tree<T> tree, T node, T replacement, BiPredicate<? super T, ? super T> isEquiv)
    • substitute

      public static <T> T substitute(T node, boolean descendIntoSubst, TreeOps2<T> ops, Function<? super T, ? extends T> transformFn)
    • substitute

      public static <T> T substitute(T node, boolean descendIntoSubst, Function<? super T, ? extends Collection<? extends T>> getChildrenFn, Function<? super T, ? extends T> transformFn, BiFunction<T,List<T>,T> copyFn)
    • findAncestor

      public static <T> T findAncestor(Tree<T> tree, T node, Predicate<T> predicate)
      Find the first ancestor for which the predicate evaluates to true
      Parameters:
      tree -
      node -
      predicate - TODO Make use of find successor()
      Returns:
    • inOrderSearch

      public static <T> Stream<T> inOrderSearch(T node, Function<T, ? extends Iterable<T>> parentToChildren)
    • inOrderSearch

      public static <T,V> Stream<Map.Entry<T,V>> inOrderSearch(T node, Function<T, ? extends Iterable<T>> parentToChildren, Function<? super T, V> nodeToValue, BiPredicate<T,V> doDescend)
      In-order-search starting from the given node and descending into the tree. Each node may be mapped to a value. A predicate determines whether to stop descending further into a sub-tree. Useful for extracting patterns from a tree.
      Parameters:
      node -
      parentToChildren -
      nodeToValue -
      doDescend -
      Returns:
    • innerNodesPerLevel

      public static <T> List<List<T>> innerNodesPerLevel(Tree<T> tree)
      For each level, yield the inner nodes The root node will always be part of the list, even if it does not have children
    • nodesPerLevel

      public static <T> List<List<T>> nodesPerLevel(Tree<T> tree)
      Returns the set of nodes in each level of the tree The set containing the root will be the first item in the list
      Parameters:
      tree -
      Returns:
    • leafStream

      public static <T> Stream<T> leafStream(Tree<T> tree)
    • getLeafs

      public static <T> List<T> getLeafs(Tree<T> tree)
    • getLeafs

      public static <T> void getLeafs(Collection<T> result, Tree<T> tree, T node)
    • getParentsOf

      public static <T> Set<T> getParentsOf(Tree<T> tree, Iterable<T> children)
      Get the set of immediate parents for a given set of children
      Parameters:
      tree -
      children -
      Returns:
    • parentMap

      public static <T> Map<T,T> parentMap(T root, Function<T,List<T>> parentToChildren)
      Traverse an op structure and create a map from each subOp to its immediate parent NOTE It must be ensured that common sub expressions are different objects, since we are using an identity hash map for mapping children to parents
      Parameters:
      op -
      Returns:
    • parentMap

      public static <T> void parentMap(Map<T,T> result, T parent, Function<T,List<T>> parentToChildren)
    • remapSubTreesToLeafs

      public static <T> Tree<T> remapSubTreesToLeafs(Tree<T> tree, Function<? super T, ? extends T> remapFn)
      Create a new tree object which has certain nodes remapped with *leaf* nodes
      Parameters:
      tree -
      remapFn -
      Returns:
    • removeUnaryNodes

      public static <T> Tree<T> removeUnaryNodes(Tree<T> tree)
    • removeUnaryNodes

      public static <T> T removeUnaryNodes(Tree<T> tree, T node, com.google.common.collect.ListMultimap<T,T> parentToChildren)
    • clusterNodesByFirstMultiaryAncestor

      public static <T> Map<T, com.google.common.collect.Multimap<T,T>> clusterNodesByFirstMultiaryAncestor(Tree<T> tree, com.google.common.collect.Multimap<T,T> mapping)
      Input: A mapping from cache nodes to candidate query nodes represented as a Multimapinvalid input: '<'T, T>. Output: The mapping partitioned by each node's first multiary ancestor. Output could also be: Multimapinvalid input: '<'Op, Op> - fmaToNodesCache For every cacheFma, map to the corresponding queryFmas - and for each of these mappings yield the candidate node mappings of the children Multimapinvalid input: '<'OpCacheFma, Mapinvalid input: '<'OpQueryFma, Multimapinvalid input: '<'OpCache, OpQuery>>> Q: What if cache nodes do not have a fma? A: In this case the fma would be null, which means that there can only be a single cache node which would be grouped with a null fma. In the query, we can then check whether we are pairing a union with another union or null. We always map from cache to query. Mapinvalid input: '<'CacheFma, QueryFma> So the challenge is now again how to represent all the facts and how to perform the permutations / combinations...
      Parameters:
      tree -
      cacheTree -
      cacheToQueryCands -
      Returns:
    • getFirstMultiaryAncestor

      public static <T> T getFirstMultiaryAncestor(Tree<T> tree, T node)
    • firstMultiaryAncestor

      public static <T> T firstMultiaryAncestor(Tree<T> tree, T node)
      Return a node's first ancestor having an arity > 1 null if there is none.
      Parameters:
      tree -
      node -
      Returns:
    • getUnaryAncestors

      public static <X> List<X> getUnaryAncestors(X x, Tree<X> tree, Tree<X> multiaryTree)
    • deriveParentMapping

      public static <A,B> com.google.common.collect.Multimap<A,B> deriveParentMapping(Tree<A> aTree, Tree<B> bTree, com.google.common.collect.Multimap<A,B> childMapping)
      Given a mapping of child nodes, determine which parents may be mapped to each other. For any nodes mapped in 'childMapping', their parents may be mapped as well.
      Parameters:
      aTree -
      bTree -
      childMapping -
      Returns: