Skip to content

Instantly share code, notes, and snippets.

@metric-space
Created March 27, 2017 00:31
Show Gist options
  • Select an option

  • Save metric-space/e110fb820f6b5a00c2611006dba232bd to your computer and use it in GitHub Desktop.

Select an option

Save metric-space/e110fb820f6b5a00c2611006dba232bd to your computer and use it in GitHub Desktop.
class Node:
def __init__(self,val,parent=None):
self.val = val
self.children = []
self.parent = parent
def __eq__(self,other):
if self.val == other.val:
return True
return False
def addChildNode(self,node):
for i in self.children:
if i == node:
return i
node.parent = self.val
self.children.append(node)
return node
def addChildNodeListOfVals(self, list_of_nodes):
if list_of_nodes == []:
return
head = list_of_nodes[0]
tail = list_of_nodes[1:]
aa = Node(head)
egg = self.addChildNode(aa)
egg.addChildNodeListOfVals(tail)
def printez(self):
gatherChildren = []
for i in self.children:
gatherChildren.append(i.printez())
return {"name":self.val,
"parent":self.parent,
"children":gatherChildren}
a = ["reddit"]
b = ["reddit","chicken","beta","side.pp"]
c = ["reddit","chicken","eta","snake.pp"]
egg = Node("website")
egg.addChildNodeListOfVals(a)
egg.addChildNodeListOfVals(b)
egg.addChildNodeListOfVals(c)
print(egg.printez())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment