# Tree class

**URL:** <https://www.ruby-forum.com/t/tree-class/68261>\
**Category:** Ruby\
**Created:** [November 25, 2005, 1:57pm UTC](https://www.ruby-forum.com/t/tree-class/68261 "2005-11-25T13:57:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 1:57pm UTC](https://www.ruby-forum.com/t/tree-class/68261/1 "2005-11-25T13:57:23Z")

</div>

Hi,

Is there a tree class in the language or a good lib out there ?  
Thanks for your suggestions.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 2:05pm UTC](https://www.ruby-forum.com/t/tree-class/68261/2 "2005-11-25T14:05:26Z")

</div>

Mickael Faivre-Macon wrote:

> Hi,
> 
> Is there a tree class in the language or a good lib out there ?  
> Thanks for your suggestions.

[http://raa.ruby-lang.org/search.rhtml?search=tree](http://raa.ruby-lang.org/search.rhtml?search=tree)

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 2:13pm UTC](https://www.ruby-forum.com/t/tree-class/68261/3 "2005-11-25T14:13:29Z")

</div>

Hi Robert,

Thanks for your reply.

What would be the reference class for Ruby ?  
For example it is said tat REXML is the reference for managing XML.  
Is there such a librairy for trees ?

A lot of lib in the RAA are too specialized (binary trees, avl,  
redblack trees, etc)  
I am searching for a general tree class.

Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 2:41pm UTC](https://www.ruby-forum.com/t/tree-class/68261/4 "2005-11-25T14:41:45Z")

</div>

On 25/11/05, Mickael Faivre-Macon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am searching for a general tree class.
> 
> > [http://raa.ruby-lang.org/search.rhtml?search=tree](http://raa.ruby-lang.org/search.rhtml?search=tree)
> > 
> > Kind regards
> > 
> > ```
> > robert
> > 
> > ```

The question should be what you want to do with your tree. You should  
pick a datastructure suitable for your problem, there is no tree per  
se. It depends on which pointers you need and which structure your  
tree should have.

If you just want nodes with children try this “reference implementation”

* * *

class Node  
attr\_reader :children

## def initialize @children = [] end end

which may or may not suit your needs.

Cheers,

Brian

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 7:20pm UTC](https://www.ruby-forum.com/t/tree-class/68261/5 "2005-11-25T19:20:45Z")

</div>

On 25/11/05, Mickael Faivre-Macon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > * * *
> > 
> > which may or may not suit your needs.
> 
> Sure, this is a good start. I can start by using this.  
> I was wondering if I had to reinvent the wheel or not.  
> I guess I have to.
> 
> Mickael.

What I wanted to say, was that most probably you do not have to  
reinvent the wheel if you are more specific about your application.  
What do you want to achieve: Find elements in a set fast, Have a  
datastructure where you can access and delete the smallest element in  
a fast way, build an index into a sorted array that allows for fast  
range searches, draw a natural tree with real leaves onto the computer  
monitor, structure a decision diagram, index a text, …

Depending on what you want to achieve a specialized datastructure is  
usefull, but there is no datastructure that can do all things equally  
well.

hope to help,

Brian

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [November 25, 2005, 7:04pm UTC](https://www.ruby-forum.com/t/tree-class/68261/6 "2005-11-25T19:04:37Z")

</div>

Hi Brian,

> The question should be what you want to do with your tree. You should  
> pick a datastructure suitable for your problem, there is no tree per  
> se. It depends on which pointers you need and which structure your  
> tree should have.

My question was simple, the concept of tree is simple too, I don’t  
want to over complicate the problem. I must assume that there is no  
generic tree implementation in Ruby avalaible right now. Okay.

> * * *
> 
> which may or may not suit your needs.

Sure, this is a good start. I can start by using this.  
I was wondering if I had to reinvent the wheel or not.  
I guess I have to.

Mickael.
