File size: 660 Bytes
b6130a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from typing import NamedTuple, TypeVar, Callable
A = TypeVar("A")
R = TypeVar("R")
Branch = Callable[[R, R], R]
class BinaryTree(NamedTuple):
constructor: Callable[[R, Branch[R]], R]
def __call__(self, leaf: R, branch: Branch[R]):
return self.constructor(leaf, branch)
@classmethod
def leaf(cls):
def leafer(leaf: R, branch: Branch[R]) -> R:
return leaf
return cls(leafer)
@classmethod
def branch(cls, left, right):
def brancher(leaf: R, branch: Branch[R]) -> R:
return branch(left, right)
return cls(brancher)
class BT(NamedTuple):
left: "BT"
right: "BT" |