| 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 |
var Splay = new BenchmarkSuite('Splay', 126125, [ |
| 37 |
new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown) |
| 38 |
]); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
var kSplayTreeSize = 8000; |
| 43 |
var kSplayTreeModifications = 80; |
| 44 |
var kSplayTreePayloadDepth = 5; |
| 45 |
|
| 46 |
var splayTree = null; |
| 47 |
|
| 48 |
|
| 49 |
function GeneratePayloadTree(depth, key) { |
| 50 |
if (depth == 0) { |
| 51 |
return { |
| 52 |
array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], |
| 53 |
string : 'String for key ' + key + ' in leaf node' |
| 54 |
}; |
| 55 |
} else { |
| 56 |
return { |
| 57 |
left: GeneratePayloadTree(depth - 1, key), |
| 58 |
right: GeneratePayloadTree(depth - 1, key) |
| 59 |
}; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function GenerateKey() { |
| 65 |
|
| 66 |
|
| 67 |
return Math.random(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
function InsertNewNode() { |
| 72 |
|
| 73 |
var key; |
| 74 |
do { |
| 75 |
key = GenerateKey(); |
| 76 |
} while (splayTree.find(key) != null); |
| 77 |
splayTree.insert(key, GeneratePayloadTree(kSplayTreePayloadDepth, key)); |
| 78 |
return key; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
function SplaySetup() { |
| 84 |
splayTree = new SplayTree(); |
| 85 |
for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
function SplayTearDown() { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
var keys = splayTree.exportKeys(); |
| 94 |
splayTree = null; |
| 95 |
|
| 96 |
|
| 97 |
var length = keys.length; |
| 98 |
if (length != kSplayTreeSize) { |
| 99 |
throw new Error("Splay tree has wrong size"); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
for (var i = 0; i < length - 1; i++) { |
| 104 |
if (keys[i] >= keys[i + 1]) { |
| 105 |
throw new Error("Splay tree not sorted"); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
function SplayRun() { |
| 112 |
|
| 113 |
for (var i = 0; i < kSplayTreeModifications; i++) { |
| 114 |
var key = InsertNewNode(); |
| 115 |
var greatest = splayTree.findGreatestLessThan(key); |
| 116 |
if (greatest == null) splayTree.remove(key); |
| 117 |
else splayTree.remove(greatest.key); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function SplayTree() { |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
SplayTree.prototype.root_ = null; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
SplayTree.prototype.isEmpty = function() { |
| 147 |
return !this.root_; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
SplayTree.prototype.insert = function(key, value) { |
| 160 |
if (this.isEmpty()) { |
| 161 |
this.root_ = new SplayTree.Node(key, value); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
this.splay_(key); |
| 167 |
if (this.root_.key == key) { |
| 168 |
return; |
| 169 |
} |
| 170 |
var node = new SplayTree.Node(key, value); |
| 171 |
if (key > this.root_.key) { |
| 172 |
node.left = this.root_; |
| 173 |
node.right = this.root_.right; |
| 174 |
this.root_.right = null; |
| 175 |
} else { |
| 176 |
node.right = this.root_; |
| 177 |
node.left = this.root_.left; |
| 178 |
this.root_.left = null; |
| 179 |
} |
| 180 |
this.root_ = node; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
SplayTree.prototype.remove = function(key) { |
| 193 |
if (this.isEmpty()) { |
| 194 |
throw Error('Key not found: ' + key); |
| 195 |
} |
| 196 |
this.splay_(key); |
| 197 |
if (this.root_.key != key) { |
| 198 |
throw Error('Key not found: ' + key); |
| 199 |
} |
| 200 |
var removed = this.root_; |
| 201 |
if (!this.root_.left) { |
| 202 |
this.root_ = this.root_.right; |
| 203 |
} else { |
| 204 |
var right = this.root_.right; |
| 205 |
this.root_ = this.root_.left; |
| 206 |
|
| 207 |
this.splay_(key); |
| 208 |
|
| 209 |
|
| 210 |
this.root_.right = right; |
| 211 |
} |
| 212 |
return removed; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
SplayTree.prototype.find = function(key) { |
| 224 |
if (this.isEmpty()) { |
| 225 |
return null; |
| 226 |
} |
| 227 |
this.splay_(key); |
| 228 |
return this.root_.key == key ? this.root_ : null; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
SplayTree.prototype.findGreatestLessThan = function(key) { |
| 237 |
if (this.isEmpty()) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
this.splay_(key); |
| 243 |
|
| 244 |
|
| 245 |
if (this.root_.key <= key) { |
| 246 |
return this.root_; |
| 247 |
} else if (this.root_.left) { |
| 248 |
return this.findMax(this.root_.left); |
| 249 |
} else { |
| 250 |
return null; |
| 251 |
} |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
SplayTree.prototype.exportKeys = function() { |
| 259 |
var result = []; |
| 260 |
if (!this.isEmpty()) { |
| 261 |
this.root_.traverse_(function(node) { result.push(node.key); }); |
| 262 |
} |
| 263 |
return result; |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
SplayTree.prototype.splay_ = function(key) { |
| 278 |
if (this.isEmpty()) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
var dummy, left, right; |
| 287 |
dummy = left = right = new SplayTree.Node(null, null); |
| 288 |
var current = this.root_; |
| 289 |
while (true) { |
| 290 |
if (key < current.key) { |
| 291 |
if (!current.left) { |
| 292 |
break; |
| 293 |
} |
| 294 |
if (key < current.left.key) { |
| 295 |
|
| 296 |
var tmp = current.left; |
| 297 |
current.left = tmp.right; |
| 298 |
tmp.right = current; |
| 299 |
current = tmp; |
| 300 |
if (!current.left) { |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
right.left = current; |
| 306 |
right = current; |
| 307 |
current = current.left; |
| 308 |
} else if (key > current.key) { |
| 309 |
if (!current.right) { |
| 310 |
break; |
| 311 |
} |
| 312 |
if (key > current.right.key) { |
| 313 |
|
| 314 |
var tmp = current.right; |
| 315 |
current.right = tmp.left; |
| 316 |
tmp.left = current; |
| 317 |
current = tmp; |
| 318 |
if (!current.right) { |
| 319 |
break; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
left.right = current; |
| 324 |
left = current; |
| 325 |
current = current.right; |
| 326 |
} else { |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
left.right = current.left; |
| 332 |
right.left = current.right; |
| 333 |
current.left = dummy.right; |
| 334 |
current.right = dummy.left; |
| 335 |
this.root_ = current; |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
SplayTree.Node = function(key, value) { |
| 346 |
this.key = key; |
| 347 |
this.value = value; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
SplayTree.Node.prototype.left = null; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
SplayTree.Node.prototype.right = null; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
SplayTree.Node.prototype.traverse_ = function(f) { |
| 371 |
var current = this; |
| 372 |
while (current) { |
| 373 |
var left = current.left; |
| 374 |
if (left) left.traverse_(f); |
| 375 |
f(current); |
| 376 |
current = current.right; |
| 377 |
} |
| 378 |
}; |