Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:41:08+00:00 2026-05-16T01:41:08+00:00

There is a wonderful problem set called Ninety-Nine Prolog Problems . Problem P70 is

  • 0

There is a wonderful problem set called Ninety-Nine Prolog Problems. Problem P70 is the one referred to in the title. And here is a great Prolog solution of this problem which takes only 5 lines. However, my understanding of Prolog is limited.

How does this solution look like in a C-like form (no itertools available)?

Edited by request. I hope I do not violate copyright.

The problem:

Syntax in BNF:

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

A nice solution using difference lists:

tree(TS,T) :- atom(TS), !, atom_chars(TS,TL), tree_d(TL-[ ],T). % (+,?)
tree(TS,T) :- nonvar(T), tree_d(TL-[ ],T), atom_chars(TS,TL).   % (?,+)
tree_d([X|F1]-T, t(X,F)) :- forest_d(F1-['^'|T],F).
forest_d(F-F,[ ]).
forest_d(F1-F3,[T|F]) :- tree_d(F1-F2,T), forest_d(F2-F3,F).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:41:09+00:00Added an answer on May 16, 2026 at 1:41 am

    Problem Definition

    (taken from P-99: Ninety-Nine Prolog Problems)

    We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character ^ has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.

    By this rule, the tree in the figure is represented as: afg^^c^bd^e^^^

    alt text
    (source: ti.bfh.ch)

    Define the syntax of the string and write a predicate tree(String,Tree) to construct the Tree when the String is given. Work with atoms (instead of strings). Make your predicate work in both directions.


    Solution Part 1: String2Tree

    This is easy with a stack. Here’s the pseudocode:

    FUNCTION String2Tree(String str) : Tree
       LET st BE New-Stack<Node>
       LET root BE New-Node
       st.push(root)
    
       FOREACH el IN str
          IF el IS '^'
             st.pop()
          ELSE
             LET child BE New-Node(el)
             LET top BE st.top()
             top.adopt(child)
             st.push(child)
    
       RETURN New-Tree(root)
    

    The use of a dummy root node simplifies matters. Essentially the algorithm is as follows:

    • Scan the string left to right
    • Whenever we encounter a node label, we create a new node
      • That node is adopted by the top of the stack
      • That node is then pushed to the the stack and becomes the new top
    • When we encounter a '^', we simply pop off the top of the stack

    Solution Part 2: Tree2String

    The opposite direction is a matter of simple recursion:

    FUNCTION string(Tree t) : String
       LET sb BE New-StringBuffer
    
       visit(t.root, sb)
    
       RETURN New-String(sb)
    
    PROCEDURE visit(Node n, StringBuffer sb)
       sb.append(n.label)
    
       FOREACH child IN n.children()
          visit(child, sb)
    
       sb.append('^')
    

    As specified in the problem, we insert ^ whenever we backtrack to the previous level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Windows 7 has a wonderful facility called problem steps recorder . You can start
Bazaar has a wonderful uncommit command, which simply undoes the last commit. Is there
There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
There are two table s : one is the master and one the detail
I'm just beginning to use Linq-to-SQL and it's just wonderful to use. The problem
There are tons of great examples of creating work items using the TFS API,
I've looked around here and saw wonderful solutions how to print the content of
Hello wonderful people of stackoverflow! I have a problem. I am working on an
I read here and there about touch but I can't get to it completely.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.