坠小二叉堆的一些方法

•堆其实是一颗完全二叉树
•任何一非叶子节点的关键字不大于或者不小于其左右子节点的关键字
•如左图所示,为最小堆
•堆的存储结构为顺序结构,以左图为例,其在顺序表中的存储序列为{15,18,30,45,35,36,48,93,53,72}

Father= (Child-1)/2

LeftChild=Father*2+1

RightChild=Father*2+2



package BinaryHeap;

public class BinaryHeap {
	
		public int value;
		public int capacity=15;
        public int length=0;
		public int[] heap=new int[capacity];
		public int currentcapacity;
		public void insert(int x)
		{
			heap[length]=x;
	            currentcapacity=length+1;
			    int currentindex=length;
			    int father=(currentindex-1)/2;
			    while(father>=0 && heap[father]>heap[currentindex])
			    {
			       int exchange=heap[father];
			       heap[father]=heap[currentindex];
			       heap[currentindex]=exchange;
			       exchange=father;
			       father=(father-1)/2;
			       currentindex=exchange;
			    }
			    length++;
		}
		public void BinaryHeap(int x)
		{
		   insert(x);	
		}
		public void Delete(int x)
		{
		   int i=0;
		   //找到这个数在堆中的位置
		   while(heap[i]!=x)
			   i++;
		   heap[i]=heap[length-1];
	         heap[length-1]=0;
		    while(heap[i]>Math.max(heap[i*2+1], heap[i*2+2])&& i*2+2<length-1)
		    {
		    	int change=heap[i];
		    	if(heap[i*2+1]>heap[i*2+2]) 
		    	{
		    		heap[i]=heap[i*2+2];
		    		heap[i*2+2]=change;
		    		i=i*2+2;	
		    	}
		    		
		    	else {
		    		heap[i]=heap[i*2+1];
		    		heap[i*2+1]=change;
		    		i=i*2+1;
		    	}
		    	
		    }
		    length--;
		}
		public void IncreaseKey(int delta,int x)
		{
			 int i=0;
			   //找到这个数在堆中的位置
			   while(heap[i]!=x)
				   i++;
			    heap[i]=heap[i]+delta;
			    while(heap[i]>Math.max(heap[i*2+1], heap[i*2+2])&& i*2+2<length-1)
			    {
			    	int change=heap[i];
			    	if(heap[i*2+1]>heap[i*2+2]) 
			    	{
			    		heap[i]=heap[i*2+2];
			    		heap[i*2+2]=change;
			    		i=i*2+2;	
			    	}
			    		
			    	else {
			    		heap[i]=heap[i*2+1];
			    		heap[i*2+1]=change;
			    		i=i*2+1;
			    	}
			    	
			    }
			   
		}
		public void DecreaseKey(int delta,int x)
		{
			int i=0;
			   //找到这个数在堆中的位置
			   while(heap[i]!=x)
				   i++;
			    heap[i]=heap[i]-delta;
			    while(heap[i]<heap[(i-1)/2]&&(i-1)/2>=0)
			    {
			    	int change=heap[i];
			    		heap[i]=heap[(i-1)/2];
			    		heap[(i-1)/2]=change;
			    		i=(i-1)/2;
			    }
		}
       public void PrintHeap()
       {
    	   System.out.println("打印出来当前堆");
    	   for(int i=0;i<length;i++)
    	   {
    		   System.out.print(heap[i]+" ");
    	   }
    	   System.out.println();
       }
}

package BinaryHeap;

public class MainMethod {
public static void main(String[] args)
{
	int[] input={32,12,14,55,23,16,30,49};
	BinaryHeap h=new BinaryHeap();
	for(int i=0;i<input.length;i++)
	{
		h.insert(input[i]);
	}
	h.IncreaseKey(17, 16);
	h.PrintHeap();
	h.DecreaseKey(6, 14);
	h.PrintHeap();
	h.Delete(12);
	h.PrintHeap();
}
}

运行结果:

打印出来当前堆
12 23 14 49 32 33 30 55 
打印出来当前堆
8 23 12 49 32 33 30 55 
打印出来当前堆
8 23 30 49 32 33 55 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值