Hi Dave,

I don't know if there is the one true way of representing a tree as a
vector, but clojure.zip allows you to zip over pretty much any structure
you like, as long as you provide the right functions. The zipper
function is a bit like an informal protocol in that way.

The built-in vector-zip treats all vectors as branch nodes, but these
branch nodes don't have any "node value" of their own, as you would like them
to have in your example.

On Mon, Oct 15, 2012 at 01:20:13PM -0700, Dave Kincaid wrote:
> <https://lh5.googleusercontent.com/-mq1roiqfqw4/UHxvMDQUIfI/AAAAAAAAA_o/mNA9pAkOEIs/s1600/tree.png>
> my first intuition was a vector like this [:A [:B [:D :E]] [:C [:F :G]]] but 
> that didn't work. Then I thought of something like this: [[:B [:D :E]] :A 
> [:C [:F :G]]]. It seems like that gets me a little bit closer since now the 
> zipper at least gets that :A has two children.

Here is a primitive example of how you might construct the tree you want
with a vector notation. The first element in the vector is treated
as the "node value", the rest as the children (this use of a vector is
similar to how the hiccup library uses vectors, for example).

(ns foo
  (:require [clojure.zip :as z]
            [clojure.string :as string]))

(defn print-tree [loc]
  (when-not (z/end? loc)
    (do
      (println (str (string/join "" (repeat (count (z/path loc)) " "))
                    (if (z/branch? loc)
                      (first (z/node loc))
                      (z/node loc))))
      (recur (z/next loc)))))

(defn my-make-node [n children]
  (cons (first n) children))

(defn my-zipper [v]
  (z/zipper coll? next my-make-node v))

(defn show-tree []
  (print-tree (my-zipper [:A [:B :D :E] [:C :F :G]])))


Regards,

Caspar


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to