说我定义为每个在推荐树这个帖子,虽然它在我的情况下,一个载体,它希望不应该的问题(他们在编程Clojure的书向量):
(def tree [1 [[2 [4] [5]] [3 [6]]]])
Run Code Online (Sandbox Code Playgroud)
这应该是这样的:
1
/ \
2 3
/ \ |
4 5 6
Run Code Online (Sandbox Code Playgroud)
现在,我想在没有任何传统方法(例如队列)的情况下对树进行广度优先遍历,而是专门使用堆栈来传递信息.我知道这不是最简单的路线,但我主要是做运动.此外,我不打算返回一个集合(我会在之后将其视为练习),而是在我浏览它们时打印出节点.
我目前的解决方案(刚开始使用Clojure,很好):
(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))))))) …
Run Code Online (Sandbox Code Playgroud) 在Scheme中,我可以使用define-struct
二进制搜索树,但是你如何在Clojure中做到这一点?