Tree diagrams
A first draft, 26 October 2026: Peter's note on tree diagrams, revised for MyMark, with the fat form in place of s-expressions. It elaborates the short guide in The formal model.
Tree data structures are traditionally shown like this (but with better graphics!):
23
/ | \
5 201 5
/ | \
74 12 9
The numbers in this tree have no particular meaning: the example is copied from a tutorial on tree structures for the C programming language.
Here is the same tree structure again, this one showing a family hierarchy:
Susan
/ | \
Mike Kim Chuck
/ | \
Foster Yoda Jane
Diagrams like this are so well established in computer science that we almost always see tree data structures presented this way. But for some applications there is a better way.
Tree-line diagrams
In a traditional diagram the root is at the top, and the tree is upside down, but it still looks a bit like a tree. An alternative tree-line diagram shows the same branch and leaf structure from a different perspective:
23
| 5
| 201
| | 74
| | 12
| | 9
| 5
Or:
Susan
| Mike
| Kim
| | Foster
| | Yoda
| | Jane
| Chuck
These are the same examples; they just don't look much like a botanical tree any more, but the branching structure is clearly preserved.
At first sight the branching structure of a tree-line diagram may not be so obvious. But after a little familiarity it becomes quite intuitive to read down a column rather than across a row. The tree-line format is easy to read and write, and it can make the structure of the tree easier to understand, particularly for larger trees.
Reading down the page, the nodes are in depth-first order. Processing of tree data structures is often done in this order, and that is very convenient. The children are indented under their parent, so it is easy to read down the list of siblings, and to scan back up from a child to find its parent.
More examples
The same C tutorial had more examples, first a simple AST (Abstract Syntax Tree):
Function
/ | \
Id Args Body
/ | \
Decl Decl Decl
And as a tree-line diagram:
Function
| Id
| Args
| | Decl
| | Decl
| | Decl
| Body
A file structure:
root
/ | | \
user bin proc home
/ \ |
local bin jacob
As a tree-line diagram:
root
| user
| | local
| | bin
| bin
| proc
| home
| | jacob
This format is very similar to the way programmers indent source code to show the nested structure of data and program code, and to the much richer TreeView interface that is used to show files and folders. Tree-line diagrams are crude in comparison, but very simple to read and write. Their bars mimic the faint guide lines that modern editors draw beside indented code, and they could be drawn as light grey box-line characters, or replaced by white-space indentation.
Tall or fat
A tree-line diagram may be laid out tall, a line for every node, or fat, with nodes run on along a line. On a line:
:goes down, to a node's first child;,goes across, to its next sibling;;goes back up a level, one;for each level.
So type: child, child; is a node and two children. The tall tree:
Susan
| Mike
| Kim
| | Foster
| | Yoda
| | Jane
| Chuck
is, all on one line:
Susan: Mike, Kim: Foster, Yoda, Jane; Chuck
The ; after Jane comes back up from Kim's children, so that Chuck is Susan's child. Closing ; at the end of a line may be left out: the next line's bars say where it is.
The two layouts mix. Lines are given to the nodes down to some depth, the trunk, and below it each node's subtree runs on along its line. This is how pPEGpy's stree(trunk) prints a parse tree: tall by default, and "often a useful picture emerges at depth 2 or 3".
Function
| Id
| Args: Decl, Decl, Decl
| Body
Parse trees
A parser takes an input string and generates a parse tree, or AST. For the input:
x = y + 2*z + 1
an AST labels essential elements and discards white space and concrete syntax. pPEGpy, with this grammar:
assign = var _ '=' _ add
add = mult (_ '+' _ mult)*
mult = val (_ '*' _ val)*
val = int / var
var =: [a-z]+
int =: [0-9]+
_ : ' '*
prints its parse tree as:
assign
| var 'x'
| add
| | var 'y'
| | mult
| | | int '2'
| | | var 'z'
| | int '1'
All the nodes have names, the grammar's rule names, and the leaves have a string value, the text they matched. At trunk 1, and at trunk 0:
assign
| var 'x'
| add: var 'y', mult: int '2', var 'z'; int '1'
assign: var 'x', add: var 'y', mult: int '2', var 'z'; int '1'
In JSON, as pPEGpy's ptree, the same tree is:
['assign', [['var', 'x'], ['add', [['var', 'y'], ['mult', [['int', '2'], ['var', 'z']]], ['int', '1']]]]]
The JSON obscures the tree structure; the tree-line diagram is much easier to read, particularly for larger parse trees.
Tree-line grammar
A tree, printed as a tree-line diagram, can be read back in, so the round trip loses nothing. This grammar reads both layouts, tall and fat:
# Tree-line diagrams, tall or fat. A line is a node: its inset, a bar
# and a space for each level of depth, then the node, then perhaps more
# nodes run on along the line, each after a separator: ':' goes down to
# a child, ',' across to a sibling, ';' back up a level (one ';' a
# level). The separators are depth changes, as the bars are: the tree
# is read from them, not by the grammar. Closing ';' at the end of a
# line may be left out.
Tree = _ line+
line = inset node (sep node)* ';'* _
inset =: ('| ')*
node = key (' ' val)?
key =: ~[ \t\n\r:,;']+
val =: ['] ~['\n\r]* [']
sep =: (':' / ',' / ';'+) ' '
_ : [ \t\n\r]*
The grammar reads the bars and the separators, and the depth of each node follows from them: a line's bars set the depth of its first node; : goes one deeper, , stays, each ; comes back up one. Reading them gives the nodes in depth-first order, each with its depth, which is all a tree needs. (experiments/tree-diagrams/ checks that the tall, fat and mixed diagrams here all read to the same nodes.)
A key is any text with no space or separator in it; a value, if there is one, is in single quotes, so that it may hold a separator. Open: how a value holds a line break, which it must not show as one, or the diagram's one line a node is broken.
In MyMark
A tree diagram in a MyMark document is a Tree: block (see The formal model). The root may go after the label, and the bars are then the tree's spine; or, with nothing after the label, the whole tree goes inside its bars. Either way the lines may be tall or fat, and the tree is drawn tall, its bars as light guide lines:
Tree: Susan
| Mike
| Kim: Foster, Yoda, Jane
| Chuck
Susan │ Mike │ Kim │ │ Foster │ │ Yoda │ │ Jane │ Chuck
Tree:
| assign
| | var 'x'
| | add: var 'y', mult: int '2', var 'z'; int '1'
assign │ var 'x' │ add │ │ var 'y' │ │ mult │ │ │ int '2' │ │ │ var 'z' │ │ int '1'
Conclusion
Tree-line diagrams are a very simple way to write almost any tree data structure, and they work particularly well for parse trees. They are text, so they can be stored, sent in messages, and compared; and they are a portable form for trees that are made in different ways, in different programming languages.