难倒与功能广度优先遍历Clojure中?遍历、广度、功能、Clojure

2023-09-11 03:17:02 作者:Miyoori(御姐范)

说我有在这个帖子,虽然它在我的情况下,一个载体,希望不应该的问题(他们在编程Clojure的书向量):

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.

我目前的解决方案(刚开始使用Clojure,是不错的):

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 

基本上连接词不喜欢附加一个载体,一个长期的,如果我换连词对于 CONCAT ,然后我失败时,这两个项目我是一个串联的是不是一个向量。面对这两个连词和CONCAT失败:

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.

有什么建议?

修改1:

这棵树实际上应该是(Joost的感谢!):

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.

推荐答案

由于显然还有很贴没有广度优先的解决方案,下面是一个简单的算法,来实现第一热切,然后转化为偷懒:

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)))