Arpit Agrawal
Arpit Agrawal

Reputation: 3

AttributeError: 'int' object has no attribute 'data'

I was trying to implement some basic operations of Binary search tree in Python3.7. I have just started coding problems from Hackerrank and I got stuck while implementing levelOrder traversal in BST. What should I do to resolve this AttributeError: 'int' object has no attribute 'data'?

I am using queue to solve this problem, initially, the queue will point to root node, then checks for left and right children. If found, the children will get append to the queue and the process continues until the queue is empty. I am popping out the first element in each iteration and storing it in temp variable. This way i will get nodes at each level of tree.

class Node:
    def __init__(self, data):
        self.data = data
        self.leftChild = None
        self.rightChild = None

    def insert(self, data):
        if self.data is not None:
            if data < self.data:
                if self.leftChild is None:
                    self.leftChild = Node(data)
                else:
                    self.leftChild.insert(data)
            elif data > self.data:
                if self.rightChild is None:
                    self.rightChild = Node(data)
                else:
                    self.rightChild.insert(data)
        else:
            self.data = data

    def traverseLevelOrder(self):
        queue = []
        queue.append(self.data)
        while queue:
            # Print front of queue and remove it from queue
            print(queue[0].data)
            temp = queue.pop(0)

            # Enqueue left child
            if temp.leftChild is not None:
                queue.append(temp.leftChild)

            # Enqueue right child
            if temp.rightChild is not None:
                queue.append(temp.rightChild)


class BST:
    def __init__(self):
        self.rootNode = None

    def insert(self, data):
        if self.rootNode is None:
            self.rootNode = Node(data)
        else:
            self.rootNode.insert(data)

    def traverseLevelOrder(self):
        if self.rootNode is None:
            return
        else:
            self.rootNode.traverseLevelOrder()


bst = BST()
bst.insert(2)
bst.insert(4)
bst.insert(1)
bst.insert(3)
bst.traverseLevelOrder()

The code should return the level traversal order like one given below(within a level it should print first left node then right node):

2
1
4
3

Instead, I am having the below error:

Traceback (most recent call last):
    print(queue[0].data)
AttributeError: 'int' object has no attribute 'data'

Upvotes: 0

Views: 8304

Answers (2)

melpomene
melpomene

Reputation: 85757

queue.append(self.data)

Did you mean:

queue.append(self)

?

Right now you're only adding a number to the queue, not the whole object.

Upvotes: 0

ggorlen
ggorlen

Reputation: 56855

You're appending an integer, self.data to the queue, then attempting to access a property on the integer with queue[0].data, causing the AttributeError to be raised.

Instead, append the node itself with:

queue.append(self)

Upvotes: 1

Related Questions