-
Notifications
You must be signed in to change notification settings - Fork 2
/
difftreese.cpp
235 lines (197 loc) · 7.08 KB
/
difftreese.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "muscle.h"
#include "tree.h"
#define TRACE 0
/***
Algorithm to compare two trees, X and Y.
A node x in X and node y in Y are defined to be
similar iff the set of leaves in the subtree under
x is identical to the set of leaves under y.
A node is defined to be changed iff it is not
similar to any node in the other tree.
Nodes x and y are defined to be married iff every
node in the subtree under x is similar to a node
in the subtree under y. Married nodes are considered
to be equal. The subtrees under two married nodes can
at most differ by exchanges of left and right branches,
which we do not consider to be significant here.
A node is changed iff it is not married. If a node is
changed, then it has a dissimilar node in its subtree,
and it follows immediately from the definition of marriage
that its parent is also a bachelor. Hence all nodes on the
path from a changed node to the root are changed.
We assume the trees have the same set of leaves, so
every leaf is trivially both similar and married to
the same leaf in the opposite tree. Changed nodes
are therefore always internal (i.e., non-leaf) nodes.
Example:
-----A
-----k
----j -----B
--i -----C
------D
-----A
-----p
----n -----B
--m -----D
------C
The following pairs of internal nodes are similar.
Nodes Set of leaves
----- -------------
k,p A,B
i,m A,B,C,D
Changed nodes in the first tree are i and j, changed nodes
in the second tree are m and n.
Node k and p are married, but i and m are not (because j
and n are changed). The diffs are C, D and k.
To achieve O(N) we avoid traversing a given subtree multiple
times and also avoid comparing lists of leaves.
We visit nodes in depth-first order (i.e., a node is visited
before its parent).
If either child of a node is changed, we flag it as changed.
If both children of the node we are visiting are married,
we check whether the spouses of those children have the
same parent in the other tree. If the parents are different,
the current node is a bachelor. If they have the same parent,
then the node we are visiting is the spouse of that parent.
We assign this newly identified married couple a unique integer
id. The id of a node is in one-to-one correspondence with the
set of leaves in its subtree. Two nodes have the same set of
leaves iff they have the same id. Changed nodes do not get
an id.
***/
void DiffTreesE(const Tree &NewTree, const Tree &OldTree,
unsigned NewNodeIndexToOldNodeIndex[])
{
#if TRACE
Log("DiffTreesE NewTree:\n");
NewTree.LogMe();
Log("\n");
Log("OldTree:\n");
OldTree.LogMe();
#endif
if (!NewTree.IsRooted() || !OldTree.IsRooted())
Quit("DiffTrees: requires rooted trees");
const unsigned uNodeCount = NewTree.GetNodeCount();
const unsigned uOldNodeCount = OldTree.GetNodeCount();
const unsigned uLeafCount = NewTree.GetLeafCount();
const unsigned uOldLeafCount = OldTree.GetLeafCount();
if (uNodeCount != uOldNodeCount || uLeafCount != uOldLeafCount)
Quit("DiffTreesE: different node counts");
{
unsigned *IdToOldNodeIndex = new unsigned[uNodeCount];
for (unsigned uOldNodeIndex = 0; uOldNodeIndex < uNodeCount; ++uOldNodeIndex)
{
if (OldTree.IsLeaf(uOldNodeIndex))
{
unsigned Id = OldTree.GetLeafId(uOldNodeIndex);
IdToOldNodeIndex[Id] = uOldNodeIndex;
}
}
// Initialize NewNodeIndexToOldNodeIndex[]
// All internal nodes are marked as changed, but may be updated later.
for (unsigned uNewNodeIndex = 0; uNewNodeIndex < uNodeCount; ++uNewNodeIndex)
{
if (NewTree.IsLeaf(uNewNodeIndex))
{
unsigned uId = NewTree.GetLeafId(uNewNodeIndex);
assert(uId < uLeafCount);
unsigned uOldNodeIndex = IdToOldNodeIndex[uId];
assert(uOldNodeIndex < uNodeCount);
NewNodeIndexToOldNodeIndex[uNewNodeIndex] = uOldNodeIndex;
}
else
NewNodeIndexToOldNodeIndex[uNewNodeIndex] = NODE_CHANGED;
}
delete[] IdToOldNodeIndex;
}
// Depth-first traversal of tree.
// The order guarantees that a node is visited before
// its parent is visited.
for (unsigned uNewNodeIndex = NewTree.FirstDepthFirstNode();
NULL_NEIGHBOR != uNewNodeIndex;
uNewNodeIndex = NewTree.NextDepthFirstNode(uNewNodeIndex))
{
if (NewTree.IsLeaf(uNewNodeIndex))
continue;
// If either child is changed, flag this node as changed and continue.
unsigned uNewLeft = NewTree.GetLeft(uNewNodeIndex);
unsigned uOldLeft = NewNodeIndexToOldNodeIndex[uNewLeft];
if (NODE_CHANGED == uOldLeft)
{
NewNodeIndexToOldNodeIndex[uNewLeft] = NODE_CHANGED;
continue;
}
unsigned uNewRight = NewTree.GetRight(uNewNodeIndex);
unsigned uOldRight = NewNodeIndexToOldNodeIndex[uNewRight];
if (NODE_CHANGED == NewNodeIndexToOldNodeIndex[uNewRight])
{
NewNodeIndexToOldNodeIndex[uNewRight] = NODE_CHANGED;
continue;
}
unsigned uOldParentLeft = OldTree.GetParent(uOldLeft);
unsigned uOldParentRight = OldTree.GetParent(uOldRight);
if (uOldParentLeft == uOldParentRight)
NewNodeIndexToOldNodeIndex[uNewNodeIndex] = uOldParentLeft;
else
NewNodeIndexToOldNodeIndex[uNewNodeIndex] = NODE_CHANGED;
}
#if TRACE
{
Log("NewToOld ");
for (unsigned uNewNodeIndex = 0; uNewNodeIndex < uNodeCount; ++uNewNodeIndex)
{
Log(" [%3u]=", uNewNodeIndex);
if (NODE_CHANGED == NewNodeIndexToOldNodeIndex[uNewNodeIndex])
Log(" X");
else
Log("%3u", NewNodeIndexToOldNodeIndex[uNewNodeIndex]);
if ((uNewNodeIndex+1)%8 == 0)
Log("\n ");
}
Log("\n");
}
#endif
#if DEBUG
{
for (unsigned uNewNodeIndex = 0; uNewNodeIndex < uNodeCount; ++uNewNodeIndex)
{
unsigned uOld = NewNodeIndexToOldNodeIndex[uNewNodeIndex];
if (NewTree.IsLeaf(uNewNodeIndex))
{
if (uOld >= uNodeCount)
{
Log("NewNode=%u uOld=%u > uNodeCount=%u\n",
uNewNodeIndex, uOld, uNodeCount);
Quit("Diff check failed");
}
unsigned uIdNew = NewTree.GetLeafId(uNewNodeIndex);
unsigned uIdOld = OldTree.GetLeafId(uOld);
if (uIdNew != uIdOld)
{
Log("NewNode=%u uOld=%u IdNew=%u IdOld=%u\n",
uNewNodeIndex, uOld, uIdNew, uIdOld);
Quit("Diff check failed");
}
continue;
}
if (NODE_CHANGED == uOld)
continue;
unsigned uNewLeft = NewTree.GetLeft(uNewNodeIndex);
unsigned uNewRight = NewTree.GetRight(uNewNodeIndex);
unsigned uOldLeft = OldTree.GetLeft(uOld);
unsigned uOldRight = OldTree.GetRight(uOld);
unsigned uNewLeftPartner = NewNodeIndexToOldNodeIndex[uNewLeft];
unsigned uNewRightPartner = NewNodeIndexToOldNodeIndex[uNewRight];
bool bSameNotRotated = (uNewLeftPartner == uOldLeft && uNewRightPartner == uOldRight);
bool bSameRotated = (uNewLeftPartner == uOldRight && uNewRightPartner == uOldLeft);
if (!bSameNotRotated && !bSameRotated)
{
Log("NewNode=%u NewL=%u NewR=%u\n", uNewNodeIndex, uNewLeft, uNewRight);
Log("OldNode=%u OldL=%u OldR=%u\n", uOld, uOldLeft, uOldRight);
Log("NewLPartner=%u NewRPartner=%u\n", uNewLeftPartner, uNewRightPartner);
Quit("Diff check failed");
}
}
}
#endif
}