algorithm - Multiway tree construction from a node string -


There is a wonderful problem called ninety-nine, the problem is specified in the P70 header. And here is a great promotion of this problem which only takes 5 lines. However, my understanding of prologue is limited.

How does this solution appear in a C-like form (no Iitirolls available)?

Edit by request I hope I do not violate copyright.

Problem:

Syntax in BNF:

  tree: = letter one '^' forest:: = | Tree forest  

A good solution using inter alia:

  tree (ts, t): - atom (ts), !, atom_chairs ( TS, TL), tree_d (TL- [], T). % (+,?) Tree (ts, t): - nonveir (t), tree_d (tl- [], t), atom_church (ts, tl). % (?, +) Tree_d ([x | f1] -t, t (x, f)): - one_d (f1 - ['^' | t], f) one_d (ff, [] ) One_D (F1-F3, [TF]): - Tree_D (F1-F2, T), One_D (F2-F3, F).  

problem definition

(taken from)

We believe that the node of the multivace tree has a single character. In the sequence sequence before the depth of your nodes, whenever a particular character is ^ during the tree, this step is backwards at the previous level.

According to this rule, the tree is represented in the figure: afg ^^ c ^ bd ^ e ^^^

alt text

Define the syntax of the string and one wordicate Type tree (string, tree)) tree when string is given to create . Work with atoms (instead of stars) in both directions.


Solution Part 1: String2Tree

This is a stack Is easy with This is a pseudocode:

  function string 2 tree (string stroke): tree light st new stack & lieutenant; Node & gt; Please Root New Nude Stats. Process (Route) FOREACH EL IN ST IF L 'IS' ST.pop () ELSE take baby new-node (L) LET Top ST.top (top) top.adopt (child) St.Push (child) Returns New -Tree (root)  

A dummy root node simplifies cases using. Basically the algorithm is as follows:

  • String from left to right
  • Whenever we encounter a node label, we create a new node
      < Li> Li> > We simply close the top of the heap

    the solution part 2: Tree2String

    The opposite direction is a simple recursive case:

      function string (tree T): string late sb. New-Stringbuffer Visit (T.ROT, SB) Returns New-String (SB) Proceedings of Execution (Node N, Stringbuffer SB) sb.append (n.label) NK Children () for Children () Travel (Children, SB) Sb.append ('^')  

    As specified in the problem, whenever we go back to the previous level, we include the ^ .


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -