nLeftIndex = 2 * (nFatherIndex+1) - 1; nRightIndex = 2* (nFatherIndex+1);
/******************************************************
* file : 堆
* author : "page"
* time : "2016/11/02"
*******************************************************/
function Heap()
{
this.data = [];
}
Heap.prototype.print = function () {
console.log("Heap: " + this.data);
}
Heap.prototype.build = function(data){
// 初始化
this.data = [];
if (!data instanceof Array)
return false;
// 入堆
for (var i = 0; i < data.length; ++i) {
this.insert(data[i]);
}
return true;
}
Heap.prototype.insert = function( nValue ){
if (!this.data instanceof Array) {
this.data = [];
}
this.data.push(nValue);
// 更新新节点
var nIndex = this.data.length-1;
var nFatherIndex = Math.floor((nIndex-1)/2);
while (nFatherIndex > 0){
if (this.data[nIndex] < this.data[nFatherIndex]) {
var temp = this.data[nIndex];
this.data[nIndex] = this.data[nFatherIndex];
this.data[nFatherIndex] = temp;
}
nIndex = nFatherIndex;
nFatherIndex = Math.floor((nIndex-1)/2);
}
}
Heap.prototype.delete = function( ){
if (!this.data instanceof Array) {
return null;
}
var nIndex = 0;
var nValue = this.data[nIndex];
var nMaxIndex = this.data.length-1;
// 更新新节点
var nLeaf = this.data.pop();
this.data[nIndex] = nLeaf;
while (nIndex < nMaxIndex ){
var nLeftIndex = 2 * (nIndex+1) - 1;
var nRightIndex = 2 * (nIndex+1);
// 找最小的一个子节点(nLeftIndex < nRightIndex)
var nSelectIndex = nLeftIndex;
if (nRightIndex < nMaxIndex) {
nSelectIndex = (this.data[nLeftIndex] > this.data[nRightIndex]) ? nRightIndex : nLeftIndex;
}
if (nSelectIndex < nMaxIndex && this.data[nIndex] > this.data[nSelectIndex] ){
var temp = this.data[nIndex];
this.data[nIndex] = this.data[nSelectIndex];
this.data[nSelectIndex] = temp;
}
nIndex = nSelectIndex;
}
return nValue;
}
// test
var heap = new Heap();
heap.build([1, 3, 5, 11, 4, 6, 7, 12, 15, 10, 9, 8]);
heap.print();
// insert
heap.insert(2);
heap.print();
// delete
heap.delete();
heap.print();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有