How to write the BNF for this tree-related operation?

孤街醉人 提交于 2019-12-13 02:42:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!