August 26, 2024
P69 - Dotstring representation of binary trees.
We consider again binary trees with nodes that are identified by single lower-case letters, as in the example of problem P67. Such a tree can be represented by the preorder sequence of its nodes in which dots (.) are inserted where an empty subtree (End) is encountered during the tree traversal. For example, the tree shown in problem P67is represented as"abd..e..c.fg...". First, try to establish a syntax (BNF or syntax diagrams) and then write two methods,toDotstringandfromDotstring, which do the conversion in both directions.
> Tree.string2Tree("a(b(d,e),c(,f(g,)))").toDotstring
res0: String = abd..e..c.fg...
> Tree.fromDotstring("abd..e..c.fg...")
res1: Node[Char] = a(b(d,e),c(,f(g,)))
The file containing the full class definitions for this section is tree.erl.