A heap is a locally ordered nonlinear aggregation. Generally, a full binary tree is used to represent a heap, which is very helpful for storage and operation. Generally, the heap is divided into the maximum heap and the minimum heap according to the order of Local Order (the following uses the nature of the full Binary Tree ).
- Max heap: the value of any node is greater than or equal to the value of all its subnodes. If the full binary tree is used, it is expressed as: Ki> =
K2i + 1
And Ki
> = K2i + 2
Minimum heap: the value of any node is smaller than or equal to the value of all its subnodes. If the full binary tree is used, it is expressed as: Ki
<= K2i + 1
And Ki <= k2i + 2
The following is a heap abstraction represented by a full binary tree and uses a one-dimensional array to store data. The establishment and deletion methods of the largest and smallest heap and the expression of the priority queue based on the smallest heap are described.
- Heap abstract class:/** <Br/> * Desc: <br/> * heap interface <br> <br/> * ------------------------------------------------------------------------ <br/> * ver. date who what <br/> * region <br/> * 0.0.1 2011-1-4 leisore add <br/> * region <br/> */<br/> package CN. leisore. DSA. ds_tsinghua.c H06.sec04; <br/> Import Java. lang. reflect. array; <br/>/** <br/> * heap <br/> * @ author leisore <br/> * @ since version 0.0.1 <br/> * /<br/> public abstract class heap <t extends comparable <? Super t >{< br/> // default capacity <br/> Private Static final int default_capacity = 16; <br/> // array <br/> protected T [] elements = NULL; <br/> // capacity <br/> protected int capacity = 0; <br/> // size <br/> protected int size = 0; <br/> Public heap () {<br/> This (default_capacity ); <br/>}< br/> @ suppresswarnings ("unchecked") <br/> Public heap (INT capacity) {<br/> This. capacity = capacity; <br/> This. element S = (T []) array <br/>. newinstance (comparable. class, this. capacity); <br/>}< br/> Public heap (T [] elements) {<br/> This (elements. length); <br/> system. arraycopy (elements, 0, this. elements, 0, elements. length); <br/> size = elements. length; <br/> int startpos = (size-2)/2; <br/> while (startpos> = 0) {<br/> filterdown (startpos ); <br/> startpos --; <br/>}< br/>/** <br/> * Insert <br/> * @ para M T <br/> * @ return <br/> */<br/> Public Boolean insert (t) {<br/> ensurecapacity (); <br/> elements [size ++] = T; <br/> filterup (size-1); <br/> return true; <br/>}< br/>/** <br/> * Delete <br/> * @ return <br/> */<br/> public t remove () {<br/> If (size = 0) {<br/> throw new runtimeexception ("heap is empty "); <br/>}< br/> T remove = elements [0]; <br/> elements [0] = elements [size-1]; <br/> elemen TS [size-1] = NULL; <br/> size --; <br/> filterdown (0); <br/> return remove; <br/>}< br/>/** <br/> * obtain the heap top element <br/> */<br/> Public t PEEK () {<br/> If (size = 0) {<br/> throw new runtimeexception ("heap is empty "); <br/>}< br/> return elements [0]; <br/>}< br/>/** <br/> * heap size <br/> * @ return <br/> */<br/> Public int size () {<br/> return size; <br/>}< br/>/** <br/> * check whether the storage space is sufficient. If the storage space has reached the capacity, then it is automatically extended to the original 2 Times <br/> */<br/> @ suppresswarnings ("unchecked") <br/> private void ensurecapacity () {<br/> If (size = capacity) {<br/> capacity * = 2; <br/> T [] newelements = (T []) array. newinstance (comparable. class, <br/> This. capacity); <br/> system. arraycopy (elements, 0, newelements, 0, size); <br/> This. elements = newelements; <br/>}< br/>/** <br/> * change the binary tree with startpos as root to a minimum/large heap. <br/> * <br/> * @ Param sta Rtpos <br/> */<br/> protected abstract void filterdown (INT startpos ); <br/>/** <br/> * adjust the binary tree with startpos as root to a minimum/large heap. <br/> * @ Param startpos <br/> */<br/> protected abstract void filterup (INT startpos ); <br/>/** <br/> * @ Param I <br/> * @ Param j <br/> */<br/> protected void swap (int I, int J) {<br/> T temp = elements [I]; <br/> elements [I] = elements [J]; <br/> elements [J] = temp; <br />}< Br/> @ override <br/> Public String tostring () {<br/> stringbuilder builder = new stringbuilder (); <br/> for (INT I = 0; I <size; I ++) {<br/> builder. append (elements [I]); <br/> if (I! = Size-1) {<br/> builder. append (","); <br/>}< br/> return builder. tostring (); <br/>}< br/>}
- Minimum heap:/** <Br/> * Desc: <br/> * Minimum heap <br> <br/> * ------------------------------------------------------------------------ <br/> * ver. date who what <br/> * region <br/> * 0.0.1 2011-1-4 leisore add <br/> * region <br/> */<br/> package CN. leisore. DSA. ds_tsinghua.c H06.sec04; </P> <p>/** <br/> * minheap <br/> * @ author leisore <br/> * @ since version 0.0.1 <br /> */<br/> public class minheap <t extends comparable <? Super T> extends heap <t >{< br/> Public minheap () {<br/> super (); <br/>}< br/> Public minheap (INT capacity) {<br/> super (capacity ); <br/>}< br/> Public minheap (T [] elements) {<br/> super (elements ); <br/>}</P> <p>/** <br/> * change the binary tree with startpos as root to a minimum heap. <br/> * <br /> * @ Param startpos <br/> */<br/> protected void filterdown (INT startpos) {<br/> int I = startpos; <br/> Int J = I * 2 + 1; <br/> while (j <size) {<br/> If (j <size-1 & elements [J]. compareto (elements [J + 1])> 0) {<br/> J ++; <br/>}< br/> If (elements [I]. compareto (elements [J]) <= 0) {<br/> break; <br/>}else {<br/> swap (I, j ); <br/> I = J; <br/> J = 2 * j + 1; <br/>}< br/>/** <br/> * change the binary tree with startpos as root to a minimum heap. <br/> * @ Param startpos <br/> */<br/> protected void filterup (INT startpos) {<br/> int I = startpos; <br/> Int J = (startpos-1)/2; <br/> while (I> 0) {<br/> If (elements [J]. compareto (elements [I]) <0) {<br/> break; <br/>}else {<br/> swap (I, j ); <br/> I = J; <br/> J = (I-1)/2; <br/>}< br/>}
- Max heap:/** <Br/> * Desc: <br/> * max heap <br> <br/> * ------------------------------------------------------------------------ <br/> * ver. date who what <br/> * region <br/> * 0.0.1 2011-1-4 leisore add <br/> * region <br/> */<br/> package CN. leisore. DSA. ds_tsinghua.c H06.sec04; <br/>/** <br/> * maxheap <br/> * @ author leisore <br/> * @ since version 0.0.1 <br/> * /<br/> public class maxheap <t extends comparable <? Super T> extends heap <t >{< br/> Public maxheap () {<br/> super (); <br/>}< br/> Public maxheap (INT capacity) {<br/> super (capacity ); <br/>}< br/> Public maxheap (T [] elements) {<br/> super (elements ); <br/>}< br/>/** <br/> * the binary tree with startpos as root is adjusted down to a maximum heap. <br/> * @ Param startpos <br/> */<br/> protected void filterdown (INT startpos) {<br/> int I = startpos; <br/> Int J = I * 2 + 1; <br/> while (j <size) {<br/> If (j <size-1 & elements [J]. compareto (elements [J + 1]) <0) {<br/> J ++; <br/>}< br/> If (elements [J]. compareto (elements [I]) <0) {<br/> break; <br/>}else {<br/> swap (I, j ); <br/> I = J; <br/> J = I * 2 + 1; <br/>}< br/>/** <br/> * change the binary tree with startpos as root to a minimum heap. <br/> * @ Param startpos <br/> */<br/> protected void filterup (INT startpos) {<br/> int I = startpos; <br/> Int J = (startpos-1)/2; <br/> while (I> 0) {<br/> If (elements [J]. compareto (elements [I])> 0) {<br/> break; <br/>}else {<br/> swap (I, j ); <br/> I = J; <br/> J = (I-1)/2; <br/>}< br/>}
- Minimum heap-based priority queue:/** <Br/> * Desc: <br/> * priority queue based on the minimum heap <br> <br/> * protocol <br/> * ver. date who what <br/> * region <br/> * 0.0.1 2011-1-4 leisore add <br/> * region <br/> */<br/> package CN. leisore. DSA. ds_ts Inghua. ch06.sec04; <br/> Import CN. leisore. DSA. ds_tsinghua.ch04.sec03.queue; <br/>/** <br/> * priorityqueue <br/> * @ author leisore <br/> * @ since version 0.0.1 <br/> * /<br/> public class priorityqueue <t extends comparable <? Super T> extends minheap <t> <br/> implements queue <t> {<br/> Public priorityqueue () {<br/> super (); <br/>}< br/> Public priorityqueue (INT capacity) {<br/> super (capacity ); <br/>}< br/> Public priorityqueue (T [] elements) {<br/> super (elements ); <br/>}< br/> @ override <br/> Public t dequeue () {<br/> return remove (); <br/>}< br/> @ override <br/> Public void enqueue (T Val) {<br/> insert (VAL ); <br/>}< br/> @ override <br/> Public Boolean isempty () {<br/> return size = 0; <br/>}< br/> @ override <br/> Public Boolean isfull () {<br/> return false; <br/>}< br/> @ override <br/> Public void makeempty () {<br/> while (size> 0) {<br/> elements [-- size] = NULL; <br/>}< br/> @ override <br/> Public t getfront () {<br/> return PEEK (); <br/>}< br/>}
- Simple test program and output:/** <Br/> * Desc: <br/> * Minimum heap test <br> <br/> * ---------------------------------------------------------------------- <br/>. date who what <br/> * region <br/> * 0.0.1 2011-1-4 leisore add <br/> * region <br/> */<br/> package CN. leisore. DSA. ds_tsinghua.ch06.sec04; <br/>/** <br/> * testminheap <br/> * @ author leisore <br/> * @ since version 0.0.1 <br/> * /<br/> public class testheap {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> heap <integer> minheap = new minheap <integer> (); <br/> testheap (minheap ); </P> <p> heap <integer> maxheap = new maxheap <integer> (4); <br/> testheap (maxheap ); </P> <p> heap <integer> PQ = new priorityqueue <integer> (20); <br/> testheap (PQ ); <br/>}</P> <p> static void testheap (heap <integer> heap) {<br/> integer [] arr = new integer [] {53, 17, 78, 23, 45, 65, 87, 9}; <br/> system. out. println ("heap:" + heap. getclass (). getsimplename (); <br/> for (INT I = 0; I <arr. length; I ++) {<br/> heap. insert (ARR [I]); <br/> system. out. println ("insert" + I + ":" + heap); <br/>}< br/> system. out. println ("heap:" + heap); <br/> for (INT I = 0; I <arr. length; I ++) {<br/> heap. remove (); <br/> system. out. println ("Remove" + I + ":" + heap ); <br/>}< br/>/** <br/> output: <br/> heap: minheap <br/> insert, 53 <br/> insert, <br/> insert, 53 <br/> insert,, <br/> insert, 78 <br/> insert, <br/> insert, 53 <br/> heap: 9,17, 65,23, 45,78, 87,53 <br/> remove, <br/> remove, 45, 65, 87, 78 <br/> remove, <br/> remove, 87 <br/> remove 4:, 87 <br/> remove 5: <br/> remove 6: 87 <br/> remove 7: <br/> heap: maxheap <br/> insert, 17 <br/> insert 2: 78,17, 53 <br/> insert 3: 78,23, 53,17 <br/> insert 4: 78,45, 53,17, 23 <br/> insert 5:, <br/> insert 6:, 53, 65 <br/> insert 7:, <br/> heap:, 45, <br/> remove 0:, 9 <br/> remove 1:, <br/> remove,, <br/> remove, 17 <br/> remove, 9 <br/> remove 7: <br/> heap: priorityqueue <br/> insert, 53 <br/> insert, <br/> insert, 53 <br/> insert, <br/> insert, 78 <br/> insert, <br/> insert, 53 <br/> heap, <br/> remove, <br/> remove, 78 <br/> remove, <br/> remove, 87 <br/> remove 4:, 87 <br/> remove 5: <br/> remove 6: 87 <br/> remove 7: <br/> */