Write a python class that defines a Tree and add child


Topic: Write a python class that defines a Tree and add child

Solution

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.parent = None
        self.children =[]
    
    def add_child(self, child):
        child.parent = self
        self.children.append(child)
t = TreeNode("Arun")
t.add_child(TreeNode("Shruthi"))



List all Python Programs