Re: Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
This works fine as well. More to digest. Thanks. On 19 Aug, 20:06, Benny Tsai wrote: > This is one way to do it functionally, though it's a bit more verbose. > > "get-nodes" performs a BFS walk of the tree between two nodes, returning the > set of visited nodes: > > (defn get-nodes [from to] >  

Re: Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
It works great. Thanks a lot. I had a feeling concat and reduce were needed; I just couldn't figure out how. On 19 Aug, 22:10, Justin Kramer wrote: > Oops, renamed the function: get-edges = candidates->edges. > > Justin > > > > > > > > On Friday, August 19, 2011 4:03:27 PM UTC-4, Ulrik Sandberg w

Re: Non-binary tree?

2011-08-19 Thread Justin Kramer
Oops, renamed the function: get-edges = candidates->edges. Justin On Friday, August 19, 2011 4:03:27 PM UTC-4, Ulrik Sandberg wrote: > > And get-edges? > > On 19 Aug, 20:52, Justin Kramer wrote: > > Here's another way, which constructs a sequence of edges using > candidates, > > which are th

Re: Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
And get-edges? On 19 Aug, 20:52, Justin Kramer wrote: > Here's another way, which constructs a sequence of edges using candidates, > which are then fed into reduce to build an adjacency list. > > (defn candidates->edges [candidates from to] >   (when-let [kids (seq (candidates from to))] >     (c

Re: Non-binary tree?

2011-08-19 Thread Justin Kramer
Here's another way, which constructs a sequence of edges using candidates, which are then fed into reduce to build an adjacency list. (defn candidates->edges [candidates from to] (when-let [kids (seq (candidates from to))] (concat (for [k kids] [from k]) (mapcat #(get-edges cand

Re: Non-binary tree?

