Thursday, October 19, 2017

Binary tree creation and sorting with inorder traversal using python

class node:

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



    def insert(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


def check_inorder(root):
    if(root is not None ):
        check_inorder(root.left)
        print (root.data)
        check_inorder(root.right)


def main():


    root = node(8)
    root.insert(5)
    root.insert(10)
    root.insert(1)


    check_inorder(root)




main()

No comments:

Post a Comment