Tree¶

A tree is a data structure for which each element (node) is linked to one parent and several children.¶

Visualisation of a Tree from Wikipedia¶

Implementation¶

The class TreeNode defines a node as: its value, the list of its children¶
A Tree is define by its root TreeNode.¶
In [ ]:
class TreeNode:
    def __init__(self, val, children:list=[]):
        self.val = val
        self.children = children