欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

树形结构----最大堆与优先队列

程序员文章站 2022-03-31 19:08:30
...

树形结构----最大堆与优先队列
树形结构----最大堆与优先队列
树形结构----最大堆与优先队列

package 树形结构;

import shixianClass.ArrayList;

import java.util.Iterator;

//最大堆
public class MaxHeap<E extends Comparable<E>> implements Iterable<E> {
    //用ArrayList当做最大堆的存储容器
    private ArrayList<E> data;
    public MaxHeap(){
        data=new ArrayList<>();
    }
    //获取父结点的角标
    private int parent(int k){
        if(k<=0){
            throw new IllegalArgumentException("没有父结点");
        }
        return (k-1)/2;
    }

    //获取左孩子结点的角标
    private int leftChild(int k){
        return 2*k+1;
    }
    //获取左孩子结点的角标
    private int rightChild(int k){
        return 2*k+2;
    }

    public int size(){
        return data.size();
    }
    public  boolean isEmpty(){
        return data.isEmpty();
    }

    public void clear(){
        data.clear();
    }
    //向最大堆中添加一个元素E
    public void add(E e){
        data.add(e);
        siftUp(data.size()-1);
    }
    //将角标K所对应的元素上浮
    private void siftUp(int k) {
        while (k>0&&data.get(k) .compareTo(data.get(parent(k)))>0){
              data.swap(k,parent(k));
              k=parent(k);
        }
    }

    //查找最大值
    public E findMax(){
        if(data.isEmpty()){
            throw new IllegalArgumentException("最大堆为空");
        }
        return data.get(0);
    }
    //查找最小值
    public E findMin(){
        if(data.isEmpty()){
            throw new IllegalArgumentException("最大堆为空");
        }
        E min=data.get(0);
        for (int i=1;i<data.size();i++){
            if (data.get(i).compareTo(min)<0){
                min=data.get(i);
            }
        }
        return min;
    }

    //删除根结点
    public E extractMax(){
        E max=findMax();
        data.swap(0,data.size()-1);
        data.remove(data.size()-1);
        siftDown(0);
        return max;
    }

    //下沉
    private void siftDown(int k) {
        while (leftChild(k)<data.size()){
            int j=leftChild(k);
            if(j+1<data.size()&&data.get(j+1).compareTo(data.get(j))>0){
                j=rightChild(k);
            }
            if(data.get(k).compareTo(data.get(j))<0){
                data.swap(k,j);
                k=j;
            }else {
                break;
            }
        }
    }

    //修改
    public E replace(E e){
        E ret=findMax();
        data.set(0,e);
        siftDown(0);
        return ret;
    }
    @Override
    public Iterator<E> iterator() {
        return data.iterator();
    }

    @Override
    public String toString() {
        return data.toString();
    }
}

测试

package ceshi;

import 树形结构.MaxHeap;

import java.util.Random;

public class TestMaxHeap {
    public static void main(String[] args) {
        MaxHeap<Integer> heap=new MaxHeap<>();
        Random random=new Random();
        for (int i=0;i<10;i++){
            heap.add(random.nextInt(20));
        }
        System.out.println(heap);
        for (int i=0;i<4;i++){
            System.out.println(heap.extractMax());
        }
    }
}

执行结果

ArrayList:10/10[15,14,14,6,13,2,0,1,4,9]
15
14
14
13

优先队列

package 树形结构;

import shujujiegou_interface.Queue;

import java.util.Iterator;

//优先队列
public class PriorityQueue<E extends Comparable<E>> implements Queue<E> {
    private MaxHeap<E> heap;
    public PriorityQueue(){
        heap=new MaxHeap<>();
    }
    @Override
    public int size() {
        return heap.size();
    }

    @Override
    public boolean isEmpty() {
        return heap.isEmpty();
    }

    @Override
    public void offer(E element) {
          heap.add(element);
    }

    @Override
    public E poll() {
        return heap.extractMax();
    }

    @Override
    public E element() {
        return heap.findMax();
    }

    @Override
    public void clear() {
        heap.clear();
    }

    @Override
    public Iterator<E> iterator() {
        return heap.iterator();
    }

    @Override
    public String toString() {
        return heap.toString();
    }
}

相关标签: 数据结构与算法