I have a a binary tree, with only self._root and self._nodes. Is there anyway I could figure out how to find a parent tree of the current tree through the constructor?
- If _root is None, then _nodes is empty and _parent_tree is None
- nodes is allowed to contain empty nodes
- if _parent_tree is not empty, then self is in _parent_tree._nodes
def __init__(self, root, nodes):
"""Initialize a new BinaryTree.
@type self: BinaryTree
@type root: object
@type nodes: list[BinaryTree]
self._root = root
self._nodes = nodes
self._parent_tree = None
if self._root is not None:
Following should do good for you :
class BinaryTree: def __init__(self, root, nodes): self._root = root self._nodes = nodes self._parent_tree = None if self._root is not None: for node in self._nodes: if node is not None: node._parent_tree = self else: pass