Maximum heap algorithm Java for minimum heap

Source: Internet
Author: User


The smallest heap is also called the small top heap, the small top heap is a complete binary tree, satisfies the small top heap condition is each child node's value is bigger than the parent node. The Big top pile is the opposite.


/** * Minimum heap * @author DWL * * */public class Minheap {//using the data in the array storage heap private int[];
		Public Minheap (int[] data) {this.data = data;
	Bulidheap (); /** * Create a minimum heap/private void Bulidheap () {for (int i = (data.length)/2-1;i>=0;i--) {//subscript less than or equal to I node has child nodes Chang
		E (i);
		}/** * According to the parent node to determine whether * with the left and right child exchange * @param i * * * private void change (int i) {int temp = 0;
		int left = left (i);
		
		
		int right = right (i);
			If there is a right node, there is a left node if (right<data.length) {//Get the child small and middle Subscript temp = min (right);
				if (Data[i]>data[temp]) {swap (I, temp);
			If there is a swap with the child node, adjust the change (temp) for the child node's left and right children;
			}else if (right<data.length) {//There is no right node but there is a left node, then the left node has no children node if (Data[i]>data[left]) swap (I, ieft); Child node is greater than parent node, direct swap position}/** * Gets the subscript * @param i * @param j * @return/private int min for the smaller nodes in two nodes (int i,int j)
		{if (Data[i]>=data[j]) return J;
	return i; /** * To obtain the * left child node under the parent node subscript * @param i
	 * @return * * private int left (int i) {return ((i+1) <<1)-1; /** * According to the parent node under the table to get * Right child node subscript * @param i * @return * * private int right (int i) {return (i+1) <<1;//left 1 
		Bit equivalent to multiply by 2}/** * According to the position of the subscript exchange array * @param i * @param j * * private void Swap (int i,int j) {int temp = Data[i];
		Data[i] = Data[j];	
		
	DATA[J] = temp;
		/** * Reset Heap Top * @param root/public void newroot (int root) {data[0] = root;
		
	Change (0);
	/** * Get Heap Top * @return * * * public int getroot () {return data[0];
	/** * Get Heap data * @return/public int[] GetData () {return data; }

}

The small top heap heap has high efficiency to solve the top-k problem, and the maximum or minimum number of K (k<n) can be found in N number to use the small top heap algorithm and the Big Top heap algorithm respectively. Below I use the top heap algorithm above to find out the largest 7 digits in an array.

public class TOPK {public

	static void Main (string[] args) {
		//TODO auto-generated Method stub
		int[] num = {1 , 4,6,7,342,354,11,345,64,25,45,32,343,754};
		int[] data = {1,4,6,7,342,354,11};//takes the first 7 data to establish the minimum heap
		minheap heap = new Minheap (data);
		
		The for (int i=7;i<num.length;i++) {
			//loop is compared to the heap top to reset the heap top
			if (Num[i]>heap.getroot ()) {
				Heap.newroot ( Num[i]);
			}
		The first 7 largest number of loops output for
		(int n:heap.getdata ()) {
			System.out.println (n);
		}
		
	}

}

The test results are shown below:


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.