Recursive function going infinite and I can't see why.
Gregory Piñero
gregpinero at gmail.com
Sat Feb 4 12:41:48 EST 2006
Thanks for the advice guys. See below.
On 2/4/06, Steven D'Aprano <steve at removethiscyber.com.au> wrote:
> On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote:
>
> > class Node:
> > def __init__(self):
> > self.arg0=0
> > self.arg1=0
> > self.arg2=0
> > self.arg3=0
>
> You appear to be iterating over these attributes. Hint: as soon as you
> have more than two pieces of data with names like foo0, foo1, foo2...
> you probably want something like this:
>
> def __init__(self):
> self.trees = [0, 0, 0, 0] # they aren't args, they are sub-trees
I'll try this.
> This is quite a complex function. It seems to me, and perhaps I'm wrong,
> you are walking the nodes and ... doing what? When you call this function,
> what are the initial arguments you give it? In other words, what are
> oldnode and newnode, and where do they get set?
>
I posted the whole code, so that should be in there.
> In another post, you wrote:
>
> "By the way, all I'm trying to do here is take two trees, randomly find
> a sub-tree of each and swap the sub-trees. So if anyone has a simple
> method for doing that I'm certainly open to that too."
>
> How about something like this?
>
> random.shuffle(node.trees)
>
> That does more than you ask, but maybe it is useful to you. If you only
> want to swap two, maybe something like this:
>
> a, b = random.randint(0,3), random.randint(0,3)
> node.trees[a], node.trees[b] = node.trees[b], node.trees[a]
>
That's not quite what I want. I want to walk down each tree and get a
random subtree at a random depth.
> I think what you really need to do here is spend some time building a
> general tree class, before getting bogged down in the mechanics of your
> application. For example, it would be really useful to print your Node,
> and all its subtrees, so you can actually confirm that it contains what
> you think it contains.
I had a print method for the tree, but that was making an infinite
recursion too!
> Why is this important? Because if your tree of nodes contains a loop, such
> that node 1 has a subtree with node 2 that has a subtree with node 3 that
> has a subtree with node 1 again, most recursive algorithms will never
> halt, just keep cycling through the tree forever. Could that be your
> problem? There is no way for us to tell from the information we've got.
This sounds like what must be happening. I don't know how else it
could get caught up in an infinite recursion. The problem is, I don't
really know how to tell either ;-0
I guess I need to rethink this whole algorithm. Maybe flatten the
trees somehow and then do the swap? I'll let you guys know what I
come up with.
> --
> Steven.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
More information about the Python-list
mailing list