2011-08-19 Thread Benny Tsai
Probably a good idea for "get-nodes" to handle potential cycles: (defn get-nodes [from to] (loop [visited #{} [x & xs] [from]] (if-not x visited (let [new-visited (conj visited x) neighbors (candidates x to) new-queue (remove new-visited (concat x

Re: Non-binary tree?

2011-08-19 Thread Benny Tsai
This is one way to do it functionally, though it's a bit more verbose. "get-nodes" performs a BFS walk of the tree between two nodes, returning the set of visited nodes: (defn get-nodes [from to] (loop [queue [from] visited #{}] (let [[x & xs] queue] (if-not x visite

Re: Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
Sorry for being unclear. I would in fact like to _create_ an adjacency list, given a start and end key, and the candidates function I mentioned. However, I have problems with coming up with a functional algorithm. I have a stateful one that works, but it's very ugly. (defn build-tree [from to m]

Re: Non-binary tree?

2011-08-19 Thread Justin Kramer
I'm not sure if I'm addressing your needs exactly, but if you want to turn an adjacency list into a nested structure, here's one way: (def adj {:a [:b] :b [:c :d] :c [:e] :d [:e]}) (defn build-tree [adj from to] {:name from :children (when (not= from to) (map #(build-tree adj

Re: Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
Thanks. That looks very interesting. > ;; adjacency list > {:a [:b] :b [:c :d] :c [:e] :d [:e]} For some reason, I have trouble constructing this recursively. I seem to always end up with something like this: user> (build-tree :a :e) (:a (:b (:c (:e)) (:d (:e (defn build-tree [from to] (c

Re: Non-binary tree?

2011-08-19 Thread Justin Kramer
There are many ways one could model a tree/graph: ;; collection of edges [[:a :b] [:b :c] [:b :d] [:c :e] [:d :e]] ;; adjacency list {:a [:b] :b [:c :d] :c [:e] :d [:e]} If you're looking for a pre-made solution, the loom graph library (https://github.com/jkk/loom) may work: (ns example (:us

Non-binary tree?

2011-08-19 Thread Ulrik Sandberg
I have data that I would like to represent as a non-binary tree, or perhaps a graph, but I'm not sure how to do that. The sample data looks like this: A->B B->C,D C->E D->E Here the number of subnodes from B are just two, but they may be any number, which is the source of my

Re: Implementing search algorithm for binary tree

2011-02-23 Thread Alex Osborne
HB writes: > I'm trying to implement searching algorithm for binary tree. > > (defrecord Node [data left-child right-child]) > > (defrecord Tree [root]) > > (defn find-node [tree data] > "Search for a node" > (let [root (:root tree)] > (l

Re: Implementing search algorithm for binary tree

2011-02-22 Thread Ken Wesson
On Tue, Feb 22, 2011 at 8:37 PM, HB 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.getDat

Implementing search algorithm for binary tree

2011-02-22 Thread HB
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())

Re: Binary Tree

2009-06-30 Thread Emeka
can move to the next row however the column index has to be either > increased by one or decreased by one(and the new cell is empty). > > > If you had a balanced binary tree, you could just take log_2(N) where N is > the number of nodes. :) But since you don't you have to do so

Re: Binary Tree

2009-06-30 Thread Daniel Lyons
On Jun 30, 2009, at 3:50 AM, Nicolas Oury wrote: > > If you use depth/size a lot, it might be faster to store them in the > nodes. (it is pseudo code) > > (defstruct bintree :head :left :right :size :depth) > > (defn make-tree [head left right] > (struct bintree head left right >

Re: Binary Tree

2009-06-30 Thread Nicolas Oury
ed by one or decreased by one(and the new cell is > > empty). > > > If you had a balanced binary tree, you could just take log_2(N) where > N is the number of nodes. :) But since you don't you have to do > something like get the max depth of the left branch and t

Re: Binary Tree

2009-06-30 Thread Daniel Lyons
m one cell of any > vector I can move to the next row however the column index has to > be either increased by one or decreased by one(and the new cell is > empty). If you had a balanced binary tree, you could just take log_2(N) where N is the number of nodes. :) But since you

Re: Binary Tree

2009-06-30 Thread Meikel Brandmeyer
Hi, Am 30.06.2009 um 09:05 schrieb Emeka: I have a BinaryTree Nodes that I want to resolve it's depth and the BinaryTree may be unbalanced. How do I do this? Can't just figure it out on my own? It is in the form of vector of vectors, and from one cell of any vector I can move to the next r

Binary Tree

2009-06-30 Thread Emeka
Hello All, I have a BinaryTree Nodes that I want to resolve it's depth and the BinaryTree may be unbalanced. How do I do this? Can't just figure it out on my own? It is in the form of vector of vectors, and from one cell of any vector I can move to the next row however the column index has to be

Re: Alioth binary-tree benchmark

2008-11-18 Thread Michael Wood
On Tue, Nov 18, 2008 at 4:55 PM, mb <[EMAIL PROTECTED]> wrote: [...] > I don't know, whether this is more idiomatic Clojure code, but > it works... [...] What revision is that? On r1099 I got: java.lang.IllegalArgumentException: Don't know how to create ISeq from: Symbol (NO_SOURCE_FILE:31) so

Re: Alioth binary-tree benchmark

2008-11-18 Thread mb
Hi, blindly copying code is usually not a good way to learn a new language I don't know, whether this is more idiomatic Clojure code, but it works... (defn build-tree [item depth] (when (< 0 depth) (let [i (* 2 item) d (dec depth)] [item (build-tree (dec i) d) (build

Re: Alioth binary-tree benchmark

2008-11-18 Thread J. McConnell
On Tue, Nov 18, 2008 at 5:58 AM, Simon Brooke <[EMAIL PROTECTED]> wrote: > > However Giraud uses the Common > LISP ASH (arithmetic shift) function, and, if there's a built-in > function in Clojure, I did not find it; find-doc is your friend in this case: user=> (find-doc "shift") ---

Alioth binary-tree benchmark

2008-11-18 Thread Simon Brooke
As a learning exercise and also to continue to investigate Clojure performance I've roughly translated the Alioth binary-tree benchmark into Clojure. I chose the binary-tree simply because it's the first of the Alioth benchmarks in alphabetical collation; I may do others later. I&#