Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Tree structure - how do we link nodes together?

Reply
Thread Tools

Tree structure - how do we link nodes together?

 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      02-18-2009
I know I could just use rubytree, which looks quite nice, but I'd like
to see what you guys would do about creating a tree and linking the
nodes together.
In C++ we'd just make pointers, so how would we do the equivalent in Ruby?

--Aldric
 
Reply With Quote
 
 
 
 
Dylan Evans
Guest
Posts: n/a
 
      02-18-2009
[Note: parts of this message were removed to make it a legal post.]

I would be inclined to use an array, part of the beauty of dynamic languages
is the typeless nature of arrays which does away with a lot of management
code, in c++ trees i normally write a base node class then branch and leaf
nodes which is a lot of work compared to [x, [y, z]]
Of course if you wanted to create branch nodes for some custom purpose then
you could just assign the children to it since they are passed by reference.

On Wed, Feb 18, 2009 at 11:30 AM, Aldric Giacomoni <"aldric[removeme]"@
trevoke.net> wrote:

> I know I could just use rubytree, which looks quite nice, but I'd like
> to see what you guys would do about creating a tree and linking the
> nodes together.
> In C++ we'd just make pointers, so how would we do the equivalent in Ruby?
>
> --Aldric
>
>



--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-18-2009
2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
> I know I could just use rubytree, which looks quite nice, but I'd like
> to see what you guys would do about creating a tree and linking the
> nodes together.
> In C++ we'd just make pointers, so how would we do the equivalent in Ruby?


We use object references - as always when referring other objects.
Ruby does not have the multitude of options that C++ has.

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end

 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      02-18-2009
Robert Klemme wrote:
> 2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
>
>> I know I could just use rubytree, which looks quite nice, but I'd like
>> to see what you guys would do about creating a tree and linking the
>> nodes together.
>> In C++ we'd just make pointers, so how would we do the equivalent in Ruby?
>>

>
> We use object references - as always when referring other objects.
> Ruby does not have the multitude of options that C++ has.
>
> Cheers
>
> robert
>
>
>

Alright, Robert - I don't know how that works in Ruby! Would you provide
me with a simple example, explain it, or point me to something that
explains it, please?

Thanks,

--Aldric
 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      02-18-2009
Dylan Evans wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> I would be inclined to use an array, part of the beauty of dynamic languages
> is the typeless nature of arrays which does away with a lot of management
> code, in c++ trees i normally write a base node class then branch and leaf
> nodes which is a lot of work compared to [x, [y, z]]
> Of course if you wanted to create branch nodes for some custom purpose then
> you could just assign the children to it since they are passed by reference.
>
>

How would .. passing the children by reference work? Is that the same
thing that Robert is talking about?
The idea of an array is simple enough that it might work.. I should be
able to write something to handle however many layers deep the arrays
go.. But I think a tree may be a little handier to handle things like
deleting a node and its children.
--Aldric
 
Reply With Quote
 
Glen Holcomb
Guest
Posts: n/a
 
      02-18-2009
[Note: parts of this message were removed to make it a legal post.]

On Wed, Feb 18, 2009 at 7:20 AM, Aldric Giacomoni <"aldric[removeme]"@
trevoke.net> wrote:

> Robert Klemme wrote:
> > 2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
> >
> >> I know I could just use rubytree, which looks quite nice, but I'd like
> >> to see what you guys would do about creating a tree and linking the
> >> nodes together.
> >> In C++ we'd just make pointers, so how would we do the equivalent in

> Ruby?
> >>

> >
> > We use object references - as always when referring other objects.
> > Ruby does not have the multitude of options that C++ has.
> >
> > Cheers
> >
> > robert
> >
> >
> >

> Alright, Robert - I don't know how that works in Ruby! Would you provide
> me with a simple example, explain it, or point me to something that
> explains it, please?
>
> Thanks,
>
> --Aldric
>
>

Pretty Simple really:

class Node
attr_accessor :data
attr_accessor :left
attr_accessor :right
end

root = Node.new

node1 = Node.new

root.data = "rob"
node1.data = "tim"
root.right = node1

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      02-18-2009
[Note: parts of this message were removed to make it a legal post.]

On Wed, Feb 18, 2009 at 9:29 AM, Aldric Giacomoni <"aldric[removeme]"@
trevoke.net> wrote:

> Dylan Evans wrote:
> > [Note: parts of this message were removed to make it a legal post.]
> >
> > I would be inclined to use an array, part of the beauty of dynamic

> languages
> > is the typeless nature of arrays which does away with a lot of management
> > code, in c++ trees i normally write a base node class then branch and

