-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.py
78 lines (69 loc) · 1.75 KB
/
Node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
######### CLASS NODE #########
class Node:
def __init__(self, data):
self.parent = None
self.level = 0
self.data = data
self.children = []
def getData(self):
return self.data
def getChildren(self):
return self.children
def getLevel(self):
return self.level
def getParent(self):
return self.parent
def addChild(self, node):
if(self.__class__ == node.__class__):
node.parent = self
node.level = self.level + 1
self.children.append(node)
def equals(self, other):
if(self.__class__ == other.__class__):
if(self.data == other.data):
return True;
return False
def contains(self, other):
found = False
if(self.__class__ == other.__class__):
if(self.data == other.data):
found = True;
else:
for child in self.children:
found = child.contains(other)
if(found == True):
break
return found
def find(self, other):
foundNode = None
if(self.__class__ == other.__class__):
if(self.data == other.data):
foundNode = self;
else:
for child in self.children:
foundNode = child.find(other)
if(foundNode != None):
break
return foundNode
def toString(self, tabulate):
tabs = ' '
if(tabulate):
for i in range(0, self.level):
tabs += ' '
return str(self.level) + tabs + self.data + '\n'
def buildWithChildren(self, tabulate):
result = self.toString(tabulate)
for child in self.children:
result += child.buildWithChildren(tabulate)
return result
def buildWithAncestors(self, tabulate):
result = ''
if(self.parent is not None):
result = self.parent.buildWithAncestors(tabulate)
return result + self.toString(tabulate)
######### END OF CLASS #########
def main():
node = Node("hello world")
print(node.getData())
if __name__ == "__main__":
main()