Connecting Tech Pros Worldwide Help | Site Map

Best structure for (binary) trees?

Rasmus
Guest
 
Posts: n/a
#1: Jul 18 '05
Hi.

As partly novice in python I would like a piece of advise of how to
implement (binary) trees the best way?

Thanks in advance,

Rasmus

PS: Due to heavy spam reception (20.000+/week), I use a fake sender address.
Please answer in the newsgroup. Thanks


Fredrik Lundh
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Best structure for (binary) trees?


"Rasmus" wrote:
[color=blue]
> As partly novice in python I would like a piece of advise of how to
> implement (binary) trees the best way?[/color]

if you're 100% sure you cannot use a standard dictionary, the best
way is probably to get ZODB and use its BTrees module...

code:

http://www.zope.org/Products/ZODB3.2

introduction:

http://zope.org/Wikis/ZODB/FrontPage/guide/node6.html

</F>




Aahz
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Best structure for (binary) trees?


In article <jVGAb.13860$UV7.5376@news.get2net.dk>,
Rasmus <razor-report@daimi.au.dk> wrote:[color=blue]
>
>As partly novice in python I would like a piece of advise of how to
>implement (binary) trees the best way?[/color]

Assuming that nodes also contain data:

class Node:
def __init__(self, data):
self.data = data
self.left = self.right = None

Assuming that you're a CS student looking for homework help, I'll leave
the rest of the implementation as an exercise. You might also consider
looking at the bisect module.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
Alan Kennedy
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Best structure for (binary) trees?


[Rasmus][color=blue]
> As partly novice in python I would like a piece of advise of how to
> implement (binary) trees the best way?[/color]

The original Python Enhancement Proposal (PEP) for Generators (a
recently introduced feature of the python language), PEP-255, contains
a nice example of building and navigating binary trees.

http://www.python.org/peps/pep-0255.html

Search for the text "binary tree class" on that page. That should give
you sufficient sample code to play with.

If you're able to follow the generator/yield -based examples, I'd be
so bold as to say that that is "the best way".

regards,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Rasmus
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Best structure for (binary) trees?


Thanks to all of you.
I have found several good answers of implementing trees from your postings.

R

--

PS: Due to heavy spam reception (20.000+/week), I use a fake sender address.
Please answer in the newsgroup. Thanks

"Rasmus" <razor-report@daimi.au.dk> wrote in message
news:jVGAb.13860$UV7.5376@news.get2net.dk...[color=blue]
> Hi.
>
> As partly novice in python I would like a piece of advise of how to
> implement (binary) trees the best way?
>
> Thanks in advance,
>
> Rasmus
>
> PS: Due to heavy spam reception (20.000+/week), I use a fake sender[/color]
address.[color=blue]
> Please answer in the newsgroup. Thanks
>
>[/color]


Closed Thread