Subtree

john's Avatar

john

06 Jul, 2025 10:35 PM

Subtree is to hierarchies what slice is to lists.

The subtree node extracts a subtree from a hierarchy. Like slice, it has an invert option which allows you to extract everything BUT the subtree. You can either take the rows untouched from the original hierarchy table, or fashion them into a new tree that can stand on its own. The output is a table in standard hierarchy format.

Subtree takes four parameters:

  • Hierarchy. A data table of a hierarchical structure supplied by my hierarchy node.
  • Subtree ID. The ID of the root of the subtree as listed in the hierarchy ID column of the hierarchy table.
  • Invert. If checked, returns all row EXCEPT those belonging to the subtree.
  • New Tree. If checked, reformats the hierarchy ID and level columns so that the subtree can function as a valid stand-alone hierarchy.

Whether or not you check the "New Tree" option depends on what you want to do with the subtree:

  • If you are only isolating the subtree to analyze it in its original context, you can leave the option unchecked. For example, if you want to determine the min, max, and average depth of the subtree's position within the larger tree, you will need the levels as they originally appeared.
  • If you want to visualize the subtree in a chart or map, or use it as the input to any other node that takes a hierarchy parameter, you will need to check the "New Tree" option. The root of the subtree will become the root of the new tree, with an ID of "h" and a level set to 0. If the "Invert" option was checked, this will repair the "hole" created by renumbering any siblings left behind.

Note: the entire tree is considered to be a subtree of itself, so if you se the Subtree ID to "h", you will get the entire tree, and if you then check the "Invert" option, you will get nothing (no rows returned). Each leaf is a subtree of one, so will always return a single row. If you provide an ID that does not exist within the tree, you will get nothing.

The demo takes the same simple hierarchy featured in the hierarchy demo and breaks it into every possible subtree. It does this by identifying each item in tree with one or more children, removing the root of the main tree, and passing those IDs to a subnetwork (subtree_chart) that turns each ID into a subtree chart. I then use the arrange node to arrange all eight subtrees in two rows.

The first subtree consists of the 8 nodes belonging to the first child of the original tree. The second subtree is a subtree of that first subtree, starting with node 3, the first grandchild, and its four children. In the second row we get two subtrees formed from the second child (node 9), and then four subtrees formed from the third child (node 13).

In order to see the subtree node directly in action, I also make a second cutting of the third child (node 13, ID h.2), and use draw_table to show the actual output of the subtree node. You can turn the Invert and New Tree options on and off to see how that affects the table output. You can also choose a different Subtree ID to see the output of any item in the tree.

I am already starting to use subtree in my projects and am finding it just as useful and essential as the slice node.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • subtree_screenshot.png 508 KB
  • subtree_demo.ndbx.zip 520 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

24 May, 2026 08:01 AM
19 Apr, 2026 11:02 AM
06 Apr, 2026 08:40 PM
12 Mar, 2026 01:15 AM
10 Mar, 2026 03:15 AM

 

22 Jan, 2026 08:50 AM
22 Jan, 2026 07:17 AM
19 Dec, 2025 07:35 AM
26 Nov, 2025 06:16 AM
24 Nov, 2025 03:36 PM
16 Nov, 2025 11:08 PM