documentationfor yFiles for HTML 2.6

TreeAnalysis

Analyzes a tree graph and calculates important properties of the tree structure.

Inheritance Hierarchy
TreeAnalysis

Remarks

The algorithm supports directed, rooted trees as well as undirected tree structures. Property directedRootedTree yields whether or not the input is a directed, rooted tree.

Note that if it is only required to check whether a graph is a tree/rooted tree while no other tree information is needed, then the utility method isTree offered by GraphStructureAnalyzer is recommended instead.

For undirected trees, this algorithm computes a set of reversedEdges that would need to be reversed in order to make it a directed tree. The TreeAnalysisResult, thus, always yields a directed view of the structure when rooted at node root. Property customRootNode allows to get the analysis result when directing the tree structure such that the specified custom node is the root of the tree.

Other Tree-Related Algorithms

yFiles for HTML supports a number of other algorithms related to trees:

  • FeedbackEdgeSet – finds edges that can be removed or reversed to convert a graph into a tree
  • SpanningTree – calculates a (minimum) spanning tree for a graph

Examples

Analysing a tree graph
// create the tree analysis algorithm
const treeAnalysis = new TreeAnalysis()
// run the analysis
const result = treeAnalysis.run(graph)

// query the root node
const treeRoot = result.root

// query the child nodes of the root and set a specific tag for them
result.getChildren(treeRoot).forEach((node) => (node.tag = 'RootChild'))

// query the nearest common ancestor of two nodes
const nearestCommonAncestor = result.getNearestCommonAncestor(
  node1,
  node2
)// create the tree analysis algorithm
const treeAnalysis = new TreeAnalysis()
// run the analysis
const result = treeAnalysis.run(graph)

// query the root node
const treeRoot = result.root!

// query the child nodes of the root and set a specific tag for them
result.getChildren(treeRoot).forEach((node) => (node.tag = 'RootChild'))

// query the nearest common ancestor of two nodes
const nearestCommonAncestor = result.getNearestCommonAncestor(
  node1,
  node2
)
Make an undirected tree a directed rooted tree
// create and run the analysis
const result = new TreeAnalysis().run(graph)

if (!result.directedRootedTree) {
  // if the tree is not already a directed rooted tree
  result.reversedEdges.forEach((edge) => {
    // reverse edges that the analysis found need reversal in order to get a directed tree
    graph.reverse(edge)

    // set a specific style to highlight edges that have been reversed
    graph.setStyle(edge, highlightReversedEdgesStyle)
  })
}

Type Details

yfiles module
view-layout-bridge
yfiles-umd modules
view-layout-bridge
Legacy UMD name
yfiles.analysis.TreeAnalysis

See Also

This algorithm works with tree structures only. Invoked with a non-tree graph it will fail and throw an exception. If only a subgraph should be analyzed and if that subgraph is a tree, then properties subgraphNodes and subgraphEdges can be configured accordingly.

Constructors

Properties

Methods