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

  • Home
  • SEARCH
  • 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 9198871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:23:29+00:00 2026-06-17T22:23:29+00:00

I got a tree: (A . ((C . ((D . nil)(E . nil))) (B

  • 0

I got a tree:

(A . ((C . ((D . nil)(E . nil)))
      (B . ((F . nil)(G . nil)))))

I want to transform this tree into:

((A C D) (A C E) (A B F) (A B G))

I already implemented this function for doing so:

(defun tree->paths (tree &optional buff)
  (labels ((recurse (follow-ups extended-list)
             (if follow-ups
                 (append (list (tree->paths (car follow-ups) extended-list))
                         (recurse (cdr follow-ups) extended-list))
               nil)))
    (rstyu:aif (cdr tree)
               (recurse it (append buff (list (car tree))))
               (append buff (list (car tree))))))

But applying it results in:

(tree->paths '(A . ((C . ((D . nil) (E . nil)))
                    (B . ((F . nil) (G . nil))))))
=>
(((A C D) (A C E)) ((A B F) (A B G)))

I must be missing some kind of append/merge within the recursion but I am not seeing it.

  • 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-17T22:23:30+00:00Added an answer on June 17, 2026 at 10:23 pm

    Here, I’ve tried to rewrite it so that it would work linearly (because your original function would exhaust stack space). However, while doing so, I’ve discovered something, which you might consider in general re’ your original idea:

    (defun tree-to-paths (tree)
      (loop with node = tree
           with trackback = nil
           with result = nil
           with head = nil
           with head-trackback = nil
           while (or node trackback) do
           (cond
             ((null node)
              (setf node (car trackback)
                    trackback (cdr trackback)
                    result (cons head result)
                    head (car head-trackback)
                    head-trackback (cdr head-trackback)))
             ((consp (car node))
              (setf trackback (cons (cdr node) trackback)
                    head-trackback (cons head head-trackback)
                    head (copy-list head)
                    node (car node)))
             (t (setf head (cons (car node) head)
                      node (cdr node))))
           finally (return (nreverse (mapcar #'nreverse result)))))
    

    In your example data the result you want to receive seems intuitively correct, but you can think of it also as if there were more paths, such as for example:

    A -> C -> NIL – From looking at your data, this result seems redundant, but in general, you may want to have these results too / it would be hard to filter them all out in general.

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

Sidebar

Related Questions

i got this jsTree: $(function () { $(#tree).jstree({ json_data : { data : [
Got this error message while trying to load view: The model item passed into
I've got a tree-like structure. Each element in this structure should be able to
I've got some delimited data that I need to get into a Flex tree
I've got a DOM tree I want to receive org.xml.sax.helpers.DefaultHandler callbacks from it. Is
I've got a comment tree nested in the document, using mongoid embeds_many_recursively like this:
this code i got is from Alexander Battisti about how to make a tree
Background I've got the following tree of objects: Name Project Users nil John nil
In VIM I've got 4 windows opened and a NERD tree like this: So,
I got tree functions: var a = function (f) { // some code window.open()

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.