By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Best structure for (binary) trees?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Rasmus" wrote:
As partly novice in python I would like a piece of advise of how to
implement (binary) trees the best way?


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>


Jul 18 '05 #2

P: n/a
In article <jV******************@news.get2net.dk>,
Rasmus <ra**********@daimi.au.dk> wrote:

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


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 (aa**@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.
Jul 18 '05 #3

P: n/a
[Rasmus]
As partly novice in python I would like a piece of advise of how to
implement (binary) trees the best way?


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
Jul 18 '05 #4

P: n/a
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" <ra**********@daimi.au.dk> wrote in message
news:jV******************@news.get2net.dk...
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.