提交 b39ad705 编写于 作者: J Jason Park

tree

上级 15c73291
......@@ -188,14 +188,14 @@ section {
.data_container {
top: 60px;
bottom: 70%;
bottom: 60%;
left: 0;
right: 0;
}
.code_container {
left: 0;
top: 30%;
top: 40%;
right: 0;
bottom: 0;
}
......
......@@ -26,6 +26,7 @@ GraphTracer.prototype.clear = function () {
GraphTracer.prototype.createRandomData = function (N, ratio) {
Tracer.prototype.createRandomData.call(this, arguments);
if (!N) N = 5;
if (!ratio) ratio = .3;
var G = [];
for (var i = 0; i < N; i++) {
......@@ -39,9 +40,51 @@ GraphTracer.prototype.createRandomData = function (N, ratio) {
return G;
};
GraphTracer.prototype.setTreeData = function (G, root) {
root = root || 0;
var maxDepth = -1;
var chk = [];
for (var i = 0; i < G.length; i++) chk.push(false);
var getDepth = function (node, depth) {
if (chk[node]) throw "the given graph is not a tree because it forms a circuit";
chk[node] = true;
if (maxDepth < depth) maxDepth = depth;
for (var i = 0; i < G[node].length; i++) {
if (G[node][i]) getDepth(i, depth + 1);
}
};
getDepth(root, 1);
if (this.setData(G, root)) return true;
var place = function (node, x, y) {
var temp = graph.nodes(n(node));
temp.x = x;
temp.y = y;
};
var wgap = 1 / (maxDepth - 1);
var dfs = function (node, depth, top, bottom) {
place(node, depth * wgap, (top + bottom) / 2);
var children = 0;
for (var i = 0; i < G[node].length; i++) {
if (G[node][i]) children++;
}
var vgap = (bottom - top) / children;
var cnt = 0;
for (var i = 0; i < G[node].length; i++) {
if (G[node][i]) dfs(i, depth + 1, top + vgap * cnt, top + vgap * ++cnt);
}
};
dfs(root, 0, 0, 1);
this.refresh();
};
// Override
GraphTracer.prototype.setData = function (G) {
if (Tracer.prototype.setData.call(this, arguments)) return;
if (Tracer.prototype.setData.call(this, arguments)) return true;
graph.clear();
var nodes = [];
......@@ -82,6 +125,8 @@ GraphTracer.prototype.setData = function (G) {
ratio: 1
});
this.refresh();
return false;
};
GraphTracer.prototype._clear = function () {
......
......@@ -20,6 +20,7 @@ WeightedGraphTracer.prototype.clear = function () {
WeightedGraphTracer.prototype.createRandomData = function (N, ratio, min, max) {
Tracer.prototype.createRandomData.call(this, arguments);
if (!N) N = 5;
if (!ratio) ratio = .3;
if (!min) min = 1;
if (!max) max = 5;
......
......@@ -20,8 +20,9 @@ dataEditor.on('change', function () {
});
var loadFile = function (category, algorithm, file, explanation) {
lastData = null;
$('#explanation').html(explanation);
dataEditor.setValue('var G = [];', -1);
dataEditor.setValue('');
codeEditor.setValue('');
var dir = './algorithm/' + category + '/' + algorithm + '/' + file + '/';
......@@ -49,7 +50,7 @@ var loadAlgorithm = function (category, algorithm) {
$('#desc_ref').empty();
$('.files_bar').empty();
$('#explanation').html('');
dataEditor.setValue('var G = [];', -1);
dataEditor.setValue('');
codeEditor.setValue('');
var dir = './algorithm/' + category + '/' + algorithm + '/';
......
var timer = null;
var lastModule = null, lastData = null;
var lastData = null;
var stepLimit = 1e6;
var Tracer = function (module) {
......@@ -9,7 +9,6 @@ var Tracer = function (module) {
this.traceOptions = null;
this.traceIndex = -1;
this.stepCnt = 0;
lastData = null;
};
Tracer.prototype.resize = function () {
......@@ -31,8 +30,7 @@ Tracer.prototype.createRandomData = function (arguments) {
Tracer.prototype.setData = function (arguments) {
var data = JSON.stringify(arguments);
if (lastModule == this.module && lastData == data) return true;
lastModule = this.module;
if (lastData == data) return true;
lastData = data;
return false;
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册