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 894789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:27:12+00:00 2026-05-15T14:27:12+00:00

class Node: ”’represents a new node in the BST”’ def __init__(self,key): self.key=key self.disconnect() def

  • 0
class Node:
    '''represents a new node in the BST'''
    def __init__(self,key):
        self.key=key
        self.disconnect()
    def disconnect(self):
        self.left=None;
        self.right=None;
        self.parent=None;
    def __str__(self):
        return 'node with kay %s'%self.key

class BST:
    def __init__(self):
        self.root=None
    def insert(self,t):
        '''inserts a new element into the tree'''
        self.find_place(self.root,t)

    def find_place(self,node,key):
        """finds the right place of the element recursively"""
        if node is None:
            node=Node(key)
            print node
        else:
            if node.key > key:
                find_place(node.left,key)
            else:
                find_place(node.right,key)
def test():
    '''function to test if the BST is working correctly'''

i wrote the above code to implement a binary search tree but the insert method is not working as expected , it fails to add even the root element . i can’t undestand the cause.

  • 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-15T14:27:13+00:00Added an answer on May 15, 2026 at 2:27 pm

    You’re not actually adding any nodes to the tree!

    Its easiest to manage the adding of the root node explicitly, as you see I did below in the insert.

    A find_place function would, presumably from the name, return the parent node and also whether it’s the left or right slot for the key? I’ve made an explicit _do_insert function below that both walks and does the insert.

    From then on, you need to walk the tree, each time seeing if you recurse down a branch or whether you’ve reached an empty slot, where you add the new node.

    It might be natural to refactor your code to put responsibility for walking the tree (and doing adds, removes and such) into the Node class.

    In the code below, I ignore adding a key that is already in the tree, I just silently exit:

    def insert(self,t):
        '''inserts a new element into the tree'''
        if self.root is None:
            self.root = Node(t)
        else:
            self._do_insert(self.root,t)
    
    def _do_insert(self,parent,t):
        if t > parent.key:
            if parent.left is None:
                parent.left = Node(t)
            else:
                self._do_insert(parent.left,t)
        elif t < parent.key:
            if parent.right is None:
                parent.right = Node(t)
            else:
                self._do_insert(parent.right,t)
        else:
            # raise a KeyError or something appropriate?
            pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think I figured out a way to do this.… May 16, 2026 at 7:11 am
  • Editorial Team
    Editorial Team added an answer If you captured the ID of your currently displayed post,… May 16, 2026 at 7:11 am
  • Editorial Team
    Editorial Team added an answer I have no experience with this but Google turned up… May 16, 2026 at 7:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.