-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVis2LeafMeasure.js
147 lines (122 loc) · 3.31 KB
/
Vis2LeafMeasure.js
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
/**
* This method strips the leaves of a node
*
* @param {Vis2Node} rNode A Node
* @return {Array} Returns array with the name of all leaves below the given node
*/
function GetLeaves(rNode)
{
var vLeaves = new Array();
// return name, if this is a leaf
if (rNode.isleaf == true)
vLeaves.push(rNode.name);
else
{
// collect leaves under child nodes, if this is not a leaf
for ( var i = 0; i < rNode.GetChildrenCount(); i++)
{
var vLeavesOfChildNode = GetLeaves(rNode.children[i]);
for ( var j = 0; j < vLeavesOfChildNode.length; j++)
vLeaves.push(vLeavesOfChildNode[j]);
}
}
return vLeaves;
}
/**
* This method calculates the leaf measure for two given nodes
*
* @param {Vis2Node} rNode1 A Node
* @param {Vis2Node} rNode2 A Node
* @return {float} Returns a floating value which represents the leaf measure of the two given nodes
*/
function GetLeafMeasure(rNode1, rNode2)
{
// get leaves under node 1
vLeavesOfNode1 = GetLeaves(rNode1);
// get leaves under node 2
vLeavesOfNode2 = GetLeaves(rNode2);
var iSameLeaves = 0;
var iTotal = vLeavesOfNode1.length + vLeavesOfNode2.length;
for ( var i = 0; i < vLeavesOfNode1.length; i++)
{
var bFound = false;
for ( var j = 0; j < vLeavesOfNode2.length; j++)
{
if (vLeavesOfNode2[j] == vLeavesOfNode1[i])
{
bFound = true;
break;
}
}
if (bFound == true)
{
iSameLeaves++;
iTotal--;
}
}
return (iSameLeaves / iTotal);
}
/**
* This method calculates the best matching leaf measure of a tree compared to
* a specific reference node
*
* @param rReferenceNode
* @param rTestTree
* @returns {Number}
*/
function GetBestMatchingLeafMeasure(rReferenceNode, rTestTree)
{
var aTestTreeNodes = rTestTree.GetNodeList();
var fMaximum = 0;
for(var i = 0; i < aTestTreeNodes.length; i++)
{
var fMeasure = GetLeafMeasure(rReferenceNode, aTestTreeNodes[i]);
if(fMeasure > fMaximum)
fMaximum = fMeasure;
}
return fMaximum;
}
/**
* This method calculates the leaf measures for the given test tree,
* tested against the reference tree
*
* @param {Vis2Node} rReferenceTree The reference tree
* @param {Vis2Node} rTestTree The test tree, will be modified (leaf measure values will be set)
* @return {Vis2Node} Returns the rTestTree
*/
function Vis2LeafMeasure(rReferenceTree, rTestTree)
{
var rNodesTestTree = rTestTree.GetNodeList();
var rNodesReferenceTree = rReferenceTree.GetNodeList();
for ( var iNode = 0; iNode < rNodesTestTree.length; iNode++)
{
var iMaximum = 0;
for ( var iReferenceNode = 0; iReferenceNode < rNodesReferenceTree.length; iReferenceNode++)
{
var iMeasure = GetLeafMeasure(rNodesReferenceTree[iReferenceNode], rNodesTestTree[iNode]);
if (iMeasure > iMaximum)
iMaximum = iMeasure;
}
rNodesTestTree[iNode].leafmeasure = iMaximum;
}
return rTestTree;
}
function GetLeafMeasureOverview(rReferenceTree, rTestTree)
{
var aReferenceTreeNodes = rReferenceTree.GetNodeList();
var aTestTreeNodes = rTestTree.GetNodeList();
var fAverage = 0;
for(var i = 0; i < aTestTreeNodes.length; i++)
{
var iMaximum = 0;
for(var j = 0; j < aReferenceTreeNodes.length; j++)
{
var iMeasure = GetLeafMeasure(aTestTreeNodes[i], aReferenceTreeNodes[j]);
if(iMeasure > iMaximum)
iMaximum = iMeasure;
}
fAverage += iMaximum;
}
fAverage /= aTestTreeNodes.length;
return fAverage;
}