-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.py
More file actions
24 lines (18 loc) · 726 Bytes
/
TreeNode.py
File metadata and controls
24 lines (18 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TreeNode:
def __init__(self, value):
print("initializing node...")
self.value = value
self.children = []
def add_child(self, child_node):
print("Adding " + child_node.value)
self.children.append(child_node)
def remove_child(self, child_node):
print("Removing" + child_node.value + " from " + self.value)
self.children = [child for child in self.children if child is not child_node]
def traverse(self):
print("Traversing...")
nodes_to_visit = [self]
while len(nodes_to_visit) > 0:
current_node = nodes_to_visit.pop()
print(current_node.value)
nodes_to_visit += current_node.children