Java implementation of maximum heap and heap ordering __java

Source: Internet
Author: User
Tags array length assert rand
Max HeapThe maximum heap data structure is a complete binary tree (leaf nodes can only be present in the lowest and lower layers, and the bottom layer of the node is concentrated in the most left of the layer of the two-fork tree) at several locations.
Because of the nature of the complete binary tree, we use an array to store the nodes of the tree, from top to bottom, from left to right, in order there is an array, and the child nodes are worth less than the value of the parent node. So the root node of the heap is the maximum value in the array, which is the maximum heap. The maximum heap is often used to implement priority queues. Take a chestnut: like the Hero League or the hero of the King's glory within the bounds of his attack, he would give priority to attacking more important enemy units, the enemy units within his range of attack in a maximum heap, take out the largest heap of the root node that should be the most priority attack of the one, attack it. When another enemy unit enters the attack range, it is inserted into the maximum heap and maintains the maximum heap nature. Implementation of the maximum heap below and implementation of heap sorting:
Implementation of the largest heap (with arrays to store the nodes in the complete binary tree, from top to bottom, from left to right, ordered by the array, the value of the child node is less than the value of the parent node) public class Heap {private int[] data; private int count; Number of current nodes private int capacity;  Capacity public Heap (int capacity) {this.data=new int[capacity+1];
		Because the index 0 does not save the node, so the length plus a this.capacity=capacity;
	this.count=0;
		///A unordered array is created as a maximum heap equivalent to heap sort public Heap (int[] arr,int N) {data=new int[n+1];
		Capacity=n;
		for (int i=0;i<n;i++) {data[i+1]=arr[i];
		} count=n;
		for (int i=count/2;i>=1;i--) {//i=count/2:i is the parent node of the last leaf node (last non-leaf node) shiftdown (i); } private void Shiftup (int i) {while (i>1) && (Data[i/2]<data[i]) {//DATA[I/2] is the parent node of the current node swap (data
			, I,I/2);   I=I/2;      Update Index}} private void Shiftdown (int k) {while ((2*k) <=count) {//has Zoozi node int j=2*k;
			 This round of loops, data[k] and data[j] swap positions if ((j+1) <=count&& (data[j+1]>data[j)) {//Have right child nodes and larger j+=1 on the right;
			 } if (Data[k]>=data[j])//If the parent node is greater than or equal to the child node, stop the loop break;
			 Swap (DATA,K,J);       K=j;K is assigned the current position, initialize the next iteration}} public int size () {return count;
	public Boolean IsEmpty () {return count==0;   public void Insert (int a) {assert ((count+1) <=capacity);     Prevent array data[count+1]=a;   
		Save count++ starting from index 1;  Shiftup (count);
		Because the number of possible new additions violates the definition of the maximum heap, you will want to reorder} public int Extractmax () {//eject the maximum, that is, the root node assert (count>0);
		int ret=data[1];    Swap (Data,1,count);
		The final number is placed in the first position to maintain the structure count--of the complete binary tree;         Shiftdown (1);
	Move the first number to the appropriate position, keeping the maximum heap property return ret;
		public static void Swap (int[] Arr,int a,int b) {int c=arr[a];
		ARR[A]=ARR[B];
	Arr[b]=c; public static void Situheapsort (int[] arr,int N) {//In-place heap sort (is heap sort), starting with 0 for (int i= (n-1)/2;i>=0;i--) {//i=      (n-1)/2:i is the parent node of the last leaf node (the last non-leaf node) __shiftdown (arr,n,i);          Constructs a completely unordered array of arr into the maximum heap} for (int i=n-1;i>0;i--) {swap (arr,0,i);   The first is the maximum value and the last value Exchange __shiftdown (arr,i,0); Place the first smaller value in the appropriate position, at which time the array length is n-1}} public static void __shiftdown (int[] arr,int n,int k){while ((2*k+1) <n) {//has Zoozi node int j=2*k+1;
			 This round of loops, arr[k] and arr[j] swap positions if ((j+1) <n&& (arr[j+1]>arr[j)) {//Have right child nodes and larger j+=1 on the right;
			 } if (Arr[k]>=arr[j])//If the parent node is greater than or equal to the child node, stop the loop break;
			 Swap (ARR,K,J);       K=j;
		K is assigned the current position, initialized for Next loop}} public static void Main (string[] args) {Heap heap=new Heap (100);
		System.out.print ("inserted number is:");
			for (int i=0;i<30;i++) {int rand=new Random (). Nextint (100) +1;
			System.out.print (rand+ "");
		Heap.insert (RAND);
		} System.out.println ();
		System.out.print ("Heap.extractmax ():");   while (!heap.isempty ()) {System.out.print (Heap.extractmax () + "");
		From large to small output} System.out.println ();
		System.out.println ("********************* heap sort *********************************");
		int arr[] =main.geneatearrays (30);
		Situheapsort (arr,arr.length);
		for (int i=0;i<arr.length;i++) {System.out.print (arr[i]+ ""); }
	}

}
Print results:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.