【优先队列】java代码实现

思路

优先队列是在遵循常规队列先进先出的规则下,采用最大最小堆排序思路实现的,保证了一定有序性同时保证运算量相对较小,这里仅实现最大堆的情况,最小堆类似

代码实现

构造类

单纯为实现思路,所有用了arrayList

private List<Integer> queue = new ArrayList<Integer>();

入队方法

入队方法从队尾插入,这里抽象化表示二叉树节点关系,左子树为其节点(数位+1)X2-1,右节点为其节点(数位+1)X2的位置,某节点的父节点为其节点(数位-1)/2取整,插入之后由插入位开始与其父节点进行比较,大于则交换,若交换则对交换后的父节点再次执行该方法,直至根节点,以此在遍历少量数据的情况下完成队列排序

    public void put(Integer num){
        queue.add(num);
        this.exChange(this.queue.size()-1);
    }
    private void exChange(int listLength){
        if(listLength==0){
            return;
        }
        if(this.queue.get((listLength-1)/2)<this.queue.get(listLength)){
            Integer a = this.queue.get((listLength-1)/2);
            this.queue.set((listLength-1)/2,this.queue.get(listLength));
            this.queue.set(listLength,a);
            exChange((listLength-1)/2);
        }
    }

出队方法

出队方法为取队头并返回,而为保证队列有序性,需要有节点填充至队头的位置,这里我选择取队尾数据取代队头,然后调用同左右子树比较的方法,将队尾数据向下传递

    public Integer take(){
        Integer num = this.queue.get(0);
        int ord = 1;
        this.queue.set(0,this.queue.get(this.queue.size()-1));
        compareEchange(1*2-1,1*2,0);
        return num;
    }

    private void compareEchange(int num1,int num2,int root){
        if((this.queue.size()-1)<num1){
            return;
        }
        if((this.queue.size()-1)==num1){
            if(this.queue.get(root)<this.queue.get(num1)){
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num1));
                this.queue.set(num1,x);
            }
        }
        if((this.queue.size()-1)>=num2){
            if(this.queue.get(num1)>this.queue.get(num2)){
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num1));
                this.queue.set(num1,x);
                compareEchange(num1*2-1,num1*2,num1);
            }else{
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num2));
                this.queue.set(num2,x);
                compareEchange(num2*2-1,num2*2,num2);
            }
        }
    }

完整代码

package PriorityQueue;


import java.util.ArrayList;
import java.util.List;

public class PriorityQueue {

    private List<Integer> queue = new ArrayList<Integer>();

    public void put(Integer num){
        queue.add(num);
        this.exChange(this.queue.size()-1);
    }
    private void exChange(int listLength){
        if(listLength==0){
            return;
        }
        if(this.queue.get((listLength-1)/2)<this.queue.get(listLength)){
            Integer a = this.queue.get((listLength-1)/2);
            this.queue.set((listLength-1)/2,this.queue.get(listLength));
            this.queue.set(listLength,a);
            exChange((listLength-1)/2);
        }
    }
    public Integer take(){
        Integer num = this.queue.get(0);
        int ord = 1;
        this.queue.set(0,this.queue.get(this.queue.size()-1));
        compareEchange(1*2-1,1*2,0);
        return num;
    }

    private void compareEchange(int num1,int num2,int root){
        if((this.queue.size()-1)<num1){
            return;
        }
        if((this.queue.size()-1)==num1){
            if(this.queue.get(root)<this.queue.get(num1)){
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num1));
                this.queue.set(num1,x);
            }
        }
        if((this.queue.size()-1)>=num2){
            if(this.queue.get(num1)>this.queue.get(num2)){
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num1));
                this.queue.set(num1,x);
                compareEchange(num1*2-1,num1*2,num1);
            }else{
                int x = this.queue.get(root);
                this.queue.set(root,this.queue.get(num2));
                this.queue.set(num2,x);
                compareEchange(num2*2-1,num2*2,num2);
            }
        }
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值