'i am trying to implement binary search treee in python , insert method not working as expected , right node element are not inserted in tree?

enter code here

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


class binary_tree:

    def __init__(self):
        self.root = None


    def insert(self,data):
        if self.root is None:
            self.root = Node(data)
        else:
            cur_node = self.root
            while cur_node !=None :
                if data < cur_node.data:
                    if cur_node.left is None:
                        cur_node.left = Node(data)
                        print(cur_node.left.data)
                    else:
                        cur_node = cur_node.left
                elif data > cur_node.data:
                    if cur_node.right is None:
                        cur_node.right = Node(data)
                        print(cur_node.right.data)
                    else:
                        cur_node = cur_node.right
                else:
                    print("value already exist")


binary = binary_tree()
b = binary.insert(5)
e = binary.insert(4)
h = binary.insert(7)
print(h)

Q. binary search tree implementation , insert method does not working as expected , inserting node to right side getting error ? how to make this code bug free ? binary search tree implementation .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source