Best structure for (binary) trees?

R

Rasmus

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
 
A

Aahz

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.
 
A

Alan Kennedy

[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,
 
R

Rasmus

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

R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top