def _match(self, lab_l1, lab_l2, equal): d1, d2 = self._d1, self._d2 mapping = self._mapping # for each leaf label in both tree1 and tree2 l = intersection(lab_l1.keys(), lab_l2.keys()) # sort list to avoid differences between python version l.sort() for label in l: s1 = lab_l1[label] s2 = lab_l2[label] # compute the longest common subsequence common = lcs2(s1, s2, equal) # for each pair of nodes (x,y) in the lcs for x, y in common: # add (x,y) to the mapping mapping.append((x, y)) # mark node from tree 1 as mapped x[N_MAPPED] = TRUE # fill the mapping cache for n in get_ancestors(x, []): d1[(id(n), id(x))] = 1 for n in get_ancestors(y, []): d2[(id(n), id(y))] = 1
def _align_children(self, w, x): """ align children to correct misaligned nodes """ _partner = partner # mark all children of w an d as "out of order" self._childs_out_of_order(w) self._childs_out_of_order(x) # s1: children of w whose partner is children of x s1 = [n for n in w[N_CHILDS] if in_ref(x[N_CHILDS], _partner(0,n))] # s2: children of x whose partners are children of w s2 = [n for n in x[N_CHILDS] if in_ref(w[N_CHILDS], _partner(1,n))] # compute the longest common subsequence s = lcs2(s1, s2, has_couple) # mark each (a,b) from lcs in order for a, b in s: a[N_INORDER] = b[N_INORDER] = TRUE s1.pop(index_ref(s1, a)) # s: a E T1, b E T2, (a,b) E M, (a;b) not E s for a in s1: b = _partner(0, a) # mark a and b in order a[N_INORDER] = b[N_INORDER] = TRUE k = self._find_pos(b) self._make_move(a, w, k)