Coder Social home page Coder Social logo

gtree's Introduction

GTree

Java tree, for general purpose. Functional approach.

Beware, it is not a binary tree

Features:

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

Maven

<dependency>
    <groupId>org.jkee.gtree</groupId>
    <artifactId>gtree</artifactId>
    <version>0.64</version>
</dependency>

Builders

  • Key builder
  • Path builder

Examples

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>() {
        @Override
        public Integer getKey(Entity node) {
            return node.id;
        }
    
        @Override
        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>() {
        @Override
        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 = builder.build(urls);

Full example available here

gtree's People

Contributors

jkee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gtree's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.