https://github.com/dirkluijk/tree-api
A small Tree library for Java
https://github.com/dirkluijk/tree-api
Last synced: over 1 year ago
JSON representation
A small Tree library for Java
- Host: GitHub
- URL: https://github.com/dirkluijk/tree-api
- Owner: dirkluijk
- License: mit
- Created: 2015-04-24T18:27:59.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-18T09:52:03.000Z (about 11 years ago)
- Last Synced: 2025-02-09T15:11:35.807Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 260 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tree API
A small Java library for creating tree structures.
[](https://travis-ci.org/dirkluijk/tree-api)
[](https://coveralls.io/r/dirkluijk/tree-api?branch=master)
## API Documentation
Find the JavaDoc documentation at http://dirkluijk.github.io/tree-api/.
## Features
* A generic ``Node`` interface with a ``AbstractNode`` and a ``NameNode`` sample implementation
* A ``Tree`` helper to store the nodes in and search for descendants
* A ``TreeIterator`` to iterate over a tree
* A ``NodeComparator`` to compare nodes and maintain the tree-based ordering inside a ``Tree``
## How to use
Creating your custom node:
```java
class Employee implements Node {
private Employee manager;
private String name;
private int salary;
public Employee(String name) {
this(name, null);
}
public Employee(String name, Employee manager) {
this.name = name;
this.manager = manager;
}
@Override
public Employee getParent() {
return manager;
}
@Override
public void setParent(Employee parent) {
this.manager = parent;
}
@Override
public boolean isLeaf() {
return false;
}
@Override
public String toString() {
return this.name;
}
}
```
Creating nodes:
```java
Employee dave = new Employee("Dave");
Employee john = new Employee("John", dave);
Employee charles = new Employee("Charles", dave);
```
Storing nodes in a tree, e.g. using the included ``NodeTree``:
```java
Tree employees = new NodeTree<>();
employees.add(john);
employees.add(dave);
employees.add(charles);
```
The tree will be ordered automatically, using the parent-child structure. Siblings are ordered alphabetically by default (based on the ``toString`` implementation):
* Dave
* Charles
* John
The tree provides some interesting features:
```java
// will return a new tree containing only john and charles
employees.descendantsOf(dave);
employees.descendantsCount(dave); // 2
employees.descendantsCount(charles); // 0
```
There is also a special ``TreeIterator`` available:
```java
TreeIterator it = employees.iterator();
while(it.hasNext() {
Employee current = it.next();
it.level(); // the level of the current node
it.path(); // the path, e.g. "/dave/charles"
}
```