-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_union_rank_uf.go
54 lines (48 loc) · 1.14 KB
/
quick_union_rank_uf.go
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
/*
* Algorithm 4-th Edition
* Golang translation from Java by Robert Sedgewick and Kevin Wayne.
*
* @Author: Yajun
* @Date: 2020/11/2 15:25
*/
package fundamentals
// quick union基于rank的优化, rank即根节点的高度
// 把rank小的根节点链接到rank大的根节点,这比size优化更加合理
type QuickUnionRankUF struct {
*QuickUnionUF
rank []int
}
func NewQuickUnionRankUF(n int) *QuickUnionRankUF {
uf := &QuickUnionRankUF{
QuickUnionUF: &QuickUnionUF{
parent: make([]int, n),
count: n,
},
}
uf.rank = make([]int, n)
for i := 0; i < n; i++ {
uf.parent[i] = i
uf.rank[i] = 1
}
return uf
}
func (u *QuickUnionRankUF) Union(p, q int) {
pRoot := u.Find(p)
qRoot := u.Find(q)
if pRoot == qRoot {
return
}
// 将rank小的根节点链接到rank大的根节点
// 这里对辅助数组rank的维护和size不太一样
// 只有合并后树真的变高了,才需要更新
if u.rank[pRoot] < u.rank[qRoot] {
u.parent[pRoot] = qRoot
} else if u.rank[pRoot] > u.rank[qRoot] {
u.parent[qRoot] = pRoot
} else {
// u.rank[pRoot] == u.rank[qRoot]
u.parent[pRoot] = qRoot
u.rank[qRoot]++
}
u.count--
}