0

I'm trying to implement Binary Tree insert method with recursive calls. When i run insert function it gives me an AtributeError : BinTree instance has no atribute 'root. Can you tell me pls how I can fix it??

from random import randint
class Node:
    def __init__(self,value):
        self.value = value 
        self.left = None
        self.right = None

class BinTree:
    def __init___(self):
        self.root = None
    def insert(self, value):
        if self.root == None:
            self.root = Node(value)
        else:
            self._insert(value, self.root)
    def _insert(self, value, curr_node):
        if value < curr_node.value:
            if curr_node.left == None:
                curr_node.left = Node(value)
            else:
                self._insert(value, curr_node.left)
        elif value>curr_node.value:
            if curr_node.right == None:
                curr_node.right = Node(value)
            else:
                self._insert(value, curr_node.rigth)

        else:
            print('Node already exist!')
    def printTree(self):
        if self.root != None:
            self._printTree(self.root)
    def _printTree(self, curr_node):
        if curr_node !=None:
            self._printTree(curr_node.left)
            print(str(curr_node.val))
            self._printTree(curr_node.right)

def fillTree(tree):
    for _ in range(100):
        curr_elem = randint(0,50)
        tree.insert(curr_elem)
    return tree
tree = BinTree()
fillTree(tree)
tree.printTree()

2 Answers 2

1

Your __init__ has a typo in the name. There are 3 trailing underscores. Change it to:

...
class BinTree:
    def __init__(self):
        self.root = None

And your code should work

Edit: found a couple of more issues. Here's the complete fixed code:

from random import randint
class Node:
    def __init__(self,value):
        self.value = value
        self.left = None
        self.right = None

class BinTree:
    def __init__(self):
        self.root = None
    def insert(self, value):
        if self.root == None:
            self.root = Node(value)
        else:
            self._insert(value, self.root)
    def _insert(self, value, curr_node):
        if value < curr_node.value:
            if curr_node.left == None:
                curr_node.left = Node(value)
            else:
                self._insert(value, curr_node.left)
        elif value>curr_node.value:
            if curr_node.right == None:
                curr_node.right = Node(value)
            else:
                self._insert(value, curr_node.right)

        else:
            print('Node already exist!')
    def printTree(self):
        if self.root != None:
            self._printTree(self.root)
    def _printTree(self, curr_node):
        if curr_node !=None:
            self._printTree(curr_node.left)
            print(str(curr_node.value))
            self._printTree(curr_node.right)

def fillTree(tree):
    for _ in range(100):
        curr_elem = randint(0,50)
        tree.insert(curr_elem)
    return tree
tree = BinTree()
fillTree(tree)
tree.printTree()
Sign up to request clarification or add additional context in comments.

Comments

0

Only BinTrees have roots; the children of a BinTree are Nodes, not BinTrees and thus don't have roots.

1 Comment

Hm Thanks but I'm still lost in my code.. What should i Change here ??

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.