-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
300 lines (240 loc) · 6.4 KB
/
index.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hashing: Double Hashing</title>
<!--
<script type="text/javascript" src="myjavascript.js"></script>
<link href="course.css" rel="stylesheet" type="text/css">
-->
<style type="text/css"><!--
/* internal styles */
body {
font-family: "Trebuchet MS", Skia, "Lucida Sans Unicode", sans-serif;
font-size: 16px;
margin: 2.5em auto 5em 5% ;
width: 90%;
}
#header {
margin: 3em 0 ;
border: 3px #369;
border-style: solid solid ;
padding: 0em 2em 0 ;
text-align: center ;
}
#header .title {
font-size: 1.6em ;
margin-bottom: 0 ;
}
#footer {
margin: 3em 0 ;
/*border: 2px #036;*/
border: 3px #369;
border-style: solid solid ;
padding: .7em ;
}
#footer img {
border: none ;
}
.section {
font-size: 1.1em ;
font-weight: bold ;
}
a {
color: #339 ;
text-decoration: none ;
}
.prebox {
background: #ffc;
overflow: scroll;
}
.prebox span {
background: #fcc ;
}
.bucketnumber {
margin-top: 4.5em;
text-align: center;
color: #555 ;
}
.buckettext {
float: left;
text-align: center;
width: 100%;
margin-top: 1em ;
font-size: 1.5em ;
}
.bucketbox {
float: left; width: 4em; height: 4em;
}
#hashtablecontainer {
height: 8em ;
}
--></style>
<script type="text/javascript">
var TABLE_SIZE = 16 ;
var numElementsInTable = 0;
var bucketcolor = new Array(
'#8ff',
'#ff8',
'#dca',
'#f88',
'#8ff',
'#ff8',
'#dca',
'#f88',
'#8ff',
'#ff8',
'#dca',
'#f88',
'#8ff',
'#ff8',
'#dca',
'#f88'
);
function setElementValue(elementId, value) {
if ( document.getElementById(elementId).innerText!=undefined ) {
document.getElementById(elementId).innerText = value ;
} else {
document.getElementById(elementId).textContent = value ;
}
}
function setCellColor(cell, color) {
document.getElementById(cell).style.background = color ;
}
function makeBucketHTMLString(i) {
return '<div class="bucketbox" id="bb_'+i+'" style="background: '+bucketcolor[i]+';">'
+'<div id="bucket_'+i+'" class="buckettext"><'
+ '/div><div class="bucketnumber">'+i+'<'+'/div><'+'/div>' ;
}
function initElement() {
var el = document.getElementById("hashtablecontainer") ;
var i ;
for (i=0;i<TABLE_SIZE;i++ ) {
el.innerHTML = el.innerHTML + makeBucketHTMLString(i) ;
}
}
function setElementValue(elementId, value) {
if ( document.getElementById(elementId).innerText!=undefined ) {
document.getElementById(elementId).innerText = value ;
} else {
document.getElementById(elementId).textContent = value ;
}
}
function getElementValue(elementId) {
if ( document.getElementById(elementId).innerText!=undefined ) {
//window.alert("if statement" + document.getElementById(elementId).innerText );
return document.getElementById(elementId).innerText ;
} else {
//window.alert("else statement" + document.getElementById(elementId).textContent );
return document.getElementById(elementId).textContent ;
}
}
function getFromBucket(b) {
// check for IE or Gecko. This is Gecko only:
var to_return = getElementValue('bucket_'+b);
//window.alert(to_return);
return to_return ;
}
function putToBucket(b,t) {
// check for IE or Gecko. This is Gecko only:
setElementValue('bucket_'+b,t) ;
document.getElementById('bucket_'+b).style.color="#0a0" ;
}
function makeBucketsBlack() {
var i ;
for ( i=0;i<TABLE_SIZE;i++ )
document.getElementById('bucket_'+i).style.color="#000" ;
}
function colorBucketTouched(i) {
//window.alert("testing coloer buckettouched start");
document.getElementById('bucket_'+i).style.color="#a00" ;
//window.alert("testing coloer buckettouched end");
}
function clearInputValues() {
var i ;
for ( i=0;i<TABLE_SIZE;i++ ) {
putToBucket(i,"") ;
}
}
function calculateHash(to_insert, i){
var hash_one = to_insert%TABLE_SIZE ;
var hash_two = 2*(to_insert%4) + 1;
var hash = (hash_one + i*hash_two) % TABLE_SIZE;
return hash;
}
function hashTableInsert(j) {
if(numElementsInTable ==TABLE_SIZE){
//window.alert("condition met");
clearInputValues()
}
var i = 0 ;
var hash = calculateHash(j, i);
//window.alert(hash);
while ( getFromBucket(hash) != "" ) {
//window.alert("testing entering while loop");
colorBucketTouched(hash) ;
//window.alert("testing more");
i++ ;
hash = calculateHash(j, i);
//window.alert(hash);
if ( i>TABLE_SIZE ) break ;
}
//window.alert("testing");
putToBucket(hash,j) ;
numElementsInTable ++;
}
function getInputValue() {
var s = document.getElementById('hashin').value ;
var j = parseInt(s) ;
makeBucketsBlack() ;
if ( !isNaN(j) ) {
hashTableInsert(j) ;
}
document.getElementById('hashin').value="" ;
}
</script>
</head>
<body onload="initElement();">
<div id="header">
<h1>
Hashing: Double Hashing
</h1>
</div>
<h2>
Hashtable
</h2>
<div id="hashtablecontainer">
<!-- left margin -->
<div style="float: left; width: 2em;"> </div>
<!-- javascript will create buckets here -->
<!-- background color for the buckets is described by entries in array bucketcolor -->
</div>
<h2>Input</h2>
<!-- left margin -->
<div style="float: left; width: 2em;"> </div>
<table>
<tr>
<th style="background: #cfc; width: 10em;">Value
<td><input type="text" id="hashin">
<tr>
<th style="border: solid red 2px; background: #eef;"
onclick="getInputValue()"> Enter
<th style="border: solid red 2px; background: #eef;"
onclick="clearInputValues()"> Clear
</table>
<h2>
Overview
</h2>
<p>
This animation demonstrates a hash table that utilizes double hashing. The first hash function is H1 =
key mod 16. The second hash function is H2 = 2*(key mod 4) + 1. Using the values of H1 and H2 we can map
to a slot in out table, we use the function (H1 + iH2) mod 16 to do this.
<p>
This animation demonstates a successful insertion with the color green, and indicates that a collision
occurred when inserting using the color.
<p>
Upon insertion while the table is at capacity, the table will clear and then the key will
be inserted. This hash table also allows for duplicate key values.
</p>
</body>
</html>