堆和二叉堆

1.什么是堆?

堆是一颗具有特定性质的完全二叉树。
特定性质是:所有节点的值必须大于或等于(或小于或等于)其孩子节点的值。

2.堆的类型。

最小堆:节点的值必须小于或等于孩子节点的值。
最大堆:节点的值必须大于或等于孩子节点的值。

3.堆的java实现

以最大堆为例:

我们使用数组来实现。
那么堆的元素是如何存储的呢?
例子:
这里有一个最大堆:
在这里插入图片描述
我们可以得到它的数组存储表为:
在这里插入图片描述
通过上面的表我们可以得到:
假设一个节点的位置为i,则有:
1.节点的父母节点位置为:(i-1)/2
2.节点的孩子节点位置为:左(2i+1)右(2i+2)

1.堆的声明:
下面代码的方法有:
1.创建堆
2.获得节点的父母
3.获得节点的孩子
4.获得堆的最大元素

class Heap{
    public int[] array;//存储元素的数组
    public int count;//描述堆中存在元素的个数
    public int capacity;//堆的大小
    public int heap_type;//最大堆或最小堆
    //创建堆构造方法
    public Heap(int capacity,int heap_type)
    {
        this.capacity=capacity;
        this.heap_type=heap_type;
        this.count=0;
        this.array=new int[capacity];
    }
    //得到父母节点的位置
    public int Parent(int i)
    {
        if(i<=0||i>this.count)
            return -1;
        return (i-1)/2;
    }
    //得到左孩子节点的位置
    public int LeftChild(int i)
    {
        int left=2*i+1;
        if(left>=this.count)
            return -1;
        return left;
    }
    //得到右孩子节点的位置
    public int RightChild(int i)
    {
        int right=2*i+2;
        if(right>=this.count)
            return -1;
        return right;
    }
    //获取最大元素
    public int GetMaximum()
    {
        if(this.count==0)
            return -1;
        return this.array[0];
    }
    
}

2.部分操作方法的定义(堆化元素,插入,删除,清空堆,数组建堆):

堆化元素:当插入一个元素到堆中,他可能不满足堆的性质,这样的情况下我们需要调整这个元素的位置使每个节点满足堆的性质为止,这个调整的过程称为堆化元素。
具体的策略为:找到此元素的孩子节点中的最大值,然后将他与当前元素交换,重复该过程直至每个节点都满足堆的性质为止。

代码如下:

//堆化元素

public void PercolateDown(int i)
{
    int l,r,max,temp;
    l=LeftChild(i);
    r=RightChild(i);
    if(l==-1&&r==-1)//如果没有孩子节点则返回
        return;
    if(l!=-1&&this.array[l]>this.array[i])
        max=l;
    else
        max=i;
    if(r!=-1&&this.array[r]>this.array[max])
        max=r;

    if(max==i)//如果本节点就是最大的,返回
         return;
    else
    {
        temp=this.array[i];
        this.array[i]=this.array[max];
        this.array[max]=temp;
    }
    PercolateDown(max);
}
    

插入:

//插入元素
   void Insert(int data)
    {
        int i;
        if(this.count==this.capacity)
            ResizeHeap();
        this.count++;
        i=this.count-1;
        while(i>=0&&data>this.array[(i-1)/2])//不断找到比自己小的父母节点交换之
        {
            this.array[i]=this.array[(i-1)/2];
            i=(i-1)/2;
        }
        this.array[i]=data;

    }
    //扩展堆
    void ResizeHeap()
    {
        int []array_old=new int[this.capacity];
       System.arraycopy(this.array,0,array_old,0,this.count-1);//复制方法
        this.array=new int[this.capacity*2];
        if(this.array==null)
        {
            System.out.println("Memory Error");
            return;
        }
        for(int i=0;i<this.capacity;i++)
        {
            this.array[i]=array_old[i];

        }
        this.capacity *=2;
        array_old=null;

    }

