-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraph.js
More file actions
155 lines (136 loc) · 3.65 KB
/
graph.js
File metadata and controls
155 lines (136 loc) · 3.65 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
var textBox = document.getElementById('value');
var msg = document.getElementById('msg');
function InsertNode() {
var val = Number(textBox.value || getRandomInt(0, 1000));
bst.InsertVal(val);
textBox.value = '';
msg.innerHTML = '';
textBox.focus();
drawTree();
}
function DeleteNode() {
if (textBox.value != '') {
var val = Number(textBox.value);
bst.DeleteVal(val);
textBox.value = '';
drawTree();
}
msg.innerHTML = '';
textBox.focus();
}
function SearchNode() {
if (textBox.value != '') {
var val = Number(textBox.value);
var node = bst.Search(val);
if (node == -1)
msg.innerHTML = 'not found';
else
msg.innerHTML = 'found';
}
textBox.value = '';
textBox.focus();
}
// traversal is a function
function Print(traversal) {
var numbers = traversal.call(bst);
msg.innerHTML = numbers.join(', ');
textBox.focus();
}
function handleKeyPress(e) {
var key = e.keyCode || e.which;
msg.innerHTML = '';
if (key == 13)
InsertNode();
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Draws the tree in treeData
function drawTree() {
// Clear the canvas
d3.select("svg").remove();
// This json represents the tree
var treeData;
// If the tree is not empty
if (bst.root)
treeData = bst.root.json;
else
return;
// set the dimensions and margins of the diagram
var margin = {
top: 40,
right: 90,
bottom: 50,
left: 90
},
width = window.innerWidth - 10 - margin.left - margin.right,
height = window.innerHeight - 45 - margin.top - margin.bottom;
// declares a tree layout and assigns the size
var treemap = d3.tree()
.size([width, height]);
// assigns the data to a hierarchy using parent-child relationships
var nodes = d3.hierarchy(treeData);
// maps the node data to the tree layout
nodes = treemap(nodes);
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom),
g = svg.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// adds the links between the nodes
var link = g.selectAll(".link")
.data(nodes.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
// If its child is the only one
// move it to the right or to the left
// (the D3.js tree's default will put the node
// exactly below its parent)
if (d.parent && d.parent.children.length == 1) {
if (d.data.direction == 'right') {
if (d.parent.parent)
d.x += Math.abs(d.parent.x - d.parent.parent.x) / 2;
else
d.x += width / 4;
} else {
if (d.parent.parent)
d.x -= Math.abs(d.parent.x - d.parent.parent.x) / 2;
else
d.x -= width / 4;
}
}
return "M" + d.x + "," + d.y +
"C" + (d.x + d.parent.x) / 2 + "," + (d.y + d.parent.y) / 2 +
" " + (d.x + d.parent.x) / 2 + "," + (d.y + d.parent.y) / 2 +
" " + d.parent.x + "," + d.parent.y;
});
// adds each node as a group
var node = g.selectAll(".node")
.data(nodes.descendants())
.enter().append("g")
.attr("class", function(d) {
return "node" +
(d.children ? " node--internal" : " node--leaf");
})
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
// adds the circle to the node
node.append("circle")
.attr("r", 15);
// adds the text to the node
node.append("text")
.attr("dy", ".35em")
.attr("y", function(d) {
return 0;
})
.style("text-anchor", "middle")
.text(function(d) {
return d.data.name;
});
}