Create a tree using Hiccup:
(ns xyz
(:use tupelo.core tupelo.forest))
(with-forest (new-forest)
(let [root-hid (add-tree-hiccup [:a
[:b 1]
[:b 2]
[:b
[:c 4]
[:c 5]]
[:c 9]])]
Display the tree in a compact format:
(is= (format-paths (find-paths root-hid [:a]))
[[{:tag :a}
[{:tag :b, :value 1}]
[{:tag :b, :value 2}]
[{:tag :b}
[{:tag :c, :value 4}]
[{:tag :c, :value 5}]]
[{:tag :c, :value 9}]]])
Display the paths from the root to all :c
nodes:
(let [c-paths (find-paths root-hid [:** :c]) ]
(is= c-paths [[:0006 :0004 :0002]
[:0006 :0004 :0003]
[:0006 :0005]])
Each value like :0004
is a Hexidecimal ID (HID) that serves as a pointer to a tree node like [:c 9]
.
Each vector of HIDs is a path from the root node :a
to a :c
node. There are three :c
nodes and, hence,
three paths. You can see that the length of each of the 3 paths matches the position in the
tree of the 3 :c
nodes. Each :c
node is at the end of its respective path.
Display the last :c
node as Hiccup:
(is= (hid->hiccup :0005) [:c 9] )
Find paths to all [:c 4]
nodes. There is only one:
(let [c4-paths (find-paths root-hid [:** {:tag :c :value 4}]) ]
(is= c4-paths [[:0006 :0004 :0002]] ))
Find the parent of the [:c 4]
node:
(let [c4-parent (-> c4-paths only reverse second)]
(is= c4-parent :0004)
(is= (hid->hiccup c4-parent) [:b [:c 4] [:c 5]] )
(is= (hid->node c4-parent) {:tag :b, :tupelo.forest/khids [:0002 :0003] } )
This shows the internal structure of tree nodes as simple maps. Node :0004
has a :tag
value of :b
and 2 child
nodes (Kid HIDs => "khids").
Modify the value of the [:c 4]
node:
(let [c4-hid (-> c4-paths only last)]
(is= c4-hid :0002)
(value-update c4-hid inc)
(is= (hid->node c4-hid) {:tupelo.forest/khids [], :tag :c, :value 5}) )