[Python-de] Hilfe im "Baum" aufbauen...
Simon Pamies
lists at bipbap.de
Mon Okt 13 15:31:09 CEST 2003
>class node:
> def __init__(self, id, parent = None):
> self.id = id
> self.parent = parent # Merke fuer jedes node-Objekt id und parent
> self.children = []
> if parent:
> parent.children.append(self)
>
>
>
root = node('root')
left = node('left',root)
right = node('right', root)
left_child = node('child_left', left)
right_child = node('child_right', right)
...
Zugriff auf left_child:
root.children[0].children[0]
Das geht aber schöner, wenn man dictionaries benutzt und
so ein paar Algorithmen benutzt ("Ausgewogene Bäume")
Das was du da hast erinnert ein bischen an doppelt verkettete Listen.
Schau dir mal
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/136529
und
ftp://squirl.nightmare.com/pub/python/python-ext/avl/
an.
Gruss
Simon