[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem traversing a tree

Uwe Schmitt

1/16/2006 7:00:00 PM

Hello,

I'm new to ruby and I tried to implement a simple Tree
class, including tree traversal. The code is:


class Tree

def initialize(id, children)
@id = id
@children = children
end

def traverse_one
puts @id
@children.each do |subtree|
subtree.traverse_one
end
end

def traverse_iter
yield @id
@children.each do |subtree|
subtree.traverse_iter
end
end


end

three = Tree.new(3, [])
four = Tree.new(4, [])
two = Tree.new(2, [three,four])
one = Tree.new(1, [])
root = Tree.new(0,[one, two])

# works fine
root.traverse_one

# raises LocalJumError
root.traverse_iter{ |x| puts x }

As I commented Tree#traverse_one works fine, but Tree#traverse_iter
yields a LocalJumpError.

Any hints ?

Greetings, Uwe.



--
Uwe Schmitt Computer science is no more about Computers,
uwe.schmitt@procoders.net than astronomy is about telescopes. (Dijkstra)
http://www.pro...
6 Answers

konsu

1/16/2006 9:01:00 PM

0

i think it has something to do with passing the block on to the inner
traverse_iter.

konstantin

Dale Martenson

1/16/2006 9:56:00 PM

0

If you change the traverse_iter function to explicitly reference the
block you can pass the block on as part of the recursive function call.

def traverse_iter( &block )
yield @id # or block.call(@id)
@children.each do |subtree|
subtree.traverse_iter( &block )
end
end

Robert Klemme

1/17/2006 9:02:00 AM

0

Uwe Schmitt wrote:
> Hello,
>
> I'm new to ruby and I tried to implement a simple Tree
> class, including tree traversal. The code is:
>
>
> class Tree
>
> def initialize(id, children)
> @id = id
> @children = children
> end
>
> def traverse_one
> puts @id
> @children.each do |subtree|
> subtree.traverse_one
> end
> end
>
> def traverse_iter
> yield @id
> @children.each do |subtree|
> subtree.traverse_iter
> end
> end
>
>
> end
>
> three = Tree.new(3, [])
> four = Tree.new(4, [])
> two = Tree.new(2, [three,four])
> one = Tree.new(1, [])
> root = Tree.new(0,[one, two])
>
> # works fine
> root.traverse_one
>
> # raises LocalJumError
> root.traverse_iter{ |x| puts x }
>
> As I commented Tree#traverse_one works fine, but Tree#traverse_iter
> yields a LocalJumpError.
>
> Any hints ?

I think you got hints regarding your problem already. I'd just like to
additionally mention two things about your design: first, as you did it
"TreeNode" would be a more appropriate name for your class because that's
what it basically is. Second, tree implementations often use at least two
distinct classes - one for tree nodes and one for the tree as a whole.
That way you can better separate concerns (for example, store the size of
the tree, or have algorithms in place that make only sense for a complete
tree). It may not be necessary for your application - just wanted to
mention it.

Kind regards

robert

Uwe Schmitt

1/17/2006 2:34:00 PM

0

||
||
|| I think you got hints regarding your problem already. I'd
|| just like to
|| additionally mention two things about your design: first, as
|| you did it
|| "TreeNode" would be a more appropriate name for your class
|| because that's
|| what it basically is. Second, tree implementations often
|| use at least two
|| distinct classes - one for tree nodes and one for the tree
|| as a whole.
|| That way you can better separate concerns (for example,
|| store the size of
|| the tree, or have algorithms in place that make only sense
|| for a complete
|| tree). It may not be necessary for your application - just wanted to
|| mention it.

I'd like to thank for all contributions. Passing the block to the
recursive call works fine.

In reality I allready use two classes for representing the tree and
nodes. Here I just constructed a small example for provoking the
error message. Posting the full code would have lengthen my posting
too much.

Greetings, Uwe.


Steve

11/29/2012 12:06:00 AM

0

On Wed, 28 Nov 2012 14:24:50 -0800 (PST), robw <noddy093@comcast.net>
wrote:

>On Nov 26, 9:36?pm, Steve <stevencan...@yahooooo.com> wrote:
>> On Mon, 26 Nov 2012 15:59:10 -0800 (PST), robw <noddy...@comcast.net>
>> wrote:
>>
>>
>>
>>
>>
>> >On Nov 26, 5:49?pm, Steve <stevencan...@yahooooo.com> wrote:
>> >> On Mon, 26 Nov 2012 07:25:50 -0800 (PST), wy <w...@myself.com> wrote:
>> >> >On Nov 26, 4:41 am, Steve <stevencan...@yahooooo.com> wrote:
>> >> >> On Sun, 25 Nov 2012 21:10:37 -0600, David Hartung <david@h0tm*il.com>
>> >> >> wrote:
>>
>> >> >> >On 11/25/2012 04:28 AM, Steve wrote:
>> >> >> >> On Sat, 24 Nov 2012 21:59:21 -0600, "NotMe" <m...@privacy.net> wrote:
>>
>> >> >> >>> "Steve" <stevencan...@yahooooo.com> wrote in message
>> >> >> >>>news:3c52b8p4ub736gtlbfctli464m3ilvfgbl@4ax.com...
>> >> >> >>>> On Sat, 24 Nov 2012 11:52:27 -0600, "NotMe" <m...@privacy.net> wrote:
>>
>> >> >> >>>>> "Steve" <
>>
>> >> >> >>>>>>>>>>>>> You do understand that many employers now track their employees
>> >> >> >>>>>>>>>>>>> via
>> >> >> >>>>>>>>>>>>> GPS. What is the difference here?
>>
>> >> >> >>>>>>>>>>>> Someone else is doing it so it must be OK?
>>
>> >> >> >>>>>>>>>>>> Well for one, they make the tracking a condition for a "right" to
>> >> >> >>>>>>>>>>>> an
>> >> >> >>>>>>>>>>>> education.
>>
>> >> >> >>>>>>>>>>>> They intend to broadcast social security numbers?
>>
>> >> >> >>>>>>>>>>>> How do you feel about getting your own RFID? To prevent voter
>> >> >> >>>>>>>>>>>> fraud,
>> >> >> >>>>>>>>>>>> or child abuse, or illegal aliens, or to provide marketing info,
>> >> >> >>>>>>>>>>>> or?
>> >> >> >>>>>>>>>>>> Doubtless someone will come up with some way to make it seem
>> >> >> >>>>>>>>>>>> innocuous
>> >> >> >>>>>>>>>>>> and palatable. What then?
>>
>> >> >> >>>>>>>>>>> It's called a 'cell phone' and is very easily tracked.
>>
>> >> >> >>>>>>>>>> Only to the tower you are hooked up with if one knows how to set the
>> >> >> >>>>>>>>>> phone.
>>
>> >> >> >>>>>>>>> I was directly involved in the industy standards drafting process.
>> >> >> >>>>>>>>> Regardless of what you've been told the devices can be tracked with
>> >> >> >>>>>>>>> considerable ease and accuracy.
>>
>> >> >> >>>>>>>> <LOL> You've been watching too much TV.
>>
>> >> >> >>>>>>> All modern cell phones include GPS chips and can be used for door to
>> >> >> >>>>>>> door navigation without the need for towers.
>>
>> >> >> >>>>>> Indeed, and it can be shut off, hence my reference to how to set the
>> >> >> >>>>>> phone.
>>
>> >> >> >>>>> Some (many, most?) of the current phones cannot be totally disabled with
>> >> >> >>>>> the
>> >> >> >>>>> on/off switch.
>>
>> >> >> >>>> I wasn't even referring to the on/off switch. The GPS can be shut
>> >> >> >>>> down by turning off the location services. With GPS off, it may use
>> >> >> >>>> wifi of cell towers for a general location. Of course, one can shut
>> >> >> >>>> off wifi too. I have no reason for either of those to be on. If one
>> >> >> >>>> imagines that they can be tracked, they can simply carry it in a photo
>> >> >> >>>> film shield bag.
>>
>> >> >> >>> "it can be shut off" if you were not referring to the on/off switch ...
>>
>> >> >> >> I was referring to shutting off the location services, sometimes
>> >> >> >> referred to as the GPS. I'll try to speak slower next time.
>>
>> >> >> >>> The same can be accomplished by leaving the phone home which either case
>> >> >> >>> defeats the purpose of having a cell phone.
>>
>> >> >> >> My phone functions quite nicely with the location service and the WIFI
>> >> >> >> shut off,
>>
>> >> >> >Steve, I am not any sort of electronics engineer, but a number of years
>> >> >> >ago I had the experience of setting up the phones for a show at the
>> >> >> >Memphis convention center. The name of the show was the National
>> >> >> >Technical Investigator's Show. Believe me, what I saw there tells me
>> >> >> >that there is much that these folks can do, that we are not at all aware
>> >> >> >of. It would not surprise me at all if there is a piece of software
>> >> >> >capable of surreptitiously turning on your location services and of
>> >> >> >tracking you without your knowledge.
>>
>> >> >> Of course the technology is available, but if it was happening, you'd
>> >> >> have legitimate technical people discovering it and legitimate news
>> >> >> sources telling us about it and the cell phone manufacturer would be
>> >> >> put out of business because nobody would buy their products... ever
>> >> >> again. It'd be a huge risk for them to take because there are plenty
>> >> >> of people with the tech knowledge to spot it, not to mention a whistle
>> >> >> blower inside the company, and there are plenty of legitimate news
>> >> >> sources eager to report something like that.
>>
>> >> >So if the FBI is using your phone to track your pedophile ways without
>> >> >you knowing about it and they snare you in a trap and haul you off to
>> >> >prison as a result, why would "legitimate" people care about such
>> >> >technology if it helped keep you off the street? ?The last people to
>> >> >suffer would be the cell phone manufacturer, especially since they
>> >> >have nothing to do with whatever software is injected into your phone
>> >> >by any outside parties. ?That'd be like saying GM is responsible for
>> >> >the brakes that failed on your car after someone who hates you
>> >> >tampered with them. ?Boy, you're fundamentally stupid, but keep using
>> >> >that phone of yours and act like no one's snooping, maybe with luck
>> >> >you will end up locked up one day. ?That day can't come to soon.
>>
>> >> It's easy to see that wy doesn't need and cannot afford a cellphone.
>> >> The old frump has no friends or family... ? and probably the reason wy
>> >> doesn't need a car is that he/she is obese and wanders about the
>> >> singlewide in one of those government financed electric wheelchairs.- Hide quoted text -
>>
>> >> - Show quoted text -
>>
>> >your fantasy world is amazing.
>>
>> <LOL> That's from Robbiegirl Westergoon who is still pretending to be
>> married and still claiming to live in Philadelphia... ? when the facts
>> show that he lives alone in an upstairs apartment in Delaware.
>>
>> It's clear that his wife left him and it's likely that he doesn't have
>> a job.- Hide quoted text -
>
>> - Show quoted text -
>
>and you'e come to this........................how?
>addresses over a decade old?
>addresses usd as offices or studios?


You really need to stop the charade, Westergoon. I know your current
address, I know her current address. They are not the same and
neither one is in Philadelphia. You don't have an office and you
don't have a studio. Your apartment is cheap and sort of shabby so I
doubt you have a job.

robw

11/29/2012 12:14:00 AM

0

On Nov 28, 7:05 pm, Steve <stevencan...@yahooooo.com> wrote:
> On Wed, 28 Nov 2012 14:24:50 -0800 (PST), robw <noddy...@comcast.net>
> wrote:
>
>
>
>
>
> >On Nov 26, 9:36 pm, Steve <stevencan...@yahooooo.com> wrote:
> >> On Mon, 26 Nov 2012 15:59:10 -0800 (PST), robw <noddy...@comcast.net>
> >> wrote:
>
> >> >On Nov 26, 5:49 pm, Steve <stevencan...@yahooooo.com> wrote:
> >> >> On Mon, 26 Nov 2012 07:25:50 -0800 (PST), wy <w...@myself.com> wrote:
> >> >> >On Nov 26, 4:41 am, Steve <stevencan...@yahooooo.com> wrote:
> >> >> >> On Sun, 25 Nov 2012 21:10:37 -0600, David Hartung <david@h0tm*il..com>
> >> >> >> wrote:
>
> >> >> >> >On 11/25/2012 04:28 AM, Steve wrote:
> >> >> >> >> On Sat, 24 Nov 2012 21:59:21 -0600, "NotMe" <m...@privacy.net> wrote:
>
> >> >> >> >>> "Steve" <stevencan...@yahooooo.com> wrote in message
> >> >> >> >>>news:3c52b8p4ub736gtlbfctli464m3ilvfgbl@4ax.com...
> >> >> >> >>>> On Sat, 24 Nov 2012 11:52:27 -0600, "NotMe" <m...@privacy.net> wrote:
>
> >> >> >> >>>>> "Steve" <
>
> >> >> >> >>>>>>>>>>>>> You do understand that many employers now track their employees
> >> >> >> >>>>>>>>>>>>> via
> >> >> >> >>>>>>>>>>>>> GPS. What is the difference here?
>
> >> >> >> >>>>>>>>>>>> Someone else is doing it so it must be OK?
>
> >> >> >> >>>>>>>>>>>> Well for one, they make the tracking a condition for a "right" to
> >> >> >> >>>>>>>>>>>> an
> >> >> >> >>>>>>>>>>>> education.
>
> >> >> >> >>>>>>>>>>>> They intend to broadcast social security numbers?
>
> >> >> >> >>>>>>>>>>>> How do you feel about getting your own RFID? To prevent voter
> >> >> >> >>>>>>>>>>>> fraud,
> >> >> >> >>>>>>>>>>>> or child abuse, or illegal aliens, or to provide marketing info,
> >> >> >> >>>>>>>>>>>> or?
> >> >> >> >>>>>>>>>>>> Doubtless someone will come up with some way to make it seem
> >> >> >> >>>>>>>>>>>> innocuous
> >> >> >> >>>>>>>>>>>> and palatable. What then?
>
> >> >> >> >>>>>>>>>>> It's called a 'cell phone' and is very easily tracked.
>
> >> >> >> >>>>>>>>>> Only to the tower you are hooked up with if one knows how to set the
> >> >> >> >>>>>>>>>> phone.
>
> >> >> >> >>>>>>>>> I was directly involved in the industy standards drafting process.
> >> >> >> >>>>>>>>> Regardless of what you've been told the devices can be tracked with
> >> >> >> >>>>>>>>> considerable ease and accuracy.
>
> >> >> >> >>>>>>>> <LOL> You've been watching too much TV.
>
> >> >> >> >>>>>>> All modern cell phones include GPS chips and can be used for door to
> >> >> >> >>>>>>> door navigation without the need for towers.
>
> >> >> >> >>>>>> Indeed, and it can be shut off, hence my reference to how to set the
> >> >> >> >>>>>> phone.
>
> >> >> >> >>>>> Some (many, most?) of the current phones cannot be totally disabled with
> >> >> >> >>>>> the
> >> >> >> >>>>> on/off switch.
>
> >> >> >> >>>> I wasn't even referring to the on/off switch. The GPS can be shut
> >> >> >> >>>> down by turning off the location services. With GPS off, it may use
> >> >> >> >>>> wifi of cell towers for a general location. Of course, one can shut
> >> >> >> >>>> off wifi too. I have no reason for either of those to be on.. If one
> >> >> >> >>>> imagines that they can be tracked, they can simply carry it in a photo
> >> >> >> >>>> film shield bag.
>
> >> >> >> >>> "it can be shut off" if you were not referring to the on/off switch ...
>
> >> >> >> >> I was referring to shutting off the location services, sometimes
> >> >> >> >> referred to as the GPS. I'll try to speak slower next time.
>
> >> >> >> >>> The same can be accomplished by leaving the phone home which either case
> >> >> >> >>> defeats the purpose of having a cell phone.
>
> >> >> >> >> My phone functions quite nicely with the location service and the WIFI
> >> >> >> >> shut off,
>
> >> >> >> >Steve, I am not any sort of electronics engineer, but a number of years
> >> >> >> >ago I had the experience of setting up the phones for a show at the
> >> >> >> >Memphis convention center. The name of the show was the National
> >> >> >> >Technical Investigator's Show. Believe me, what I saw there tells me
> >> >> >> >that there is much that these folks can do, that we are not at all aware
> >> >> >> >of. It would not surprise me at all if there is a piece of software
> >> >> >> >capable of surreptitiously turning on your location services and of
> >> >> >> >tracking you without your knowledge.
>
> >> >> >> Of course the technology is available, but if it was happening, you'd
> >> >> >> have legitimate technical people discovering it and legitimate news
> >> >> >> sources telling us about it and the cell phone manufacturer would be
> >> >> >> put out of business because nobody would buy their products... ever
> >> >> >> again. It'd be a huge risk for them to take because there are plenty
> >> >> >> of people with the tech knowledge to spot it, not to mention a whistle
> >> >> >> blower inside the company, and there are plenty of legitimate news
> >> >> >> sources eager to report something like that.
>
> >> >> >So if the FBI is using your phone to track your pedophile ways without
> >> >> >you knowing about it and they snare you in a trap and haul you off to
> >> >> >prison as a result, why would "legitimate" people care about such
> >> >> >technology if it helped keep you off the street? The last people to
> >> >> >suffer would be the cell phone manufacturer, especially since they
> >> >> >have nothing to do with whatever software is injected into your phone
> >> >> >by any outside parties. That'd be like saying GM is responsible for
> >> >> >the brakes that failed on your car after someone who hates you
> >> >> >tampered with them. Boy, you're fundamentally stupid, but keep using
> >> >> >that phone of yours and act like no one's snooping, maybe with luck
> >> >> >you will end up locked up one day. That day can't come to soon.
>
> >> >> It's easy to see that wy doesn't need and cannot afford a cellphone.
> >> >> The old frump has no friends or family... and probably the reason wy
> >> >> doesn't need a car is that he/she is obese and wanders about the
> >> >> singlewide in one of those government financed electric wheelchairs..- Hide quoted text -
>
> >> >> - Show quoted text -
>
> >> >your fantasy world is amazing.
>
> >> <LOL> That's from Robbiegirl Westergoon who is still pretending to be
> >> married and still claiming to live in Philadelphia... when the facts
> >> show that he lives alone in an upstairs apartment in Delaware.
>
> >> It's clear that his wife left him and it's likely that he doesn't have
> >> a job.- Hide quoted text -
>
> >> - Show quoted text -
>
> >and you'e come to this........................how?
> >addresses over a decade old?
> >addresses usd as offices or studios?
>
> You really need to stop the charade, Westergoon.  I know your current
> address, I know her current address.  They are not the same and
> neither one is in Philadelphia.  You don't have an office and you
> don't have a studio.   Your apartment is cheap and sort of shabby so I
> doubt you have a job.- Hide quoted text -
>
> - Show quoted text -

and my address would be.........................?????