MCPcopy Index your code
hub / github.com/itcharge/AlgoNote / find

Method find

codes/python/05_tree/tree_unionFind_UnoinByRank.py:6–10  ·  view source on GitHub ↗
(self, x)

Source from the content-addressed store, hash-verified

4 self.rank = [1 for i in range(n)] # 每个元素的深度初始化为 1
5
6 def find(self, x): # 查找元素根节点的集合编号内部实现方法
7 while self.fa[x] != x: # 递归查找元素的父节点,直到根节点
8 self.fa[x] = self.fa[self.fa[x]] # 隔代压缩优化
9 x = self.fa[x]
10 return x # 返回元素根节点的集合编号
11
12 def union(self, x, y): # 合并操作:令其中一个集合的树根节点指向另一个集合的树根节点
13 root_x = self.find(x)

Callers 2

unionMethod · 0.95
is_connectedMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected