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

class binoryTree:
    maxi=float('inf')
    mini=-float('inf')
    def __init__(self):
        self.root=None

    def insert(self,root,data):
        if self.root is None:
            self.root=Node(data)
            print("root data values %s", self.root.data)
        else:
            print("root data values %s", self.root.data)
            if self.root.data>data:
                **if self.root.left is None:
                   self.root.left = Node(data)**
                else:
                    self.insert(self.root.left,data)
            else:   
                if self.root.right is None:
                    self.root.right = Node(data)
                else:
                    self.insert(self.root.right,data)

    def print_tree(self,root):
        if root is None:
            return
        self.print_tree(root.left)
        print("%d",root.data)
        print("----------")
        self.print_tree(root.right)

    def is_bst(self, root, mini, maxi):
        if root is None:
            return 0
        if root.data <mini and root.data > maxi:
            return 0
        return self.is_bst(root.left,mini,root.data) and self.is_bst(root.right,root.data, maxi)

t= binoryTree()
t.insert(t.root,50)
t.insert(t.root,4)
t.insert(t.root,1)
t.insert(t.root,8)
t.insert(t.root,100)
t.print_tree(t.root)

unable to identify the issue in insert function. Not able to understand why it is not able to go in bold section code after inserting first node.

[preeti@oc1238606604 data_structures]

$ /usr/bin/env /usr/bin/python3 /home/preeti/.vscode/extensions/ms-python.python-2020.11.371526539/pythonFiles/lib/python/debugpy/launcher 41717 -- /home/preeti/Desktop/code_playground/data_structures/isBst.py

root data values %s 50 root data values %s 50 root data values %s 50 root data values %s 50 root data values %s 50 root data values %s 50 root data values %s 50

Getting recursion exceed error since it is calling insert again and again.

1
  • You seem to be mixing up root and self.root. Dec 15, 2020 at 6:30

1 Answer 1

1

As pointed out in the comment, you have mixed up the self.root and root. The code is not entering the blocked part because you are checking the value of self.root which is equal to 50 here, but you needed to check the value of root which you were passing as the argument. And a minor issue is that you are using %s or %d but not using formatting, , inside the print prints the value after a space, so you should use something likeprint("root data values %s".format(root.data)) or print("root data values %s"%root.data)

Modified code for insert ->

    def insert(self,root,data):
        if self.root is None:
            self.root=Node(data)
            print("root data values %d"%self.root.data)
        else:
            print("root data values %s"%root.data)
            if root.data>data:
                if root.left is None:
                   root.left = Node(data)
                else:
                    self.insert(root.left,data)
            else:   
                if root.right is None:
                    root.right = Node(data)
                else:
                    self.insert(root.right,data)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.