Python DSA

Trees

Tree data structure

Trees

A tree is a hierarchical data structure with a root value and subtrees of children.

Tree Node

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.children = []

root = TreeNode('A')
root.children.append(TreeNode('B'))
root.children.append(TreeNode('C'))