《数据结构与算法》学习笔记26 堆

本文介绍了一种使用堆数据结构实现优先级队列的方法,包括堆的基本概念、堆的操作如插入、删除最大元素等,并提供了完整的Java实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用堆实现优先级队列,O(logn)

1、完全二叉树。除了最后一层节点不需要是满的,其他的每一层从左到右都完全是满的

2、常由数组实现。

3、堆里的每一个节点都满足:父节点关键字大于所有的子节点。

public class Node {
     private int data;
     public Node(int key){
    	 data=key;
     }
     
     public int getKey(){
    	 return data;
     }
     
     public void setKey(int id){
    	 data=id;
     }
}

public class Heap {
    private Node[] heapArray;
    private int size;
    private int currentsize;
    
    public Heap(int n){
    	size=n;
    	currentsize=0;
    	heapArray=new Node[size];
    }
    
    public boolean isEmpty(){
    	return size==0;
    }
    
    public boolean insert(int key){
    	if(currentsize==size) return false;
    	Node newNode=new Node(key);
    	heapArray[currentsize]=newNode;
    	trickUp(currentsize);//新节点放在最后,比较之后向上调整
       	currentsize++;
       	return true;
    }
    
    public void trickUp(int index){
    	int parent=(index-1)/2;
    	Node bottom=heapArray[index];
    	while(index>0 && heapArray[parent].getKey()<bottom.getKey()){
    		heapArray[index]=heapArray[parent];
    		index=parent;
    		parent=(parent-1)/2;
    	}
    	heapArray[index]=bottom;
    }
    
    public Node delete(){ // 删除最大项
       Node root=heapArray[0];
       heapArray[0]=heapArray[--currentsize];//最后的数据项移动到根上
       trickDown(0);
       return root;
    }
    
    public void trickDown(int index){
    	int lagerChild;
    	Node top=heapArray[index];
    	while(index<currentsize/2){  //如果未到最后一层
    		int leftChild=2*index+1;
    		int rightChild=leftChild+1;
    		if(rightChild<currentsize && heapArray[leftChild].getKey()<heapArray[rightChild].getKey())
    			lagerChild=rightChild;
    		else
    			lagerChild=leftChild;
    		if(top.getKey()>=heapArray[lagerChild].getKey()) break;
    		heapArray[index]=heapArray[lagerChild];
    		index=lagerChild;
    	}
    	heapArray[index]=top;
    }
    	
    public boolean change(int index,int newValue){
    	if(index<0 || index>= currentsize){
    		return false;
    	}
    	int oldValue=heapArray[index].getKey();
    	heapArray[index].setKey(newValue);
    	if(oldValue<newValue) trickUp(index);
    	else trickDown(index);
    	return true;
    }
    
    public void displayHeap(){
    	System.out.print("heap:");
    	for(int i=0;i<currentsize;i++){
    		if(heapArray[i]!=null) System.out.print(heapArray[i].getKey()+" ");
    		else System.out.print("-- ");
    	}
    	
    	//树状输出
    	int nBlank=32;
    	int itemsPerRow=1;
    	int column=0;
    	int j=0;
    	String dots="........";
    	System.out.println(dots+dots);
    	while(currentsize>0){
    		if(column==0){
    			for(int k=0;k<nBlank;k++)
    				System.out.print(" ");
    		}
    	System.out.print(heapArray[j].getKey());
    	if(++j==currentsize) break; //打印完了
    	if(++column==itemsPerRow) { //一行打印完了
    	  nBlank/=2;
    	  itemsPerRow*=2;
    	  column=0;
    	  System.out.println();  //打印完换行
    	}else
    		for(int k=0;k<nBlank*2-2;k++) System.out.print(" ");	
    	}
    	System.out.println(dots+dots);
    }
    
    public void deleteKey(int n){
    	int i;
    	for(i=0;i<currentsize;i++){
    		if(heapArray[i].getKey()==n){
    			heapArray[i]=heapArray[--currentsize];
    	    	trickDown(i);
    			break;
    		}
    	}
    	//System.out.print(i);
    }
}
    

import java.io.*;

public class heapApp {
	public static String getString() throws IOException{
		  InputStreamReader isr=new InputStreamReader(System.in);
		  BufferedReader br=new BufferedReader(isr);
		  return br.readLine();	  
	  }
	  
	  public static char getchar() throws IOException{
		  return getString().charAt(0);
	  }
	  
	  public static int getInt() throws IOException{
		  return Integer.parseInt(getString());
	  }
	  
	  public static void main(String[] args){
		  Heap theheap=new Heap(31);
		  theheap.insert(70);
		  theheap.insert(40);
		  theheap.insert(50);
		  theheap.insert(20);
		  theheap.insert(60);
		  theheap.insert(100);
		  theheap.insert(80);
		  theheap.insert(30);
		  theheap.insert(10);
		  theheap.insert(90);
		  theheap.displayHeap();
          //theheap.delete();
		  theheap.deleteKey(90);
          theheap.displayHeap();
          
	  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值