[Tutor] building nonbinary trees
Vincent Wan
wan at walrus.us
Fri Nov 18 02:20:33 CET 2005
I would like to have a tree data structure with trees whose nodes
could have an arbitrary
number of descendants and that I could iterate over by the names of
nodes
I'v got a start but I'm stuck because I don't understand how to call
a tree's nodes by name.
Here is the code I have so far:
import random
# constants that control the simulation
NUMBER_REPS = 10 # run's the simulation
MAX_LINAGES = 10 # number of species in each run
BRANCHING_PROBABILITY = 0.5
class Tree(object):
numLinages = 0
class Node(object):
def __init__(self, name):
self.name = name
self.alive = True
self.descendents = []
Tree.numLinages += 1
def __init__(self):
self.rootnode = Tree.Node(0)
def AddBranch(self, linage, offspring):
new = Tree.Node(offspring)
self.descendents.append(new)
def NumLinages( ):
return Tree.numLinages
NumLinages = staticmethod(NumLinages)
currentTree = Tree()
print Tree.NumLinages()
for i in range(NUMBER_REPS):
j = 0
while j <= currentTree.NumLinages():
if random.random() >= BRANCHING_PROBABILITY:
currentTree.AddBranch(j, currentTree.NumLinages() + 1)
j += 1
thank you,
Vincent Wan
------------------------------------------------------------------------
--------------
PhD Candidate
Committee on the Conceptual and Historical Studies of Science
University of Chicago
PO Box 73727
Fairbanks, AK 99707
wan AT walrus DOT us (change CAPS to @ and . )
More information about the Tutor
mailing list