

How to transform a seq into a tree
source link: https://www.codesd.com/item/how-to-transform-a-seq-into-a-tree.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to transform a seq into a tree
I have a seq of maps such as the coll below. I want to arrange it in a tree. Each map has a key named :parent which is the :id of the parent. Any hints on how can I do it ?
(def coll [{:id 1}
{:id 2 :parent 1}
{:id 3 :parent 1}
{:id 4 :parent 2}
{:id 5 :parent 4}
{:id 6 :parent 5}
{:id 7 :parent 5}
{:id 8 :parent 5}
{:id 9 :parent 7}])
If it walks like a tree...
(require '[clojure.zip :as z])
(defn create-zipper [s]
(let [g (group-by :parent s)]
(z/zipper g #(map :id (g %)) nil (-> nil g first :id))))
(def t (create-zipper coll)) ; using the coll defined in the OP
(-> t z/root)
;=> 1
(-> t z/children)
;=> (2 3)
(-> t z/next z/children)
;=> (4)
Note that you may preserve the format of the original nodes (rather than just returning id numbers) by using #(g (% :id))
as the children and (first (g nil))
as the root.
You can use post-order traversal to build up another representation of the tree if desired.
Related Articles
Spark: How to transform a Seq from RDD to RDD
In python, how to transform the slash into an escape sequence
How to transform a string into a python list?
How to transform this find_by_sql into named_scope?
How to transform the text into input?
How to transform unclassified elements into xml to match an xsd: sequence order?
How to transform vertical data into horizontal data with SQL?
How to transform this Func into an expression?
How to transform this string into a table?
How to transform the matrix into a digital matrix?
How to transform a labyrinth into a graph?
How to transform the matrix into a matrix with an identity matrix
How to transform a number into its binary representation?
How to transform a tuple into a whole number in Python (examples inside)?
Recommend
-
7
.Net Core with 微服务 - Seq 日志聚合 上一次我们介绍并演示了如...
-
2
以指定增量从首数开始打印数字到尾数,seq命令用于产生从某个数到另外一个数之间的所有整数。> seq [选项]... 尾数 > seq [选项]... 首数 尾数 > seq [选项]... 首数 增量 尾数-f, --for...
-
5
seq命令是sequence的缩写,用于以递增或者递减的方式打印数字序列。换句话说,就是打印指定数值的范围。数字可以是整数或带小数点的实数,也可以是负数。你也可以指定序列的上限或者下限等。在本教程中,我们将说明如何使用seq命令和seq常用的命令选项,...
-
2
André Slupik Posted on Mar 3...
-
7
最近在分析影響效能的 Query,發現 PostgreSQL 有時的查詢效能不如我們預期,用了 EXPLAIN 下去分析索引,發現確實新增的 index 並沒有在 query plain 裡面,我想瞭解為什麼。評估結果是 seq scan 更快在數據量很小的時候, seq scan 會比 inde...
-
4
kernel 劫持seq_operations && 利用pt_regs 劫持seq_operations进行栈迁移 seq_operations是一个大小为0x20的结构体,在...
-
4
Comparison of SEQ With and Without Numbers Jim Lewis, PhD and Jeff Sauro, PhD...
-
3
Evaluation of Three SEQ Variants Jeff Sauro, PhD • Jim Lewis, PhD ...
-
5
理解TCP协议中SEQ与ACK的计算逻辑 理解TCP协议中SEQ与ACK的计算逻辑 tcpdump 抓包示例(删除了不必要的字段): 172.31.27.207.33944 > 192.168.242.180.27017: Flags [S], seq 1310410798 ... length 0...
-
7
Describing SEQ® Scores with Adjectives Jim Lewis, PhD and Jeff Sauro, P...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK