Last active
July 3, 2024 02:51
-
-
Save JadenGeller/a39a6bb32167f616dd494edce6b3b6e6 to your computer and use it in GitHub Desktop.
itertools bfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import zip_longest, chain | |
from dataclasses import dataclass, field | |
@dataclass | |
class Tree: | |
value: any | |
children: list = field(default_factory=list) | |
def levels(self): | |
yield [self] | |
for level in zip_longest(*(c.levels() for c in self.children), fillvalue=()): | |
yield chain.from_iterable(level) | |
def bfs(self): | |
return chain.from_iterable(self.levels()) |
Author
JadenGeller
commented
Jul 3, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment