-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.py
176 lines (114 loc) · 3.75 KB
/
tree.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from utils import strToList
class node:
def __init__(self,value,parent=None,camada=1,lastTopic=None):
self.__value=value
self.__children=set()
self.__parent=parent
self.__camada=camada
self.__sockets=set()
self.__lastTopic=lastTopic
@property
def value(self):
return self.__value
@property
def children(self):
return self.__children
@property
def parent(self):
return self.__parent
@property
def camada(self):
return self.__camada
@property
def sockets(self):
return self.__sockets
@property
def lastTopic(self):
return self.__lastTopic
def isChild(self,value):
for child in self.children:
if child.value==value:
return True
return False
def getChild(self,value):
for child in self.children:
if(child.value==value):
return child
def addLastTopic(self,msg):
self.__lastTopic=msg
def constructValue(self,topic):
if self.value=="/":
return self.value+ topic
else:
return self.value+"/"+topic
def getNode(self,value):# depth-first
# print(self.value)
if(self.value==value):
return self
if( self.children):
for c in self.children:
x=c.getNode(value)
if x:
return x
def getNodev2(self,camada,value):
if(camada==self.camada+1):
for no in self.children:
if value==no.value:
return no
return
else:
for child in self.children:
node=child.getNodev2(camada,value)
if node:
return node
def addSocket(self,conn):
self.sockets.add(conn)
def removeSocket(self,conn):
self.sockets.remove(conn)
def insert(self,value,parent,camada): # /temp/1
n=node(value,parent,camada)
self.children.add(n)
return n
def addNode(self,topic):
# print(topic)
if len(topic)==0:
print(self)
return self
if (not self.children):
childvalue=self.constructValue(topic[0])
# print(childvalue)
# print(type(topic[0]))
child=self.insert(childvalue,self,self.camada+1)
return child.addNode(topic[1:len(topic)])
else:
# print(topic[0])
childvalue=self.constructValue(topic[0])
if self.isChild(childvalue):
child=self.getChild(childvalue)
return child.addNode(topic[1:len(topic)])
else:
# childvalue=str(self.value()) + str(topic[0])
child=self.insert(childvalue,self,self.camada+1)
return child.addNode(topic[1:len(topic)])
def transverse(self):
print(self.value)
if not self.children:
return
for child in self.children:
child.transverse()
def RemoveNode(self,node):
self.children.remove(node)
def __str__(self):
return self.value
def getChildren(self,set):
if not self.children:
return set
for child in self.children:
set.add(child)
child.getChildren(set)
return set
def getParent(self,parentSet):
if self.camada==1:
return parentSet
parentSet.add(self.parent)
return self.parent.getParent(parentSet)