-
Notifications
You must be signed in to change notification settings - Fork 0
/
splay.html
277 lines (259 loc) · 7.37 KB
/
splay.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META http-equiv="X-UA-Compatible" content="IE=9" > </META>
<title>Spaly Tree</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link rel="stylesheet" href="css/avl.css"/>
</head>
<body>
<div id="links" align="center">
<a href="avl.html">AVL</a>
<a href="splay.html">Splay</a>
</div>
<h1 align="center">Splay Tree</h1>
<div id="input" align="center">
<p id="svgArg">
Screen:
#Step:
<select id="svgNum">
<option>1</option>
<option selected>2</option>
<option>3</option>
<option>4</option>
</select>
Scale:
<select id="treeScale">
<option>0.50</option>
<option>0.75</option>
<option selected>1.00</option>
<option>1.25</option>
</select>
Width:
<select id="svgWidth">
<option selected>50%</option>
<option>100%</option>
</select>
Height:
<select id="svgHeight">
<option selected>50%</option>
<option>75%</option>
<option>100%</option>
</select>
</p>
<p>
<input id="bbst" type="checkbox" checked>BBST
<button id="history">History</button>
<input id="num" type="text" autocomplete="off">
<button id="insert">Insert</button>
<button id="remove">Remove</button>
<select id="removeSwap">
<option selected>pred</option>
<option>succ</option>
<option>rand</option>
</select>
<button id="removeAll">RemoveAll</button>
<button id="search">Search</button>
</p>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/d3.v3.min.js"></script>
<script type="text/javascript" src="js/draw.js"></script>
<script type="text/javascript" src="js/treenode.js"></script>
<script type="text/javascript" src="js/splay.js"></script>
<script type="text/javascript" src="js/bst.js"></script>
<script type="text/javascript">
var drawy = 20;
var initRandomSize = 10;
var randomSize = Math.ceil(initRandomSize / $("#svgNum").val());
var thistree, splayCache, bstCache;
var choose, drawroot, history, svg, svgNum;
start();
function func(op) {
$("#svgNum option[value=" + svgNum + "]").attr("selected", "true");
var input = $("#num").val();
$("#num").attr("value", "");
var opRecord;
if ($("#bbst").attr("checked") != null) {
opRecord = "Splay " + op + " ";
}else {
opRecord = "BST " + op + " ";
}
if (op == "Remove") {
opRecord += "(" + $('#removeSwap').val() + ") ";
}
if (op == "RemoveAll") {
bstCache.removeAll();
splayCache.removeAll();
}else {
if (input == null) return;
input = input.split(" ");
var nums = [];
for (var i = 0; i < input.length; ++i) {
var num = parseInt(input[i]);
if (!isNaN(num)) {
nums.push(num);
}
}
if (nums.length == 0) {
//return; //uncomment this to make it unavailable to add random number
nums.push(Math.ceil(Math.random() * 100));
}
for (var i = 0; i < nums.length; ++i) {
var num = nums[i];
if (op == "Insert") {
bstCache.insert(num);
splayCache.insert(num);
}
if (op == "Remove") {
bstCache.remove(num, $('#removeSwap').val());
splayCache.remove(num, $('removeSwap').val());
}
if (op == "Search") {
bstCache.search(num);
splayCache.search(num);
}
opRecord += num + " ";
//break; //comment this to make it available to deal with many numbers once
}
}
drawTree(thistree.process);
history += opRecord + "\n";
}
$("#insert").click(function() {
func("Insert");
})
$("#remove").click(function() {
func("Remove");
})
$("#search").click(function() {
func("Search");
})
$("#removeAll").click(function() {
func("RemoveAll");
})
function genDrawingTree(node) {
if (node == null) return null;
var left = genDrawingTree(node.left);
var right = genDrawingTree(node.right);
var root = new Node(node.value);
if (left != null) root.appendleft(left);
if (right != null) root.appendright(right);
return root;
}
function drawTree(output) {
if (drawroot[choose] != null) {
drawroot[choose].remove();
}
drawroot[choose] = genDrawingTree(thistree.root);
if (drawroot[choose] != null) {
drawroot[choose].getHlign();
var pos = drawroot[choose].hlignl / (drawroot[choose].hlignl + drawroot[choose].hlignr);
pos *= parseInt($("#svgWidth").val()) / 100;
drawroot[choose].draw(svg[choose], document.body.clientWidth * pos, drawy * $("#treeScale").val(), $("#treeScale").val());
}
if (output != null) {
$('#process' + choose).html(output);
}
choose = (choose + 1) % svgNum;
}
function reDrawTrees() {
for (var i = 0; i < svgNum; ++i) {
if (drawroot[i] != null) {
drawroot[i].remove();
var pos = drawroot[i].hlignl / (drawroot[i].hlignl + drawroot[i].hlignr);
pos *= parseInt($("#svgWidth").val()) / 100;
drawroot[i].draw(svg[i], document.body.clientWidth * pos, drawy * $("#treeScale").val(), $("#treeScale").val());
}
}
}
function randomData() {
var output;
if ($("#bbst").attr("checked") != null) {
output = "Splay Insert ";
}else {
output = "BST Insert ";
}
for (var i = 0; i < randomSize; ++i) {
var num = Math.ceil(Math.random() * 100);
bstCache.insert(num);
splayCache.insert(num);
output += num + " ";
}
drawTree(output);
history += output + "\n";
};
function start() {
svgNum = $('#svgNum').val();
var divsvg = $('.divsvg');
if (divsvg) {
divsvg.remove();
}
for (var i = 0; i < svgNum; ++i) {
$('body').append('<div class="divsvg" id="divsvg' + i + '"align="center"></div>');
}
divsvg = $('.divsvg');
var cssWidth = parseInt(100 / svgNum) + "%";
divsvg.css('width', cssWidth);
var svgWidth = $("#svgWidth");
svgWidth.empty();
svgWidth.append($("<option>").text(cssWidth).attr("selected", true));
for (var i = svgNum - 1; i >= 1; --i) {
var widthOption = parseInt(100 / i) + "%";
svgWidth.append($("<option>").text(widthOption));
}
bstCache = Bst();
splayCache = Splay();
if ($("#bbst").attr("checked") != null) {
thistree = splayCache;
}else {
thistree = bstCache;
}
choose = 0;
drawroot = [];
svg = [];
history = "";
for (var i = 0; i < divsvg.length; ++i) {
svg.push(d3.select('#divsvg'+i).append('svg'));
$('#divsvg'+i).append('<p class="process" id="process' + i + '"></p>');
}
randomSize = Math.ceil(initRandomSize / svgNum);
randomData();
updateSvgHeight();
}
$("#svgNum").change(start);
$("#history").click(function() {
alert(history);
});
function updateSvgHeight() {
value = $("#svgHeight").val();
$(".divsvg").css("height", value);
}
$("#svgHeight").change(updateSvgHeight);
function updateSvgWidth() {
$(".divsvg").css("width", $("#svgWidth").val());
reDrawTrees();
}
$("#svgWidth").change(updateSvgWidth);
function updateTreeScale() {
reDrawTrees();
}
$("#treeScale").change(updateTreeScale);
$("#bbst").change(function() {
if ($("#bbst").attr("checked") != null) {
thistree = splayCache;
var output = "Splay choosed";
drawTree(output);
history += output + "\n";
}else {
thistree = bstCache;
splayCache.cloneTo(bstCache);
var output = "BST choosed";
drawTree(output);
history += output + "\n";
}
})
</script>
</body>
</html>