All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.abego.treelayout.package-info Maven / Gradle / Ivy

The newest version!
/*
 * [The "BSD license"]
 * Copyright (c) 2011, abego Software GmbH, Germany (http://www.abego.org)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the abego Software GmbH nor the names of its 
 *    contributors may be used to endorse or promote products derived from this 
 *    software without specific prior written permission.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 Efficiently create compact, highly customizable tree layouts.


 

Introduction

The TreeLayout creates tree layouts for arbitrary trees. It is not restricted to a specific output or format, but can be used for any kind of two dimensional diagram. Examples are Swing based components, SVG files, and many more. This is possible because TreeLayout separates the layout of a tree from the actual rendering.

To use the TreeLayout you mainly need to supply an instance of the {@link org.abego.treelayout.TreeLayout TreeLayout} class with the nodes of the tree (including "children" links), together with the "size" of each node. In addition you can configure the layout by specifying parameters like "gap between levels" etc..

Based on this information TreeLayout creates a compact, nice looking layout. The layout has the following properties [2]:

  1. The layout displays the hierarchical structure of the tree, i.e. the y-coordinate of a node is given by its level. (*)
  2. The edges do not cross each other and nodes on the same level have a minimal horizontal distance.
  3. The drawing of a subtree does not depend on its position in the tree, i.e. isomorphic subtrees are drawn identically up to translation.
  4. The order of the children of a node is displayed in the drawing.
  5. The algorithm works symmetrically, i.e. the drawing of the reflection of a tree is the reflected drawing of the original tree.
(*) When the root is at the left or right (see "Root Location") the x-coordinate of a node is given by its level

Here an example tree layout:

Simple Tree with 6 nodes

Usage

To use the TreeLayout you will create a {@link org.abego.treelayout.TreeLayout TreeLayout} instance with:
  • a tree, accessible through the {@link org.abego.treelayout.TreeForTreeLayout TreeForTreeLayout} interface,
  • a {@link org.abego.treelayout.NodeExtentProvider NodeExtentProvider}, and
  • a {@link org.abego.treelayout.Configuration Configuration}.
Using these objects the TreeLayout will then calculate the layout and provide the result through the method {@link org.abego.treelayout.TreeLayout#getNodeBounds() getNodeBounds}.

TreeForTreeLayout

The TreeLayout works on any kind of tree and uses the {@link org.abego.treelayout.TreeForTreeLayout TreeForTreeLayout} interface to access such a tree.

To use the TreeLayout you therefore need to provide it with a TreeForTreeLayout implementation. In most situations you will not need to deal with all details of that interface, but create an implementation by extending {@link org.abego.treelayout.util.AbstractTreeForTreeLayout AbstractTreeForTreeLayout}, or even use the class {@link org.abego.treelayout.util.DefaultTreeForTreeLayout DefaultTreeForTreeLayout} directly.

Example: Extending AbstractTreeForTreeLayout

Assume you have a tree consisting of nodes of type StringTreeNode:

UML diagram for StringTreeNode

As StringTreeNode provides the children in a list and you can get the parent for each node you can extend {@link org.abego.treelayout.util.AbstractTreeForTreeLayout AbstractTreeForTreeLayout} to create your TreeForTreeLayout implementation. You only need to implement two methods and the constructor:

 public class StringTreeAsTreeForTreeLayout extends
     AbstractTreeForTreeLayout<StringTreeNode> {

     public StringTreeAsTreeForTreeLayout(StringTreeNode root) {
          super(root);
     }

     public StringTreeNode getParent(StringTreeNode node) {
          return node.getParent();
     }

     public List<StringTreeNode> getChildrenList(StringTreeNode parentNode) {
          return parentNode.getChildren();
     }
 } 
 
(Make sure to check out the performance constraints of {@link org.abego.treelayout.util.AbstractTreeForTreeLayout#getChildrenList(Object) getChildrenList} and {@link org.abego.treelayout.util.AbstractTreeForTreeLayout#getParent(Object) getParent}.)

Example: Using the DefaultTreeForTreeLayout

Assume you want to create a tree with TextInBox items as nodes:

UML diagram for TextInBox

As you have no own tree implementation yet you may as well use {@link org.abego.treelayout.util.DefaultTreeForTreeLayout DefaultTreeForTreeLayout} to create the tree:

 TextInBox root = new TextInBox("root", 40, 20);
 TextInBox n1 = new TextInBox("n1", 30, 20);
 TextInBox n1_1 = new TextInBox("n1.1\n(first node)", 80, 36);
 TextInBox n1_2 = new TextInBox("n1.2", 40, 20);
 TextInBox n1_3 = new TextInBox("n1.3\n(last node)", 80, 36);
 TextInBox n2 = new TextInBox("n2", 30, 20);
 TextInBox n2_1 = new TextInBox("n2", 30, 20);

 DefaultTreeForTreeLayout<TextInBox> tree = 
         new DefaultTreeForTreeLayout<TextInBox>(root);
 tree.addChild(root, n1);
 tree.addChild(n1, n1_1);
 tree.addChild(n1, n1_2);
 tree.addChild(n1, n1_3);
 tree.addChild(root, n2);
 tree.addChild(n2, n2_1);
 
This will create a tree like this:

Tree created with SVG

NodeExtentProvider

TreeLayout also needs to know the extent (width and height) of each node in the tree. This information is provided through the {@link org.abego.treelayout.NodeExtentProvider NodeExtentProvider}.

If all nodes have the same size you can use a {@link org.abego.treelayout.util.FixedNodeExtentProvider FixedNodeExtentProvider} instance with the proper width and height.

In general you will create your own NodeExtentProvider implementation.

Example

Assume you want to create a tree with TextInBox items as nodes:

UML diagram for TextInBox

Here each node contains its width and height. So your NodeExtentProvider may look like this:

 public class TextInBoxNodeExtentProvider implements
         NodeExtentProvider<TextInBox> {

     @Override
     public double getWidth(TextInBox treeNode) {
         return treeNode.width;
     }

     @Override
     public double getHeight(TextInBox treeNode) {
         return treeNode.height;
     }
 }

Configuration

You can use a {@link org.abego.treelayout.Configuration Configuration} to customize various aspects of the TreeLayout:
  • the gap between levels,
  • the minimal gap between nodes,
  • the position of the root node,
  • the alignment of smaller nodes within a level.
Most of the times using the {@link org.abego.treelayout.util.DefaultConfiguration DefaultConfiguration} class will be sufficient.

Root Position

By default the root of the tree is located at the top of the diagram. However one may also put it at the left, right or bottom of the diagram.
Possible Root Positions
Top (Default) Left Right Bottom
Tree with root at top Tree with root at left side Tree with root at right side Tree with root at bottom

See {@link org.abego.treelayout.Configuration#getRootLocation() getRootLocation}.

Alignment in Level

By default all nodes of one level are centered in the level. However one may also align them "towards the root" or "away from the root". When the root is located at the top this means the nodes are aligned "to the top of the level" or "to the bottom of the level".

Alignment in level when root is at the top
Center (Default) TowardsRoot ("top of level") AwayFromRoot ("bottom of level")
Tree with root at top and nodes center aligned Tree with root at top and nodes aligned to top Tree with root at top and nodes aligned to bottom
Alignment in level when root is at the left
Center (Default) TowardsRoot ("left of level") AwayFromRoot
("right of level")
Tree with root at left side and nodes center aligned Tree with root at left side and nodes left aligned Tree with root at left side and nodes right aligned

Of cause the alignment also works when the root is at the bottom or at the right side.

See {@link org.abego.treelayout.Configuration#getAlignmentInLevel() getAlignmentInLevel}.

Gap between Levels and Nodes

The gap between subsequent levels and the minimal gap between nodes can be configured.

levels, gapBetweenLevels and gapBetweenNodes Visualization

See {@link org.abego.treelayout.Configuration#getGapBetweenLevels(int) getGapBetweenLevels} and {@link org.abego.treelayout.Configuration#getGapBetweenNodes(Object, Object) getGapBetweenNodes}.

Examples

In the "demo" package you will find examples using the TreeLayout.
  • SVGDemo - Demonstrates how to use the TreeLayout to create a tree diagram with SVG (Scalable Vector Graphic)
  • SwingDemo - Demonstrates how to use the TreeLayout to render a tree in a Swing application

Performance

Based on Walker's algorithm [1] with enhancements suggested by Buchheim, Jünger, and Leipert [2] the software builds tree layouts in linear time. I.e. even trees with many nodes are built fast. Other than with the Reingold–Tilford algorithm [3] one is not limited to binary trees.

The following figure show the results running the TreeLayout algorithm on a MacBook Pro 2.4 GHz Intel Core 2 Duo (2 GB Memory (-Xmx2000m)). The variously sized trees were created randomly.

Chart: Performance of TreeLayout on random trees

The picture illustrates the linear time behavior of the algorithm and shows the applicability also for large number of nodes. In this setting it takes approx. 5 µs to place one node.

License

TreeLayout is distributed under a BSD license of abego Software. (License text)

Sponsor

The development of TreeLayout was generously sponsored by Terence Parr "The ANTLR Guy" (parrt at cs dot usfca dot edu).

References

[1] Walker JQ II. A node-positioning algorithm for general trees. Software—Practice and Experience 1990; 20(7):685–705.

[2] Buchheim C, Jünger M, Leipert S. Drawing rooted trees in linear time. Software—Practice and Experience 2006; 36(6):651–665

[3] Reingold EM, Tilford JS. Tidier drawings of trees. IEEE Transactions on Software Engineering 1981; 7(2):223–228.

@author Udo Borkowski ([email protected]) */ package org.abego.treelayout;





© 2015 - 2024 Weber Informatics LLC | Privacy Policy