> leaf
> > nodes which is a lot of work compared to [x, [y, z]]
> > Of course if you wanted to create branch nodes for some custom purpose

> then
> > you could just assign the children to it since they are passed by

> reference.
> >
> >

> How would .. passing the children by reference work?



Ruby is a uniformly object oriented language, all values are object
references. Variables, and parameters are simply named references to
objects, not the objects themselves. Individual slots in an array also hold
reverences to objects rather than the objects themselves, they just don't
have names.

So everything is passed by object reference. This is not the same thing as
passing by referernce in a language like C, although the differences can be
subtle.

Here's an oldie but goody of mine which might help understand the
relationship between variables, values, and objects.

http://talklikeaduck.denhaven2.com/a...es-and-objects


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-18-2009
On 18.02.2009 15:18, Aldric Giacomoni wrote:
> Robert Klemme wrote:
>> 2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
>>
>>> I know I could just use rubytree, which looks quite nice, but I'd like
>>> to see what you guys would do about creating a tree and linking the
>>> nodes together.
>>> In C++ we'd just make pointers, so how would we do the equivalent in Ruby?
>>>

>> We use object references - as always when referring other objects.
>> Ruby does not have the multitude of options that C++ has.


> Alright, Robert - I don't know how that works in Ruby! Would you provide
> me with a simple example, explain it, or point me to something that
> explains it, please?


I wasn't aware that you were after _such_ basic information. Actually,
since you mentioned using rubytree I assumed that you are familiar with
the language. The simplest and most basic form of a relation between
two objects is probably:

class Foo
def set(x)
@the_other = x
end
end

f = Foo.new
x = Foo.new
f.set(x)

Now f references x.

I suggest you get your hands on David's new book once it is out and in
the meantime consult those various introductory documents (can be found
via http://www.ruby-doc.org/).

Cheers

robert
 
Reply With Quote
 
Michael Malone
Guest
Posts: n/a
 
      02-18-2009
Robert Klemme wrote:
> On 18.02.2009 15:18, Aldric Giacomoni wrote:
>> Robert Klemme wrote:
>>> 2009/2/18 Aldric Giacomoni <"aldric[removeme]"@trevoke.net>:
>>>
>>>> I know I could just use rubytree, which looks quite nice, but I'd like
>>>> to see what you guys would do about creating a tree and linking the
>>>> nodes together.
>>>> In C++ we'd just make pointers, so how would we do the equivalent
>>>> in Ruby?
>>>>
>>> We use object references - as always when referring other objects.
>>> Ruby does not have the multitude of options that C++ has.

>
>> Alright, Robert - I don't know how that works in Ruby! Would you provide
>> me with a simple example, explain it, or point me to something that
>> explains it, please?

>
> I wasn't aware that you were after _such_ basic information.
> Actually, since you mentioned using rubytree I assumed that you are
> familiar with the language. The simplest and most basic form of a
> relation between two objects is probably:
>
> class Foo
> def set(x)
> @the_other = x
> end
> end
>
> f = Foo.new
> x = Foo.new
> f.set(x)
>
> Now f references x.
>
> I suggest you get your hands on David's new book once it is out and in
> the meantime consult those various introductory documents (can be
> found via http://www.ruby-doc.org/).
>
> Cheers
>
> robert
>

Essentially, the main information you require is that, everything in
Ruby is an object, and every object is actually a reference to an object.

so
class Foo

attr_reader :the_other

def set(x)
@the_other = x
end
end

f = Foo.new
bar = f
bar.the_other => nil
x = Foo.new
f.set(x)
bar.the_other => x

It helps me to think of references and the objects they point to as
separate entities (I'm unsure of the truthfulness of this). It's the
opposite of c++ really. c++ requires you to explicitly state you want
pass-by-reference and Ruby requires you to state pass-by-value (which
ends up in the form of a .dup call all the times I've ever wanted to use
it, which is less often than you might think)

Cheers,
Michael

================================================== =====================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
================================================== =====================


 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      02-18-2009
[Note: parts of this message were removed to make it a legal post.]

On Wed, Feb 18, 2009 at 5:08 PM, Michael Malone
<(E-Mail Removed)>wrote:

> Essentially, the main information you require is that, everything in Ruby
> is an object, and every object is actually a reference to an object.
>


Actually, no.

The value of any expression is an object.

Every object consists of references to objects.

Variables and parameters are named references to objects.

Assignment transfers the reference to the object, not the object itself.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 2 10-31-2007 02:58 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 1 10-30-2007 11:01 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 4 10-30-2007 08:21 PM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments