Tree SVG

Code to generate SVG from trees

License

License

GroupId

GroupId

nl.dvberkel
ArtifactId

ArtifactId

tree.svg
Last Version

Last Version

1.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Tree SVG
Code to generate SVG from trees
Project URL

Project URL

https://github.com/all-trees/tree-svg
Source Code Management

Source Code Management

http://github.com/all-trees/tree-svg/tree/master

Download tree.svg

How to add to project

<!-- https://jarcasting.com/artifacts/nl.dvberkel/tree.svg/ -->
<dependency>
    <groupId>nl.dvberkel</groupId>
    <artifactId>tree.svg</artifactId>
    <version>1.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/nl.dvberkel/tree.svg/
implementation 'nl.dvberkel:tree.svg:1.1.1'
// https://jarcasting.com/artifacts/nl.dvberkel/tree.svg/
implementation ("nl.dvberkel:tree.svg:1.1.1")
'nl.dvberkel:tree.svg:jar:1.1.1'
<dependency org="nl.dvberkel" name="tree.svg" rev="1.1.1">
  <artifact name="tree.svg" type="jar" />
</dependency>
@Grapes(
@Grab(group='nl.dvberkel', module='tree.svg', version='1.1.1')
)
libraryDependencies += "nl.dvberkel" % "tree.svg" % "1.1.1"
[nl.dvberkel/tree.svg "1.1.1"]

Dependencies

compile (3)

Group / Artifact Type Version
nl.dvberkel : tree.domain jar 1.0.2
org.apache.xmlgraphics : batik-svg-dom jar 1.8
org.apache.xmlgraphics : batik-anim jar 1.8

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.mockito : mockito-core jar 1.10.19
commons-io : commons-io jar 2.4

Project Modules

There are no modules declared in this project.

tree-svg

Code to generate SVG from trees

Usage

Before we can create a SVG of a binary tree, we first need a tree. We can create one via Builder

Builder builder = new Builder();
Tree tree =  builder.build("112211211222");

The Builder#build method accepts a Dyck word and it will build the corresponding Tree.

We can transform the Tree into an SvgTree with a TreeTransformer. The SvgTree determines the bounding boxes of all the subtrees. The TreeTransformer is configured with a Configuration that determines radius of the vertices and the padding around each subtree.

Configuration configuration = configuration().withNodeRadius(20).withLeafRadius(3).withPadding(5);
TreeTransformer treeTransformer = new TreeTransformer(configuration);
SvgTree svgTree = treeTransformer.transform(tree);

The SvgTree can be transformed into a SVGSVGElement, that is the root of a SVG document, with a SvgTreeTransformer.

SvgTreeTransformer svgTreeTransformer = new SvgTreeTransformer(configuration);
SVGSVGElement root = svgTreeTransformer.transform(svgTree);

To stream the SVG document to a file we will use a FileOutputStream and a Transformer.

FileOutputStream stream = new FileOutputStream(new File("src/main/resources/example.svg"));

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
StreamResult result = new StreamResult(stream);
transformer.transform(new DOMSource(root), result);

Put together it will produce the following image

Binary Tree

nl.dvberkel

Versions

Version
1.1.1