Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:39:03+00:00 2026-05-15T10:39:03+00:00

how do i represent binary search trees in python?

  • 0

how do i represent binary search trees in python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:39:03+00:00Added an answer on May 15, 2026 at 10:39 am
    class Node(object):
    
      def __init__(self, payload):
        self.payload = payload
        self.left = self.right = 0
    
        # this concludes the "how to represent" asked in the question.  Once you
        # represent a BST tree like this, you can of course add a variety of
        # methods to modify it, "walk" over it, and so forth, such as:
    
      def insert(self, othernode):
        "Insert Node `othernode` under Node `self`."
        if self.payload <= othernode.payload:
          if self.left: self.left.insert(othernode)
          else: self.left = othernode
        else:
          if self.right: self.right.insert(othernode)
          else: self.right = othernode
    
      def inorderwalk(self):
        "Yield this Node and all under it in increasing-payload order."
        if self.left:
          for x in self.left.inorderwalk(): yield x
        yield self
        if self.right:
          for x in self.right.inorderwalk(): yield x
    
      def sillywalk(self):
        "Tiny, silly subset of `inorderwalk` functionality as requested."
        if self.left:
          self.left.sillywalk()
        print(self.payload)
        if self.right:
          self.right.sillywalk()
    

    etc, etc — basically like in any other language which uses references rather than pointers (such as Java, C#, etc).

    Edit:

    Of course, the very existence of sillywalk is silly indeed, because exactly the same functionality is a singe-liner external snippet on top of the walk method:

    for x in tree.walk(): print(x.payload)
    

    and with walk you can obtain just about any other functionality on the nodes-in-order stream, while, with sillywalk, you can obtain just about diddly-squat. But, hey, the OP says yield is “intimidating” (I wonder how many of Python 2.6’s other 30 keywords deserve such scare words in the OP’s judgment?-) so I’m hoping print isn’t!

    This is all completely beyond the actual question, on representing BSTs: that question is entirely answered in the __init__ — a payload attribute to hold the node’s payload, left and right attribute to hold either None (meaning, this node has no descendants on that side) or a Node (the top of the sub-tree of descendants on the appropriate side). Of course, the BST constraint is that every left descendant of each node (if any) has a payload less or equal than that of the node in question, every right one (again, if any) has a greater payload — I added insert just to show how trivial it is to maintain that constraint, walk (and now sillywalk) to show how trivial it is to get all nodes in increasing order of payloads. Again, the general idea is just identical to the way you’d represent a BST in any language which uses references rather than pointers, like, for example, C# and Java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the problem of counting the number of structurally distinct binary search trees :
If I wanted to represent states or options or something similar using binary flags
The objective of skeletonization is to represent a binary image with a minimum set
Given four binary vectors which represent classes: [1,0,0,0,0,0,0,0,0,0] [0,0,0,0,0,0,0,0,0,1] [0,1,1,1,1,1,1,1,1,0] [0,1,0,0,0,0,0,0,0,0] What methods are
I need to represent a 40-bit binary number. Which C datatype should be used
I am writing an immutable binary search tree in c++. My terminating nodes are
How do you express an integer as a binary number with Python literals? I
So I was implementing my own binary search tree and noticed that an ugly
Lets Take Binary search for instance, The best case running time would be obtained
This question is related to this post : int to binary python I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.