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 7763131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:35:16+00:00 2026-06-01T14:35:16+00:00

Tree traversal refers to the process of visiting each node in a tree data

  • 0

Tree traversal refers to the process of visiting each node in a tree data structure in a systematic way. The preorder traversal in the following image

Sorted_binary_tree

returns F, B, A, D, C, E, G, I, H (root, left, right). This is the Prolog code:

preorder(tree(X,L,R),Xs) :-
    preorder(L,Ls),
    preorder(R,Rs),
    append([X|Ls],Rs,Xs).
preorder(void,[]).

I would write a prolog program that returns

F, B, A, F,B,D, C,F,B,D, E,F, G, I, H

that is, the paths of the tree. Any suggestions?

  • 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-06-01T14:35:18+00:00Added an answer on June 1, 2026 at 2:35 pm

    Try this out for size:

    dfs_paths(tree(X, void, void), [X]) :- !.
    dfs_paths(tree(X, L, _R), [X|Xs]) :-
        dfs_paths(L, Xs).
    dfs_paths(tree(X, _L, R), [X|Xs]) :-
        dfs_paths(R, Xs).
    

    Testing it out with a fact representing the tree from here:

    tree(T) :- T = tree(f, tree(b, tree(a,void,void), tree(d,tree(c,void,void),tree(e,void,void))), tree(g, void, tree(i, tree(h,void,void),void))).
    

    Trying it out (without showing bindings for T):

    ?- tree(T), dfs_paths(T, L).
    L = [f, b, a] ;
    L = [f, b, d, c] ;
    L = [f, b, d, e] ;
    L = [f, g, i, h] 
    

    Note that dfs_paths/2 backtracks to give you the alternate paths. If you wanted a list of them all up front, you could try:

    ?- tree(T), findall(P, dfs_paths(T, P), Ps).
    Ps = [[f, b, a], [f, b, d, c], [f, b, d, e], [f, g, i, h]].
    

    If you wanted a flat list of terms as you’ve written above, you can flatten/2 the result.

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

Sidebar

Related Questions

I've got hierarchal data in the database, stored in Modified Preorder Tree Traversal format.
I have hierarchical ordered data saved using the modified preorder tree traversal algorithm. Here's
I'm using nested sets (aka modified preorder tree traversal) to store a list of
I had been working on Preorder tree traversal algorithm as a part of my
Based on this question: Getting a modified preorder tree traversal model (nested set) into
I m trying to implement Tree Traversal PreOrder using yield return which returns an
Assuming a complete binary tree, each node can be adressed with the position it
In addition to this question: Preorder tree traversal copy folder I was wondering if
I have implemented an iterative algorithm, where each iteration involves a pre-order tree traversal
Based on Getting a modified preorder tree traversal model (nested set) into a <ul>

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.