 (defn find-in [xs n]
   (.indexOf xs n))
 
+(defrecord Node [triple element sub-counter children])
+
+(defn count-elements
+  [{:keys [sub-counter element]}]
+  (if element
+    (inc sub-counter)
+    sub-counter))
+
+(defn count-children
+  [children]
+  (reduce + (map count-elements children)))
+
 (defn node-zip
   [root]
   (z/zipper
-    (fn [_] true)
-    (fn [n] (if (vector? n)
-              (seq n)
-              (:children n)))
-    (fn [n c] nil) ;do not use any editing on the tree
+    (fn [node] (:children node))
+    (fn [node] (seq (:children node)))
+    (fn [{:keys [triple element]} children]
+      (->Node triple
+              element
+              (count-children children)
+              (sorted :triple children)))
     root))
-
-(defrecord Node [triple element sub-counter children])
 
 (defn node
   [[triple & xs] element]

                (= (count children) 0))
          (conj path index)
          (recur (conj path index)
                 (get-in node [:children index])
                 (get-in limb [:children 0])))))))

(defn crawl-to
  "returns a zipper with it's location where the index in a node"

  [{:keys [children] :as node} triple]
  (find-in (map :triple children) triple))

(defn nth-child
  "Returns the loc of the nth child of the node at this loc, or
  nil if no children or nth-child"
  [loc n]
  (when (z/branch? loc)
    (let [[node path] loc
          cs (z/children loc)
          c (nth cs n nil)]
      (when (and cs c)
        (with-meta [c
                    {:l (if (> n 0) (vec (take n cs)) [])
                     :pnodes (if path
                               (conj (:pnodes path) node)
                               [node])
                     :ppath path
                     :r (drop (+ n 1) cs)}]
                   (meta loc))))))

(defn add
  "adds a node with one child at each level(a limb) to the children of a node"
  ; TODO use a indexes func on a limb instead of this
  [node limb]
  (let [paths (indexes node limb)]
    (if (last paths)
      (loop [[head & tail] paths,
             node-loc (node-zip node),
             limb limb]
        (let [node-child (nth-child node-loc head)]
          (if (empty? tail)
            (if (-> node-child z/node :element)
              node
              (-> node-child
                  (z/edit assoc :element (:element limb))
                  z/root))
            (recur tail
                   node-child
                   (first (:children limb))))))
      (loop [[head & tail] paths
             node-loc (node-zip node)
             limb limb]
        (if-not head
          (-> node-loc (z/append-child limb) z/root)
          (recur tail
                 (nth-child node-loc head)
                 (first (:children limb))))))))

(defn del
  [this node]
