Binary Search Tree in Python

This is a basic implementation of BINARY SEARCH TREE with insertion and traversal(in-order).

Note: This code will be extended to include a complete implementation with additional features

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data

    def insert(self, data):
# Compare the new value with the parent node
        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the tree
    def PrintTree(self):   #In-order traversal
        print( self.data),
        
        if self.left:
            self.left.PrintTree()
        
        if self.right:
            self.right.PrintTree()

# Use the insert method to add nodes
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)
root.insert(-1)
root.insert(0)

root.PrintTree()

Leave a Reply

Your email address will not be published. Required fields are marked *