1

I am new to Python and came across an old problem in HackerRank which defines a Binary Tree as such. I reviewed this video on classes and instances (and the next one) to try to understand what is happening in the below code but I still don't fully grasp what is going on.

  • I understand the role of the __ init __(self, ...) but I'm not sure what attribute info has. I also do not understand why self.left = None, self.right = None, self.level = None.
  • In the second class BinarySearchTree, there's an init with no attribute and I also do not understand the self.root = None.

Even though I don't understand most of the code below, I think if someone can explain why the person set self.____= None, it would help me understand how to define a Binary Search Tree.

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

    def __str__(self):
        return str(self.info) 

class BinarySearchTree:
    def __init__(self): 
        self.root = None

    def create(self, val):  
        if self.root == None:
            self.root = Node(val)
        else:
            current = self.root

            while True:
                if val < current.info:
                    if current.left:
                        current = current.left
                    else:
                        current.left = Node(val)
                        break
                elif val > current.info:
                    if current.right:
                        current = current.right
                    else:
                        current.right = Node(val)
                        break
                else:
                    break

1 Answer 1

2

If you try sketch your tree structure as a bunch of circles with some values inside, you will get something like that:

Binary tree

The 'info' attribute will contain the values that are inside of the circles. Every node of a binary tree can have at most two children, that's what the 'left' and 'right' attributes are used for. If the 'left' attribute is 'None', it basically means there is no child node on the left side yet (like in case of the node 16 on the image). If you create a new node, you usually do not expect it to have any children, that's why the 'left' and 'right' attributes are 'None' by default.

The class 'BinarySearchTree' represents a tree as a whole and keeps the current root node (the top one on the image) in the corresponding 'root' attribute. At the beginning the tree is empty, so the 'root' attribute equals to 'None'.

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

I think I understand what you wrote. When I run the code without anything like print(2), I get AttributeError: 'NoneType' object has no attribute 'info'. but when I print(root.info) I still get the correct value of the root with the same error above. Is there a problem with the way the class is defined?
Not sure what you mean with print(2), but it looks like you are trying to access the attribute of a class instance, that was not created yet. Try following: print(root.info if root is not None else "N/A").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.