Skip to content

Instantly share code, notes, and snippets.

@gamesguru
Created January 15, 2021 18:19
Show Gist options
  • Save gamesguru/fb85b4bcf84cea6050c851f2af64e688 to your computer and use it in GitHub Desktop.
Save gamesguru/fb85b4bcf84cea6050c851f2af64e688 to your computer and use it in GitHub Desktop.
Python printing: family-tree
class Node:
def __init__(self, parent_id, id, name):
self.id = id
self.parent_id = parent_id
self.name = name
self.children = []
def printChildren(node, level=1):
for c in node.children:
padding = " " * level
print(padding + c.name + " (" + str(c.id) + ")")
# Recursive call
printChildren(c, level + 1)
def printTree(s):
people = [x.split(",") for x in s.split("|")]
people = [Node(int(x[0].replace("null", "-1")), int(x[1]), x[2]) for x in people]
people = {x.id: x for x in people}
# Build children lists
for p in people.values():
p_id = p.parent_id
if p_id > -1:
people[p_id].children.append(p)
# assume first person is root
root = people[0]
print(root.name + " (" + str(root.id) + ")")
printChildren(root)
printTree(
"null,0,grandpa|0,1,son|0,2,daugther|1,3,grandkid|1,4,grandkid|2,5,grandkid|5,6,greatgrandkid"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment