Skip to content

Instantly share code, notes, and snippets.

@kelvingakuo
Created November 19, 2021 11:07
Show Gist options
  • Save kelvingakuo/d9deb1b9ef1dad1b30a3cecf36ff2fe5 to your computer and use it in GitHub Desktop.
Save kelvingakuo/d9deb1b9ef1dad1b30a3cecf36ff2fe5 to your computer and use it in GitHub Desktop.
class Node(object):
def __init__(self, val):
self.value = val # Value of node
self.children = [] # Can have multiple children
def add_child(self, node):
self.children.append(node)
return node
# Usage
tree = Node("root")
f_child = tree.add_child(Node("first_child"))
s_child = tree.add_child(Node("second_child"))
t_child = tree.add_child(Node("third_child"))
ff_child = f_child.add_child(Node("first_first_child"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment