Best structure for (binary) trees?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rasmus

    Best structure for (binary) trees?

    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

    #2
    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:



    introduction:



    </F>




    Comment

    • Aahz

      #3
      Re: Best structure for (binary) trees?

      In article <jVGAb.13860$UV 7.5376@news.get 2net.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@pythoncra ft.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.

      Comment

      • Alan Kennedy

        #4
        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.

        This PEP introduces the concept of generators to Python, as well as a new statement used in conjunction with them, the yield statement.


        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

        Comment

        • Rasmus

          #5
          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" <razor-report@daimi.au .dk> wrote in message
          news:jVGAb.1386 0$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]


          Comment

          Working...