digital-society-coop / elm-tree-layout / TreeLayout

Return x and y coordinates for a general tree, layed out according to aesthetic rules defined by (Walker, 1989). This implementation is based on the pseudo-code given in the improved algorithm that runs in linear time by (Buchheim, Junger and Leipert, 2006).

While providing the coordinates, this module does not render the tree to keep it generic.

treeLayout : Basics.Float -> List ( comparable, Maybe comparable ) -> Dict comparable Coordinate

Return a dictionary of x and y coordinates keyed by a unique identifier for a node for a given 'distance' (used as a constant for x spacing) and 'nodes' (a list of hierarchy descriptors starting with the root, represented by tuples of node identifier and parent node identifier).

treeLayout 2 [ ( "a", Nothing ), ( "b", Just "a" ), ( "c", Just "a" ) ]
--> (Dict.fromList [ ( "a", { x = 0, y = 1 } ), ( "b", { x = -1, y = 2 } ), ( "c", { x = 1, y = 2 } ) ])

Note: The x coordinates will have negative values, as the root node will always be at 0. The y coordinates will be integers from 1 to the max depth of the tree.