Home > OS >  How to access an attribute of different class in python
How to access an attribute of different class in python

Time:01-17

I am trying to form a Binary Search Tree and print it. I am getting the following error:

self.root.left.printtree() AttributeError: 'Node' object has no attribute 'printtree'
class Node:
    def __init__(self,data):
        self.data=data
        self.left=None
        self.right=None

class Binarysearchtree():
    def __init__(self,data):
        self.root=Node(data)
        
    def insert(self,data):
        if self.root.data>data:
            if self.root.left==None:
                self.root.left=Node(data)
            else:
                self.root.left.insert(data)
        elif self.root.data<data:
            if self.root.right==None:
                self.root.right=Node(data)
            else:
                self.root.right.insert(data)
        else:
            self.root.data=data
        
    def printtree(self):
        if self.root.left:
            self.root.left.printtree()
        print(self.root.data)
        if self.root.right:
            self.root.right.printtree()
           
a=Binarysearchtree(23)
a.insert(20)
a.insert(24)
a.printtree()

CodePudding user response:

class Node:

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

    def print_tree(self):
        print(self.data)
        if self.left:
            self.left.print_tree()
        if self.right:
            self.right.print_tree()

class BinarySearchTree:

    def __init__(self, data):
        self.root = Node(data)

    def insert(self,data):
        if self.root.data > data:
            if self.root.left is None:
                self.root.left = Node(data)
            else:
                self.root.left.insert(data)
        elif self.root.data < data:
            if self.root.right is None:
                self.root.right = Node(data)
            else:
                self.root.right.insert(data)
        else:
            self.root.data = data

    def print_tree(self):
        self.root.print_tree()


a = BinarySearchTree(23)

a.insert(20)
a.insert(24)

a.print_tree()

you need to put your print_tree function in the Node class. You also have to change your print_tree function BinarySearchTree and you have to use the print_tree function of Node

Don't forget to respect respect the snake case convention

CodePudding user response:

You can create BST like this

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

def createNode(root_node, data):
    while(root_node != None):
        if data < root_node.data:
            if root_node.left == None:
                root_node.left = Node(data)
                break
            else:
                root_node = root_node.left
        else:
            if root_node.right == None:
                root_node.right = Node(data)
                break
            else:
                root_node = root_node.right


#In order Traversal
def inOrderTravel(r):
    if r == None:
        return
    inOrderTravel(r.left)
    print(r.data)
    inOrderTravel(r.right)


class Binarysearchtree:

    def __init__(self,data):
        self.root = Node(data)
        
    def insert(self,data):
        if data < self.root.data:
            ref_root = self.root
            createNode(ref_root, data)  
        else:
            ref_root = self.root
            createNode(ref_root, data)
        return self.root
        


a = Binarysearchtree(23)
r = a.insert(20)
r = a.insert(24)
r = a.insert(25)
r = a.insert(0)
inOrderTravel(r)
  •  Tags:  
  • Related