数据结构:堆

package none012堆;
//堆:1,它是完全二叉树2,它常常用一个数组实现3,满足堆的条件:每一个节点的关键字都大于或等于这个节点
//的子节点的关键字
//堆是完全二叉树的事实说明了表示堆的数组中没有“洞”。
//移除是指删除关键字最大的节点,这个节点总是根节点
//堆通常用数组实现,表现为一颗完全二叉树,根节点的下标为0,最后一个节点的下表为N-1;堆提供移除最大的数据项和插入的方法,时间复杂度都是O(logN);
//最大数据项总是在根位置。
import java.io.*;
import java.util.*;

public class HeapApp {
	public static void main(String[] args) throws IOException{
		int value,value2;
		Heap theHeap=new Heap(31);
		boolean success;
		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);
		while(true){
			System.out.print("Enter first letter of ");
			System.out.print("show,insert,remove,change: ");
			int choice=getChar();
			switch(choice)
			{
			case 's':theHeap.displayHeap();
			         break;
			case 'i':
				System.out.print("Enter value to insert: ");
				value=getInt();
				success=theHeap.insert(value);
				if(!success)
					System.out.println("can not insert,heap full");
				break;
			case 'r':
				if(!theHeap.isEmpty())
					theHeap.remove();
				else 
					System.out.println("can not remove;heap empty");
				break;
			case 'c':
				System.out.print("Enter current index of item: ");
				value=getInt();
				System.out.print("Enter new Key: ");
				value2=getInt();
				success=theHeap.change(value, value2);
				if(!success)
					System.out.println("Invalid index");
				break;
				default:
					System.out.println("invalid entry\n");
			}
					
		}
	}
	
	public static String getString() throws IOException
	{
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		String s=br.readLine();
		return s;
		
	}
	public static char getChar() throws IOException{
		String s=getString();
		return s.charAt(0);
	}
	public static int getInt() throws IOException
	{
		String s=getString();
		return Integer.parseInt(s);
	}
}
class Node
{
	private int iData;
	public Node(int key)
	{
		iData=key;
	}
	public int getKey(){
		return iData;
	}
	public void setKey(int id){
		iData=id;
	}
}
class Heap
{
	private Node[] heapArray;
	private int maxSize;
	private int currentSize;
	public Heap(int mx)
	{
		maxSize=mx;
		currentSize=0;
		heapArray=new Node[maxSize];
	}
	public boolean isEmpty()
	{
		return currentSize==0;
	}
	public boolean insert(int key)
	{
		if(currentSize==maxSize)
			return false;
		Node newNode=new Node(key);
		heapArray[currentSize]=newNode;
		trickleUp(currentSize++);
		return true;
	}
	public void trickleUp(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 remove()
	{
		Node root=heapArray[0];
		heapArray[0]=heapArray[--currentSize];
		trickleDown(0);
		return root;
	}
	public void trickleDown(int index){
		int largerChild;
		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())
				largerChild=rightChild;
			else 
				largerChild=leftChild;
			if(top.getKey()>=heapArray[largerChild].getKey())
				break;
			heapArray[index]=heapArray[largerChild];
			index=largerChild;
		}
		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)
			trickleUp(index);
		else 
			trickleDown(index);
		return true;
		
	}
	public void displayHeap(){
		System.out.print("heapArray: ");
		for(int m=0;m<currentSize;m++)
			if(heapArray[m]!=null)
				System.out.print(heapArray[m].getKey()+" ");
			else
				System.out.print(".. ");
		System.out.println();
		int nBlanks=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<nBlanks;k++)
					System.out.print(' ');
			System.out.print(heapArray[j].getKey());
			if(++j==currentSize)
				break;
			if(++column==itemsPerRow)
			{
				nBlanks/=2;
				itemsPerRow*=2;
				column=0;
				System.out.println();
			}
			else
				for(int k=0;k<nBlanks*2-2;k++)
					System.out.print(' ');
			
		}
		System.out.println("\n"+dots+dots);
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值