问题
We have a tree like this:
We can convert it to a dotstring representation
, i.e.,
Such a tree can be represented by the preorder sequence of its nodes in which dots (.) are inserted where an empty subtree (nil) is encountered during the tree traversal.
So we can convert the tree in the picture to 'abd..e..c.fg...'
.
If I am about to write a function to do this conversion, what's the BNF
or syntax diagrams
of it?
回答1:
It's not clear what you're asking. If you think of the string as a sentence in a language and the tree as an AST for a parse, maybe the following BNF would be right:
tree ::= empty | node
empty ::= '.'
node ::= letter tree tree
letter ::= 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | ...
来源:https://stackoverflow.com/questions/22020879/how-to-write-the-bnf-for-this-tree-related-operation