-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsol.py
60 lines (53 loc) · 2 KB
/
sol.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
56
57
58
59
60
from tree import Node
import random
class TreeNode(Node):
def __init__(self, val=None):
super().__init__(data=val)
self._lock_state = False
def insertNode(self, val):
if self.left is None and self.right is not None:
self.left = TreeNode(val)
self.left.parent = self
elif self.right is None and self.left is not None:
self.right = TreeNode(val)
self.right.parent = self
elif self.left is None and self.right is None:
choice = random.choice(['LEFT', 'RIGHT'])
if choice=='LEFT':
self.left = TreeNode(val)
self.left.parent = self
else:
self.right = TreeNode(val)
self.right.parent = self
elif self.left is not None and self.right is not None:
choice = random.choice(['LEFT', 'RIGHT'])
if choice=='LEFT':
self.left.insertNode(val)
else:
self.right.insertNode(val)
def lock(self):
if self.parent._lock_state or self.left._lock_state or self.right._lock_state:
return False, 'Not possible'
self._lock_state = True
return True
def unlock(self):
if self.parent._lock_state or self.left._lock_state or self.right._lock_state:
return False, 'Not possible'
self._lock_state = False
return True
def is_locked(self):
return self._lock_state
if __name__=='__main__':
tree = TreeNode('*')
for _ in range(20):
val = random.randint(1, 100)
tree.insertNode(val)
tree.prettyPrint()
print('\n\n\n')
print(tree.left.lock()) #should lock and returnn true
print(tree.is_locked()) #should be false
print(tree.left.is_locked()) #should be true
print(tree.left.left.is_locked()) #should be false
print(tree.left.left.unlock()) #should not be possible
print(tree.left.unlock()) #should be possible
print(tree.left.left.is_locked()) #should be false