nodejs实现最大堆

nodejs实现最大堆

/**
 * 最大堆
 * 一般采用数组实现,否则计算节点太复杂,因为树并不是完全有序的。
 * 为了计算方便,第一个元素不用。
 * https://baike.baidu.com/item/%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91/7773232?fr=aladdin
 * https://blog.csdn.net/qwezhaohaihong/article/details/51050778
 */
/*function isLeft(i){
    return i%2 === 0;
}
function isRight(i){
    return i%2 === 1;
}
function isRoot(i){
    return i===1;
}*/
function parent(i){
    return i/2|0;//if i>1  按位或0,可以将小数向下取整。
}
function leftChild(i){
    return i*2;//if 2*i<=n
}
function rightChild(i){
    return 2*i+1;//if 2*i+1<=n
}
/*function isLeaf(i){
    return i>n/2|0;
}
function hasTwoChild(i){
    return i<(n-1)/2|0;
}*/
function shiftUp(heap,i){
    let nodes = heap.nodes;
    let p = parent(i);
    if(p>0 && shift(heap,i,p)){
        shiftUp(heap,p);
    }
}
function shift(heap,i,p){
    let nodes = heap.nodes;
    if(heap.cmp(nodes[i],nodes[p])>0){
        let tmp = nodes[i];
        nodes[i] = nodes[p];
        nodes[p] = tmp;
        return true;//产生了交换
    }
    return false;
}
function shiftDown(heap,i){
    let nodes = heap.nodes;
    let left = leftChild(i);
    let right = rightChild(i);
    if(left<=heap.size()){//left<=size//有左孩子
        if(right<=heap.size()){//有右孩子
            if(heap.cmp(nodes[left],nodes[right])>0){//比较左右孩子大小
                if(shift(heap,left,i)){
                    shiftDown(heap,left);
                }
            }else{
                if(shift(heap,right,i)){
                    shiftDown(heap,right);
                }
            }
        }else{//
            if(shift(heap,left,i)){
                shiftDown(heap,left);
            }
        }
    }
}
const proto = {
        pop:function(){
            let size = this.size();
            if(this.size()==0){
                return null;
            }
            let res = this.nodes[1];
            this.nodes[1] = this.nodes[size];
            this.nodes.length = size;
            shiftDown(this,1);
        },
        peek:function(){
            if(this.nodes.size()>=1){
                return this.nodes[1];
            }
            return null;
        },
        add:function(obj){
            if(obj == null){
                throw new Error('obj must not be null');
            }
            this.nodes.push(obj);
            shiftUp(this,this.size());
        },
        size:function(){
            return this.nodes.length-1;
        }
};
let newHeap = function(cmp){
    if(cmp==null){
        throw new Error('cmp must not be null');
    }
    const heap = Object.create(proto);
    heap.nodes = [0];
    heap.cmp = cmp;
    return heap;
};
function main(){
    let nums = [10,8,15,20,7,3,24,14,2,1];
    let heap = newHeap((o1,o2)=>o1-o2);
    nums.forEach(i=>{
        heap.add(i);
        console.info(heap);
    });
}
main();
module.exports = {
        newHeap
};
基于Javascript的bitmap处理,并且将位图输出为base64编码以便于浏览器进行显示。   一、Bitmap.create(width, height, bgcolor)     创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。     如:bitmap.create(10, 10, 0xff0000); // 创建一个10 x 10像素的底色为红色的位图 二、Bitmap.toBase64()     将位图输出为base64编码的带datauri头(data:image/bmp;base64,)的字符串,以便于在浏览器里显示。     如:document.getElementById('img1').src = bitmap.toBase64(); 三、Bitmap.fromBase64()     自图像的BASE64编码中恢复位图数据,目前只支持24位色的BMP位图数据。     如:bitmap.fromBase64('Qk06AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABgAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='); 四、Bitmap.setBitmapBytes(val, idx, length)     修改bitmap位图数据的第idx位置起的length字节为val值。 五、Bitmap.getBitmapBytes(idx, length)     获取bitmap位图数据的第idx位置起的length个字节的值,返回值为数组。 六、Bitmap.setHeaderValue(attribute, headerValue)     设置attribute头属性的值为headerValue,attribute必须为BitMapFormat的成员属性,需要提供offset、length等属性值。     如:bitmap.setHeaderValue(BitmapFormat.biWidth, 500); // 设置位图的宽度为500像素值 七、Bitmap.getHeaderValue(attribute)     获取位图attribute头属性的值,attribute必须为BitmapFormat的成员属性,需要提供offset、length等属性值,返回的是经过Endian转换后的实际整数值。 八、Bitmap.setPixel(x, y, color)     设置位图的(x, y)位置的像素值为color。 九、Bitmap.getPixel(x, y)     获取位图的(x, y)位置的RGB值,返回的内容为[ rr, gg, bb ]的数组内容 标签:jsBitmap
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值