-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_data_structure.py
55 lines (46 loc) · 1.13 KB
/
my_data_structure.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
class MyDataStructure:
def __init__(self, name: str, type) -> None:
self.name = name
self.type = type
class Tree:
def __init__(self, n) -> None:
"""
n: number of node
"""
self.root = [i for i in range(n)]
self.rank = [1 for _ in range(n)]
def find(self, x):
"""
description
root node represent the subset
args
x: int
index of node
return
index of root node
"""
rx = self.root[x]
if rx != x:
return self.find(rx)
else:
return x
def compressed_find(self, x):
"""
description
similar to Tree.find, except tree path compression
tree path compression is
args
x: int
index of node
return
index of root node
"""
rx = self.root[x]
if rx != x:
self.root[x] = self.find(rx)
return self.root[x]
else:
return x
def union(self, x, y):
"""
"""