Java tree, for general purpose. Functional approach.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore Initial Jun 29, 2013 Update Dec 16, 2013


Java tree, for general purpose. Functional approach.

Beware, it is not a binary tree


  • Simple
  • Iterable
  • Modification: mapping, filtering, sorting
  • Pretty-printing
  • Different traversals - depth-first, breadth-first




  • Key builder
  • Path builder


1. Build a tree from id - parentId mapping (adjacency list)

Assume you have objects like:

public class Entity {
    public final int id;
    public final int parentId;

And assume that id == 0 means no parent (e.g. root). KeyTreeBuilder will be the right choice.

  1. Create a Funnel. Funnel is an object which knows how to extract id and parentId from your object.

    KeyTreeBuilder.Funnel<Integer, Entity> funnel = new KeyTreeBuilder.Funnel<Integer, Entity>() {
        public Integer getKey(Entity node) {
        public Integer getParentKey(Entity node) {
            //so lets take 0 as no parent
            if (node.parentId == 0) return null;
            return node.parentId;
  2. Create a KeyTreeBuilder. Builder is stateless so you can create only one.

    KeyTreeBuilder<Integer, Entity> builder = new KeyTreeBuilder<Integer, Entity>(funnel);
  3. Build a tree!

    Tree<Entity> tree = builder.buildTree(entities);

If you want a forest, or an Integer -> Tree<Entity> mapping, you can use .build method and get more results. Full example available here

2. Building url tree

Assume you have a batch of url and you want to build a tree structure. It's simple, really. All we need is the PathTreeBuilder.

  1. Create a Funnel which knows how to extract a path from your object. In case of URL it will be a split by / (simplistically).

    PathTreeBuilder.Funnel<String, String> urlFunnel = new PathTreeBuilder.Funnel<String, String>() {
        public List<String> getPath(String value) {
            return Lists.newArrayList(value.split("/"));
  2. Create a PathTreeBuilder. Also stateless.

    PathTreeBuilder<String, String> builder = new PathTreeBuilder<String, String>(urlFunnel);
  3. Build a tree! Actually, not a tree - a forest. There are no guarantees of root uniqueness.

    List<Tree<String>> build =;

Full example available here