Defining the TreeNode class

The overall Tree class relies on the TreeNode class to handle the implementation details of adding, removing, and iterating through the various items in the bag. This class is rather large, so we'll present it in four sections.

The first part shows the essential elements of initialization, representation, and how the attributes are made visible:

class TreeNode:    def __init__(        self,        item: Optional[Comparable],        less: Optional["TreeNode"] = None,        more: Optional["TreeNode"] = None,        parent: Optional["TreeNode"] = None,    ) -> None:        self.item = item        self.less = less        self.more = more        if parent:            self.parent = parent    @property    def parent(self) -> Optional["TreeNode"]:        return self.parent_ref()    @parent.setter    def parent(self ...

Get Mastering Object-Oriented Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.