I've just implemented an inorder traversal function for a vector-based tree.
The functions look like,

(defn- do-traversal [tree idx traversal]
  (cond
   (not (node-exists? tree idx)) traversal
   (leaf? tree idx) (conj traversal (tree idx))
   :else (apply conj
        (do-traversal tree (left-child idx) traversal)
        (tree idx)
        (do-traversal tree (right-child idx) traversal))))

(inorder-traversal [tree]
  (do-traversal tree root-idx []))


This works as expected but now I am looking to refactor the code some. I
wanted to see if I could do away passing an empty vector to the do-traversal
function. So I updated do-traversal to look like,

(defn- do-traversal [tree idx & tree-traversal]
  (let [traversal (if tree-traversal tree-traversal [])]
   (cond
    (not (node-exists? tree idx)) traversal
    (leaf? tree idx) (conj traversal (tree idx))
    :else (apply conj
         (do-traversal tree (left-child idx) traversal)
         (tree idx)
         (do-traversal tree (right-child idx) traversal)))))

When the expected traversal for a tree is [10 20 30] I instead get ([] 30 20
10 [])) in my unit test. Can someone explain to me why using let as I have
done does not work, and what another solution might be?

Thanks

- John

-- 
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