On Tue, Feb 22, 2011 at 8:37 PM, HB <[email protected]> wrote:
> Hi,
>
> I'm trying to implement searching algorithm for binary tree.
> Here is my Java implementation:
>
> public Node findNode(Integer data) {
> Node current = root;
> while (!data.equals(current.getData())) {
> if (data < current.getData())
> current = current.getLeftChild();
> else
> current = current.getRightChild();
> if (current == null)
> return null;
> }
> return current;
> }
>
>
> And here is an unfinished Clojure version:
>
> (defrecord Node [data left-child right-child])
>
> (defrecord Tree [root])
>
> (defn find-node [tree data]
> "Search for a node"
> (let [root (:root tree)]
> (loop [current root]
> (if (= data (:data current))
> current
> (cond
> (< data (:data current))
> (> data (:data current)))))))
>
> I don't know to to set/assign a value for current inside cond form.
(defn find-node
"Search for a node"
[tree data]
(let [root (:root tree)]
(loop [current root]
(cond
(< data (:data current)) (recur (:left-child node))
(> data (:data current)) (recur (:right-child node))
:else current))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en