-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.py
More file actions
206 lines (156 loc) · 4.91 KB
/
Copy pathnode.py
File metadata and controls
206 lines (156 loc) · 4.91 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from enum import Enum, unique
@unique
class Encoding(Enum):
ABSENT = 0
PRESENT = 1
HONEST = 2
ON_LONGEST_TINE = 4
class Node:
'''Node of the tree'''
def __init__(self, parent = None):
self._depth = 0
self._height = 0
# id
self.id = None
self._num_children = 0
if parent is None:
# I am a root node
self._parent = None
else:
self.parent = parent
self.parent.add_child(self)
self._children = [] # no children now
self.visited_lca = None
# important for it to be None
self.encoding = None
def _get_parent(self):
return self._parent
def _set_parent(self, value):
if isinstance(value, Node):
self._parent = value
self._depth = self.parent.depth + 1
else:
raise TypeError("Parent must be a Node.")
parent = property(_get_parent, _set_parent)
def _get_depth(self):
return self._depth
depth = property(_get_depth)
def num_children(self):
return self._num_children
def _get_height(self):
return self._height
# don't call it lightly
def _set_height(self, h):
self._height = h
height = property(_get_height)
def _get_children(self):
return self._children
children = property(_get_children)
def add_child(self, child):
if isinstance(child, Node):
self.children.append(child)
self._num_children += 1
else:
raise ValueError("Attempting to add an invalid child")
def prune_subtree(self, cond, fork = None):
if not self.isleaf:
unfortunate_ones = [c for c in self.children if cond(c)]
# print("unfortunate_ones: {}".format([t.id for t in unfortunate_ones]))
for c in unfortunate_ones:
self.remove_child(child = c, fork = fork)
for c in self.children:
c.prune_subtree(cond = cond, fork = fork)
# removal is non-recursive
def remove_child(self, child, cond = None, fork = None):
if isinstance(child, Node):
if child in self.children:
if (cond is None) or cond(child):
cpath = child.path
self.children.remove(child)
self._num_children -= 1
if fork:
fork.on_node_deleted(child, parent = self)
print("Removed {}".format(cpath))
else:
print("\n\n#### PANIC ####")
if fork:
fork.diagnostics(matrix = False, tines = True)
print("Attempting to remove {}".format(child.path))
print("Problem node: {} children: {}".format(self.path, [c.label for c in self.children]))
raise ValueError("Child not in children")
else:
raise ValueError("Child not a node")
def _isleaf(self):
# return len(self.children) == 0
return self._num_children == 0
isleaf = property(_isleaf)
# def visit_subtree(self, func):
# func(self)
# if not self.isleaf:
# arr = list(self.children)
# for child in arr:
# child.visit_subtree(func)
# def visit_leaves(self, func):
# if self.isleaf:
# func(self)
# else:
# arr = list(self.children)
# for child in arr:
# child.visit_leaves(func)
# ============= Methods for height ===================
# propagate height upstream
def update_height_upstream(self, removal = False):
# print("In update_height_upstream for node: {}".format(self.id))
if self.isleaf:
# leaf node
if self._height is not 0:
self._height = 0
# print("Leaf node: {}".format(self.id))
p = self.parent
# child = self
else:
# internal node
# print("Internal node: {}".format(self.id))
p = self
while p is not None:
new_parent_height = p.calculate_height()
# print("Node: {} current height: {}".format(p.id, p.height))
# print("Node: {} proposed height: {}".format(p.id, new_parent_height))
if removal:
need_update = (new_parent_height < p.height)
else:
need_update = (new_parent_height > p.height)
if need_update:
# print("Update node: {} height to: {}".format(p.id, new_parent_height))
# update
p._set_height(new_parent_height)
# prepare for next iteration
# child = p
p = p.parent
else:
# no change in height
break
# height of this node is 1 + max_{child} child.height
def calculate_height(self):
if self.isleaf:
self.height = 0
else:
child_heights = [c.height for c in self.children]
return 1 + max(child_heights)
# ============ Methods for encoding ===========
def is_encoding_on_longest_tine(self):
return (self.encoding is not None) and \
(int(self.encoding) & Encoding.ON_LONGEST_TINE.value) is not 0
def calculate_and_set_encoding(self, on_longest_tine = False):
value = Encoding.PRESENT.value # Present bit
if self.label == 0 or self.honest:
value += Encoding.HONEST.value # Honest bit
if on_longest_tine:
value += Encoding.ON_LONGEST_TINE.value # The G bit
# finally
s_value = str(value)
# remember encoding as it does not change
self.encoding = value
return value
def reset_encoding(self):
self.encoding = None