forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operations on Tree.py
55 lines (48 loc) · 1.95 KB
/
Operations on 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
class LockingTree:
def __init__(self, parent: List[int]):
self.p = collections.defaultdict(lambda: -2)
self.c = collections.defaultdict(list)
for i, p in enumerate(parent):
self.c[p].append(i)
self.p[i] = p
self.user = collections.defaultdict(set)
self.node = collections.defaultdict(lambda: -2)
def lock(self, num: int, user: int) -> bool:
if self.node[num] == -2:
self.user[user].add(num)
self.node[num] = user
return True
return False
def unlock(self, num: int, user: int) -> bool:
if self.node[num] == user:
del self.node[num]
self.user[user].remove(num)
return True
return False
def upgrade(self, num: int, user: int) -> bool:
if self.node[num] != -2: return False
if not self.has_locked_descendant(num): return False
if self.has_locked_ancester(num): return False
self.lock(num, user)
self.unlock_descendant(num)
return True
def has_locked_descendant(self, num): #function to check if alteast one desendent is lock or not
has = False
for child in self.c[num]:
if self.node[child] != -2:
return True
has |= self.has_locked_descendant(child)
return has
def has_locked_ancester(self, num): # function to check if no parent is locked
if num == -1: return False
if self.node[self.p[num]] != -2:
return True
return self.has_locked_ancester(self.p[num])
def unlock_descendant(self, num): # function fro unlocking all desendents
for child in self.c[num]:
if child in self.node:
user = self.node[child]
del self.node[child]
if user in self.user:
self.user[user].remove(child)
self.unlock_descendant(child)