Skip to content

Commit

Permalink
Trees
Browse files Browse the repository at this point in the history
Data Structures - Trees
  • Loading branch information
DarthNixx authored Sep 12, 2019
1 parent 68c843e commit 91ca0fb
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Trees.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class TreeNode:
def __init__(self, value):
self.value = value # data
self.children = [] # references to other nodes

def add_child(self, child_node):
# creates parent-child relationship
print("Adding " + child_node.value)
self.children.append(child_node)

def remove_child(self, child_node):
# removes parent-child relationship
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):
# moves through each node referenced from self downwards
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

0 comments on commit 91ca0fb

Please sign in to comment.