Java implementation of minimum heap and maximum heap

Source: Internet
Author: User

/** * File name: Binaryheap.java * Time: November 3, 2014 7:15:34 * Xiu Kang */package chapter6;import java.util.*;/** * Class Name: Binaryheap Description: Establish a Minimum heap */class minheap<anytype extends comparable<? Super anytype>> {private int currentsize;private static final int default_capacity = 10;private anytype[] Array;publ IC Minheap (anytype[] items) {currentsize = Items.length;array = (anytype[]) New Comparable[currentsize + 1];int i = 1;for (AnyType item:items) array[i++] = Item;buildheap ();} /** * Method Name: Percolatedown * Description: */private void percolatedown (int position) {AnyType temp = Array[position];int child;for (; Position * 2 <= currentsize; Position = child) {child = 2 * POSITION;IF (Child! = currentsize&& Array[child + 1].compareto (Array[child]) < 0 ) child++;if (Array[child].compareto (temp) < 0) Array[position] = Array[child];elsebreak;} Array[position] = temp;} /** * Method Name: Buildheap * Description: The next filter order is critical from the middle start continuously down filter */private void Buildheap () {for (int i = CURRENTSIZE/2; i > 0; i--) PE Rcolatedown (i);} public void Insert (AnyType x) {if (currentsize >= array.length-1) Enlargearray (Array.Length * 2 + 1); int hole = ++curr Entsize;while (Hole > 1 && x.compareto (ARRAY[HOLE/2]) < 0) {Array[hole] = Array[hole/2];hole/= 2;} Array[hole] = x;} private void Enlargearray (int capacity) {anytype[] Oldarr = array; Anytype[] NewArr = (anytype[]) new comparable[capacity];for (int i = 1; i < array.length; i++) newarr[i] = Oldarr[i];arr ay = newArr;} public Boolean isEmpty () {return currentsize = = 0;} Public AnyType Delemin () {if (!isempty ()) {AnyType min = array[1];array[1] = array[currentsize--];p ercolatedown (1); return min;} return null;}  Public String toString () {stringbuffer sb = new StringBuffer (), for (int i = 1; I <= currentsize; i++) sb.append (Array[i] + ""); return new String (SB);}} /** * Class Name: Maxheap Description: Maximum heap operation and build heap */class Maxheap<anytype extends comparable<? Super anytype>> {private anytype[] array;private int currentsize;public maxheap (anytype[] arr) {currentsize = Arr.length;array = (anytype[]) New Comparable[currentsize + 1];int i = 1;for (int j = 0; J < Arr.length; J + +) array[i++ ] = Arr[j];buildheap ();} /** * Method Name: Buildheap * Description: From the middle start to the end constantly on the filter */private void Buildheap () {for (int i = CURRENTSIZE/2; I <= currentsize; i++) p Ercolateup (i);} private void Percolateup (int position) {AnyType temp = array[position];while (Position > 1) {if (array[position/2].co Mpareto (temp) < 0) {Array[position] = Array[position/2];p osition/= 2;} elsebreak;} Array[position] = temp;} Private Boolean IsEmpty () {return currentsize = = 0;} Public AnyType Delemax () {AnyType max = array[1]; AnyType temp = array[currentsize--];int position = 1;int child;while (position * <= currentsize) {child = 2 * position;i F (Child!=currentsize&&array[child].compareto (Array[child + 1]) < 0) child++;if (Array[child].compareto ( Temp) > 0) array[position] = array[child];else break;position = child;} Array[position] = Temp;return max;} public void Insert (AnyType x) {if (CurrentsiZe = = array.length-1) Enlargearray (currentsize + 1); Array[++currentsize] = X;percolateup (currentsize);} private void Enlargearray (int capacity) {anytype[] Oldarr = array; Anytype[] NewArr = (anytype[]) new comparable[capacity];for (int i = 1; i < array.length; i++) newarr[i] = Oldarr[i];arr ay = newArr;}  Public String toString () {stringbuffer sb = new StringBuffer (), for (int i = 1; I <= currentsize; i++) sb.append (Array[i] + ""); return new String (SB);}} public class Heaptest {public static void main (string[] args) {//TODO auto-generated method stubinteger[] arr = new Integ Er[] {9,8,7,6,5, 4, 3, 2}; Minheap min = new Minheap (arr); Min.insert (+); Min.delemin (); System.out.println (min); integer[] arr2 = new integer[] {1, 2, 3, 4, 5,6,7,8,9,10}; Maxheap max = new Maxheap (ARR2); Max.delemax (); Max.insert (10); SYSTEM.OUT.PRINTLN (max);}}

Java implementation of minimum heap and maximum heap

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.