Best structure for (binary) trees?

Discussion in 'Python' started by Rasmus, Dec 7, 2003.

  1. Rasmus

    Rasmus Guest

    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
    Rasmus, Dec 7, 2003
    #1
    1. Advertising

  2. Fredrik Lundh, Dec 7, 2003
    #2
    1. Advertising

  3. Rasmus

    Aahz Guest

    In article <jVGAb.13860$2net.dk>,
    Rasmus <> 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 () <*> 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.
    Aahz, Dec 7, 2003
    #3
  4. Rasmus

    Alan Kennedy Guest

    [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
    Alan Kennedy, Dec 7, 2003
    #4
  5. Rasmus

    Rasmus Guest

    Thanks: 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" <> wrote in message
    news:jVGAb.13860$2net.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
    >
    >
    Rasmus, Dec 7, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jova

    Binary Trees

    jova, Apr 25, 2004, in forum: Java
    Replies:
    11
    Views:
    722
    Roedy Green
    Apr 26, 2004
  2. Will Oram

    Non-Binary Trees

    Will Oram, Oct 27, 2003, in forum: C++
    Replies:
    3
    Views:
    698
    Stewart Gordon
    Oct 28, 2003
  3. JC

    Binary trees

    JC, Dec 8, 2003, in forum: C++
    Replies:
    6
    Views:
    579
    osmium
    Dec 10, 2003
  4. jova

    Binary Trees

    jova, Apr 25, 2004, in forum: C++
    Replies:
    11
    Views:
    756
    Roedy Green
    Apr 26, 2004
  5. jacob navia

    Binary search trees (AVL trees)

    jacob navia, Jan 3, 2010, in forum: C Programming
    Replies:
    34
    Views:
    1,391
    Dann Corbit
    Jan 8, 2010
Loading...

Share This Page