将地图矢量转换为树

时间:2015-05-29 12:40:28

标签: clojurescript

我需要像这样转换地图矢量:

[{:id 1 
  :parent nil 
  :name "a" 
  :nodes [{:id 2 
           :parent 1 
           :name "b" 
           :nodes [{:id 3 
                    :parent 2 
                    :name "c"}
                   {:id 4 
                    :parent 2 
                    :name "d" 
                    :nodes [{:id 5 :parent 4 :name "e"}]}]}]}]

到此:

(defn enum [s]
  (map vector (range) s))

(defn build-tree [tree current-path current-parent gr]
      (if-let [nodes (gr current-parent)]
        (let [current-path (conj current-path :nodes)
              new-tree (assoc-in tree current-path nodes)]
          (first 
           (for [[i node] (enum (get-in new-tree current-path))]
             (build-tree new-tree (conj current-path i) (:id node) gr))))
        tree))

(defn list->tree [ls]
  (let [gr (group-by :parent ls)
        root (first (gr nil))]
    (build-tree root [] (:id root) gr)))

更新:

按照目前的要求,我有这个:

public class MyClass
{
    public string MyString {get; set;}   
}

但是我真的怀疑这是惯用的+它无论如何都不起作用(只创建第一个节点,其余的被忽略)。我也怀疑创建一棵树是如此困难。我真的觉得我错过了什么。

1 个答案:

答案 0 :(得分:1)

您可以使用邻接列表或递归方法。

这两种方法都作为答案to this very similar question进行了更详细的讨论。

相关问题