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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:45:22+00:00 2026-06-07T17:45:22+00:00

Say I have a tree defined as per the recommendation in this post ,

  • 0

Say I have a tree defined as per the recommendation in this post, although it’s a vector in my case, which hopefully shouldn’t matter (they’re vectors in Programming Clojure book):

(def tree [1 [[2 [4] [5]] [3 [6]]]])

which should be something like:

      1
     / \
    2   3
   / \  |
  4   5 6

Now, I’d like to do a breadth-first traversal of the tree without any of the traditional means such as the queue, and instead use exclusively the stack to pass information around. I know this isn’t the easiest route, but I’m doing it mostly as exercise. Also at this point I’m not planning to return a collection (I’ll figure that out afterwards as exercise) but instead just print out the nodes as I travel through them.

My current solution (just starting out with Clojure, be nice):

(defn breadth-recur
  [queue]
  (if (empty? queue)
    (println "Done!")
    (let [collections (first (filter coll? queue))]
      (do
        ; print out nodes on the current level, they will not be wrapped'
        ; in a [] vector and thus coll? will return false
        (doseq [node queue] (if (not (coll? node)) (println node)))
        (recur (reduce conj (first collections) (rest collections)))))))

The last line is not working as intended and I’m stumped about how to fix it. I know exactly what I want: I need to peel each layer of vectors and then concatenate the results to pass into recur.

The issue I’m seeing is mostly a:

IllegalArgumentException Don't know how to create ISeq from: java.lang.Long 

Basically conj doesn’t like appending a vector to a long, and if I swap conj for concat, then I fail when one of the two items I’m concatenating isn’t a vector. Both conj and concat fail when facing:

[2 [4] [5] [3 [6]]]

I feel like I’m missing a really basic operation here that would work both on vectors and primitives in both positions.

Any suggestions?

Edit 1:

The tree should actually be (thanks Joost!):

(def tree [1 [2 [4] [5]] [3 [6]]])

However we still haven’t found a breadth-first solution.

  • 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-07T17:45:25+00:00Added an answer on June 7, 2026 at 5:45 pm

    Since apparently there is still no breadth-first solution posted, here is a simple algorithm, implemented first eagerly, and then transformed to be lazy:

    (defn bfs-eager [tree]
      (loop [ret [], queue (conj clojure.lang.PersistentQueue/EMPTY tree)]
        (if (seq queue)
          (let [[node & children] (peek queue)]
            (recur (conj ret node) (into (pop queue) children)))
          ret)))
    
    (defn bfs-lazy [tree]
      ((fn step [queue]
         (lazy-seq
          (when (seq queue)
            (let [[node & children] (peek queue)]
              (cons node
                    (step (into (pop queue) children)))))))
       (conj clojure.lang.PersistentQueue/EMPTY tree)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a method that returns this. Vector[ (PkgLine, Tree) ]() I want
Say I have a binary tree which contains pointers at each node going to
Let's say I have a binary tree data structure defined as follows type 'a
Let's say I have a class which will be used to create either tree
Let’s say I have table/list like this n=3 in this case, but n can
Lets say i have something like this: This is file tree.py: class leaf(): def
I have a javascript array which defines an infinite tree like this. z=['~GROUPHEAD 0~',
Say I have element tree returned by xquery categories = tree.xpath(u//ul[@class='tree-root']/li) for cat in
I have an own Tree object implemented in PHP. Say we have a tree
Say we have a binary tree as follow: I'm looking for an algorithm to

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.