-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockedBinary.rb
66 lines (53 loc) · 1.11 KB
/
lockedBinary.rb
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
56
57
58
59
60
61
62
63
64
65
66
class BinaryTree
attr_accessor :locked_descendants
def initialize(value, parent)
@value = value
@parent = parent
@locked_descendants = 0
@locked = false
end
def unlock
return true if !self.locked
if self.restricted?
return false
else
self.locked = false
update_parent(toggle_lock = false)
return true
end
end
def lock
return true if self.locked
if self.restricted?
return false
else
self.locked = true
update_parent(toggle_lock = true)
return true
end
end
def locked
@locked
end
def parent
@parent
end
def locked=(lock)
@locked = lock
end
def is_parent_locked
return false if !self.parent
return true if self.parent.locked
is_parent_locked(self.parent)
end
def update_parent(toggle_lock)
locked_descendant_count = toggle_lock == true ? 1 : -1
if self.parent
self.parent.locked_descendants += locked_descendant_count
update_parent(self.parent, toggle_lock)
end
end
def restricted?
is_parent_locked || self.locked_descendants > 0
end
end