def __init__(self, path): #初始化 global indent self.path = path self.children = [] self.node_hash = self.generate_file_hash(path) self.is_leaf = True if not isdir(path): # print("{}Exiting init".format(' ' * indent * 2)) return for obj in sorted(listdir(path)): # print("{}Adding child called {}".format(' ' * indent * 2, dir)) indent += 1 new_child = Node(join(path, obj)) indent -= 1 self.add_child(new_child)
时间: 2024-02-14 17:32:40 浏览: 130
这段代码定义了Node类的初始化方法__init__,用于创建节点对象。初始化方法接受一个路径参数path,表示当前节点代表的文件或目录的路径。初始化方法首先定义了全局变量indent,它表示当前节点在树形结构中的缩进层数。然后根据路径生成当前节点的哈希值,并将其赋值给node_hash属性。接着判断当前路径是否为目录,如果不是目录,则直接返回。如果是目录,则遍历目录下的所有文件和子目录,并为每个子文件或子目录创建一个Node对象,并将其添加到当前节点的子节点列表中。在创建子节点时,先将indent加1表示缩进层数加1,然后调用Node类的构造函数创建子节点对象,最后将indent减1表示缩进层数减1。需要注意的是,代码中使用了递归调用和分治算法的思想,在创建子节点时又调用了Node类的add_child方法,实现了树形结构的建立。
相关问题
优化该代码class Path(object): def __init__(self,path,cost1,cost2): self.__path = path self.__cost1 = cost1 self.__cost2 = cost2 #路径上最后一个节点 def getLastNode(self): return self.__path[-1] #获取路径路径 @property def path(self): return self.__path #判断node是否为路径上最后一个节点 def isLastNode(self, node): return node == self.getLastNode() #增加加点和成本产生一个新的path对象 def addNode(self, node, price1,price2): return Path(self.__path+[node],self.__cost1+ price1,self.__cost2+ price2) #输出当前路径 def printPath(self): global num #将num作为循环次数,即红绿灯数量 global distance num = 0 for n in self.__path: if self.isLastNode(node=n): print(n) else: print(n, end="->") num += 1 print("全程约为 {:.4}公里".format(str(self.__cost1))) print("时间大约为 {}分钟".format(str(self.__cost2))) print("需要经过{}个红绿灯".format(num)) distance = self.__cost1 #获取路径总成本的只读属性 @property def travelCost1(self): return self.__cost1 @property def travelCost2(self): return self.__cost2 class DirectedGraph(object): def __init__(self, d): if isinstance(d, dict): self.__graph = d else: self.__graph = dict() print('Sth error') def __generatePath(self, graph, path, end, results): #current = path[-1] current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: #if n not in path: if n not in path.path: #self.__generatePath(graph, path + [n], end, results) self.__generatePath(graph, path.addNode(n,self.__graph[path.getLastNode()][n][0],self.__graph[path.getLastNode()][n][1]),end, results) #self.__generatePath(graph,使其能够保存输入记录并且能够查询和显示
可以考虑以下优化:
1. 在 `Path` 类中,可以将 `getLastNode()` 方法改为直接返回 `self.__path[-1]`,因为这个方法只被 `addNode()` 方法调用,而 `addNode()` 方法中也可以直接使用 `self.__path[-1]`。
2. 在 `Path` 类中,可以将 `isLastNode()` 方法改为直接比较 `node` 和 `self.__path[-1]`,因为这个方法只被 `printPath()` 方法调用,而 `printPath()` 方法中也可以直接比较。
3. 在 `Path` 类中,可以将 `printPath()` 方法中的 `global` 声明去掉,因为在该方法中没有使用到任何全局变量。
4. 在 `Path` 类中,可以将 `distance` 变量改为实例变量,即在 `__init__()` 方法中初始化为 0,并在 `printPath()` 方法中使用 `self.__cost1` 赋值。
5. 在 `DirectedGraph` 类中,可以将 `__graph` 变量改为实例变量,即在 `__init__()` 方法中初始化为空字典,并在需要使用时直接使用 `self.__graph`。
下面是修改后的代码:
```
class Path(object):
def __init__(self, path, cost1, cost2):
self.__path = path
self.__cost1 = cost1
self.__cost2 = cost2
self.__distance = 0 # 路径总长度,初始为 0
@property
def path(self):
return self.__path
def getLastNode(self):
return self.__path[-1]
def isLastNode(self, node):
return node == self.__path[-1]
def addNode(self, node, price1, price2):
return Path(self.__path + [node], self.__cost1 + price1, self.__cost2 + price2)
def printPath(self):
num = 0
for n in self.__path:
if self.isLastNode(n):
print(n)
else:
print(n, end="->")
num += 1
print("全程约为 {:.4}公里".format(str(self.__cost1)))
print("时间大约为 {}分钟".format(str(self.__cost2)))
print("需要经过{}个红绿灯".format(num))
self.__distance = self.__cost1
@property
def travelCost1(self):
return self.__cost1
@property
def travelCost2(self):
return self.__cost2
class DirectedGraph(object):
def __init__(self, d):
if isinstance(d, dict):
self.__graph = d
else:
self.__graph = dict()
print('Sth error')
def __generatePath(self, graph, path, end, results):
current = path.getLastNode()
if current == end:
results.append(path)
else:
for n in graph[current]:
if n not in path.path:
results = self.__generatePath(graph, path.addNode(n, graph[path.getLastNode()][n][0], graph[path.getLastNode()][n][1]), end, results)
return results
```
阅读全文
相关推荐

