删除最大元素:

 //删除最大元素
    int DeleteMax()
    {
        if(this.count==0)
            return -1;
        int data=this.array[0];
        this.array[0]=this.array[this.count-1];
        this.count--;
        PercolateDown(0);
        return data;
    }

全代码如下(包括测试代码):

public class dui {
    public static void main(String[] args) {

        int a[]={13,17,1,6,4,2,5};
        Heap test=new Heap(a.length,0);
        test.BuildHeap(test,a,a.length);
        for(int i=0;i<a.length;i++)
        {
            System.out.println(test.array[i]);
        }

    }

}

class Heap{
    public int[] array;//存储元素的数组
    public int count;//描述堆中存在元素的个数
    public int capacity;//堆的大小
    public int heap_type;//最大堆或最小堆
    //创建堆构造方法
    public Heap(int capacity,int heap_type)
    {
        this.capacity=capacity;
        this.heap_type=heap_type;
        this.count=0;
        this.array=new int[capacity];
    }
    //得到父母节点的位置
    public int Parent(int i)
    {
        if(i<=0||i>this.count)
            return -1;
        return (i-1)/2;
    }
    //得到左孩子节点的位置
    public int LeftChild(int i)
    {
        int left=2*i+1;
        if(left>=this.count)
            return -1;
        return left;
    }
    //得到右孩子节点的位置
    public int RightChild(int i)
    {
        int right=2*i+2;
        if(right>=this.count)
            return -1;
        return right;
    }
    //获取最大元素
    public int GetMaximum()
    {
        if(this.count==0)
            return -1;
        return this.array[0];
    }

    //堆化元素
    public void PercolateDown(int i)
    {
        int l,r,max,temp;
        l=LeftChild(i);
        r=RightChild(i);
        if(l==-1&&r==-1)
            return;
        if(l!=-1&&this.array[l]>this.array[i])
            max=l;
        else
            max=i;
        if(r!=-1&&this.array[r]>this.array[max])
            max=r;

        if(max==i)
             return;
        else
        {
            temp=this.array[i];
            this.array[i]=this.array[max];
            this.array[max]=temp;
        }
        PercolateDown(max);
    }

    //删除最大元素
    int DeleteMax()
    {
        if(this.count==0)
            return -1;
        int data=this.array[0];
        this.array[0]=this.array[this.count-1];
        this.count--;
        PercolateDown(0);
        return data;
    }
    //插入元素
   void Insert(int data)
    {
        int i;
        if(this.count==this.capacity)
            ResizeHeap();
        this.count++;
        i=this.count-1;
        while(i>=0&&data>this.array[(i-1)/2])//不断找到比自己小的父母节点交换之
        {
            this.array[i]=this.array[(i-1)/2];
            i=(i-1)/2;
        }
        this.array[i]=data;

    }
    //扩展堆
    void ResizeHeap()
    {
        int []array_old=new int[this.capacity];
       System.arraycopy(this.array,0,array_old,0,this.count-1);//复制方法
        this.array=new int[this.capacity*2];
        if(this.array==null)
        {
            System.out.println("Memory Error");
            return;
        }
        for(int i=0;i<this.capacity;i++)
        {
            this.array[i]=array_old[i];

        }
        this.capacity *=2;
        array_old=null;

    }
    //清空堆
    void DestroyHeap()
    {
        this.count=0;
        this.array=null;
    }

    //数组建堆
    void BuildHeap(Heap h,int A[],int n)
    {
        if(h==null) return;
        while(n>this.capacity)
            h.ResizeHeap();

        for(int i=0;i<n;i++)//数组赋值
        {
            h.array[i]=A[i];

        }
        this.count=n;

        for(int i=(n-1)/2;i>=0;i--)//节点堆化
            h.PercolateDown(i);
   }
 }

结果:在这里插入图片描述

发布了73 篇原创文章 · 获赞 1 · 访问量 2444

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/105125137