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

  • SEARCH
  • Home
  • 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 9161057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:52:56+00:00 2026-06-17T13:52:56+00:00

This is my code-snippet for a Binary Tree implementation in Python. This WORKS when

  • 0

This is my code-snippet for a Binary Tree implementation in Python. This WORKS when I run the PreOrder function.

class Node:
    def __init__(self,data):
        self.left = None
        self.right = None
        self.data = data

class BinaryTree(Node):
    def __init__(self):
        self.root = None

    def addNode(self,data):
        return Node(data)

    def insert(self,root,data):
        if(root == None):
            root = self.addNode(data)
        else:
            if(data <= root.data):
               root.left = self.insert(root.left,data)
            else:
                root.right = self.insert(root.right,data)
        return root

    def PreOrder(self,root):
        if root == None:
            pass
        else:
            print(root.data)
            self.PreOrder(root.left)
            self.PreOrder(root.right)



a = BinaryTree()
root = a.addNode(2)
#root = None
a.insert(root,4)
a.insert(root,34)
a.insert(root,45)
a.insert(root,46)
a.insert(root,41)
a.insert(root,48)
a.PreOrder(root)

However changing the 2nd and the 3rd lines in main to

#root = a.addNode(2)
root = None

doesn’t print anything. I feel I am missing out on something basic here. Any clarifications will be gratefully appreciated.

  • 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-06-17T13:52:58+00:00Added an answer on June 17, 2026 at 1:52 pm

    You’re passing None to your function, which you defined with:

    if root == None:
        pass
    

    That is why nothing gets printed.

    Also, this is just a personal view, I would actually make PreOrder accept only the self argument, and do the PreOrder from there, making it very simple to define recursively.

    Basically something like:

     def PreOrder(self):
         print self.data 
         if self.left:
              print self.left.PreOrder()
         if self.right:
              print self.right.PreOrder()
    

    But this is a matter of preference, your solution works just fine.

    As blatant promotion, I actually wrote a post about writing a basic BinaryTree in Python recently, if you care to check it out, it can be found here:

    http://intothewebs.tumblr.com/post/40256328302/embrace-the-basics-binary-tree

    UPDATE:

    Ok, after you comment, I understand your doubt.

    The root parameter that is passed into your method isn’t really changed, because params in Python are passed by value:

    How do I pass a variable by reference?

    Read the accepted answer in this question, it’s awesome and should explain what I mean by that.

    Of you have:

    root = None
    a = a.insert(root,4)
    a.insert...
    

    And so forth, your code should work.

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

Sidebar

Related Questions

I'm looking at this code snippet: var addSnippet = function( req, res ) {
I have this code snippet: $(function() { $('.toolbar [id^=button]').on('click', function () { $(this) .css('background-color',
I have this code snippet inside a function that checks if an object exists
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
I found this code snippet to retrieve the title from a YouTube video and
VS 2008 I have this code snippet I found on a VB website. But
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
I am using this code snippet to add KeyDown event handler to any element

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.