Search code examples
python-3.xoopdata-structuresbinary-treebinary-search-tree

Binary Tree implementation with Separate class for node and tree


I am new to Python and data structures. While learning binary tree, I found all the available implementations have the methods of the tree inside the node class.

But I want to implement it differently, where along with the Node class, there will be a Binary tree class, which will contain the methods of the Binary tree.

This is the code I came up with, but it raises an error with the arguments I am passing to the functions insertleft() and insertright().

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

class Bt():
    def __init__(self, root):
        self.root = root

    def insertleft(temp.left, data):    
        if (temp.left == None):
            temp.left.data = data
        elif(data<temp.left.data):
            t1 = temp.left
            insertleft(t1.left, data)
        elif(data>temp.left.data):
            t1 = temp.left
            insertright(t1.left, data)

    def insertright(temp.right, data):  
        if (temp.right == None):
            temp.right.data = data
        elif(data<temp.right.data):
            t1 = temp.right
            insertleft(t1.right, data)
        elif(data>temp.right.data):
            t1 = temp.right
            insertright(t1.right, data)


    def insert(self, data):
        temp = self.root
        if(temp.data = None):
            temp.data = data
        elif(data<temp.data):
            insertleft(temp.left, data)
        elif(data>temp.data):
            insertright(temp.right, data)


r = Bt()
r.root = Node(5)
r.insert(4)
r.insert(6)

These is the error I received:

def insertleft(temp.left, data):  
                      ^
SyntaxError: invalid syntax

I am not sure what the right syntax should be. Thanks in advance for your help


Solution

  • When you define a function, you also define the parameters you will be accepting. Inside a class though, as long as the method is not static or a class method, the first parameter should be self or a variable that represents the instanciated object itself.

    In your case, you are already passing the value of temp.left and temp.right when calling the function. Changing the function definitions to just use temp should work just fine. Or even more readible, node since that is what you are actually referring to.

    Your insertleft and insertright functions do the same thing as insert, and including them is not necessary. Just modify the insert function to do it all.

    Here's what the end result should resemble:

    class Node():
        def __init__(self, arg):
            self.left = None
            self.right = None
            self.data = arg
    
    class Bt():
        def __init__(self, root=None):  #Added default value
            self.root = Node(root)
    
        def insert(self, data):
            if self.root.data is None:
                self.root.data = data   #Set the root data if it doesn't exist.
            else:
                self._insert(self.root, data)   #Prime the helper function with root node
    
        def _insert(self, node, data):    #Insert helper function to do the insertion
            if data < node.data:    #Check if data needs to be inserted on the left
                if node.left is None:   #Set left node if it doesn't exist
                    node.left = Node(data)
                else:                   #Else let the left node decide where it goes as a child
                    self._insert(node.left, data)
            else:                   #Else data needs to be inserted on the right
                if node.right is None:  #Set right node if it doesn't exist
                    node.right = Node(data)
                else:                   #Else let the right node decide where it goes as a child
                    self._insert(node.right, data)
    
    
    r = Bt(5)
    r.insert(4)
    r.insert(6)
    

    Now you're just missing functions to print the Tree and Data... and accessing the data.