Created
September 24, 2010 15:38
-
-
Save thanos/595562 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Node: | |
""" | |
>>> Node(1).append( | |
... Node(2)).append( | |
... Node(3)).append( | |
... Node(4)).append( | |
... Node(5)).append( | |
... Node(6)).append( | |
... Node(7)).append( | |
... Node(8)).pprint() | |
1 | |
2 | |
5 | |
8 | |
3 | |
6 | |
4 | |
7 | |
>>> | |
""" | |
MAX = 3 | |
def __init__(self, value): | |
self.value = value | |
self.children = [] | |
def append(self, node): | |
if len(self.children) < self.MAX: | |
self.children.append(node) | |
else: | |
_, indx = min(zip(map(len, self.children),xrange(len(self.children)))) | |
self.children[indx].append(node) | |
return self | |
def __len__(self): | |
return len(self.children) | |
def pprint(self, indent=0): | |
print " " * indent, self.value | |
for child in self.children: | |
child.pprint(indent+1) | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment