11package problems .leetcode ;
22
33import java .util .Arrays ;
4+ import java .util .Collections ;
5+ import java .util .List ;
46
57public class MaxHeap <T extends Comparable <T >> {
68
79 private T [] heap ;
810 private int size ;
911 private int maxSize ;
1012
11- // Constructor to initialize an
12- // empty max heap with given maximum
13- // capacity
1413 public MaxHeap (int maxSize ) {
15- // This keyword refers to current instance itself
16- this .maxSize = maxSize ;
17- this .size = 0 ;
18- this .heap = (T []) new Comparable [this .maxSize ];
14+ this (maxSize , Collections .emptyList ());
1915 }
2016
21- private int parent (int pos ) {
22- return ( pos - 1 ) / 2 ;
17+ public MaxHeap (int maxSize , T ... vals ) {
18+ this ( maxSize , Arrays . asList ( vals )) ;
2319 }
2420
25- private int leftChild (int pos ) {
26- return (2 * pos ) + 1 ;
21+ // heapify runtime: O(N)
22+ public MaxHeap (int maxSize , List <T > vals ) {
23+ this .size = vals .size ();
24+ this .maxSize = Math .max (maxSize , this .size );
25+ this .heap = (T []) new Comparable [this .maxSize + 1 ];
26+ // max heapify
27+ for (int i = 0 ; i < vals .size (); i ++) {
28+ this .heap [i + 1 ] = vals .get (i );
29+ }
30+ // heap[size/2..1] are leaves, which are heap themselves.
31+ for (int i = size / 2 ; i > 0 ; i --) {
32+ maxHeapify (i );
33+ }
34+ }
35+
36+ private int parent (int i ) {
37+ return i / 2 ;
38+ }
39+
40+ private int leftChild (int i ) {
41+ return 2 * i ;
2742 }
2843
29- private int rightChild (int pos ) {
30- return ( 2 * pos ) + 2 ;
44+ private int rightChild (int i ) {
45+ return 2 * i + 1 ;
3146 }
3247
3348 private void swap (int l , int r ) {
@@ -37,14 +52,19 @@ private void swap(int l, int r) {
3752 heap [r ] = tmp ;
3853 }
3954
55+ private boolean isGreater (int l , int r ) {
56+ return heap [l ].compareTo (heap [r ]) > 0 ;
57+ }
58+
59+ // runtime: O(logN)
4060 private void maxHeapify (int i ) {
4161 int l = leftChild (i );
4262 int r = rightChild (i );
4363 int largest = i ;
44- if (l < size && heap [ l ]. compareTo ( heap [ i ]) > 0 ) {
64+ if (l <= size && isGreater ( l , i ) ) {
4565 largest = l ;
4666 }
47- if (r < size && heap [ r ]. compareTo ( heap [ largest ]) > 0 ) {
67+ if (r <= size && isGreater ( r , largest ) ) {
4868 largest = r ;
4969 }
5070 if (largest != i ) {
@@ -57,27 +77,37 @@ public void add(T element) {
5777 if (size == maxSize ) {
5878 throw new IllegalStateException ("heap full!" );
5979 }
80+ heap [++size ] = element ;
81+ heapUp (size );
82+ }
6083
61- heap [size ] = element ;
62- int current = size ;
63- while (heap [current ].compareTo (heap [parent (current )]) > 0 ) {
64- swap (current , parent (current ));
65- current = parent (current );
84+ private void heapUp (int i ) {
85+ while (i > 1 && isGreater (i , parent (i ))) {
86+ swap (i , parent (i ));
87+ i = parent (i );
6688 }
67- size ++;
6889 }
6990
7091 public T remove () {
7192 if (size == 0 ) {
7293 throw new IllegalStateException ("empty heap!" );
7394 }
74- T popped = heap [0 ];
75- heap [0 ] = heap [-- size ];
76- heap [size ] = null ;
77- maxHeapify (0 );
95+ T popped = heap [1 ];
96+ heap [1 ] = heap [size ];
97+ heap [size -- ] = null ;
98+ maxHeapify (1 );
7899 return popped ;
79100 }
80101
102+ public void increase (int i , T val ) {
103+ if (i < 1 || i > size ) {
104+ throw new IndexOutOfBoundsException (i );
105+ }
106+
107+ heap [i ] = val ;
108+ heapUp (i );
109+ }
110+
81111 @ Override
82112 public String toString () {
83113 return Arrays .toString (heap );
@@ -104,5 +134,14 @@ public static void main(String[] args) {
104134
105135 System .out .println (heap .remove ());
106136 System .out .println (heap );
137+
138+ heap = new MaxHeap <>(10 , List .of (15 , 5 , 10 , 20 ));
139+ System .out .println (heap );
140+
141+ heap .increase (3 , 25 );
142+ System .out .println (heap );
143+ heap .increase (4 , 10 );
144+ System .out .println (heap );
107145 }
146+
108147}
0 commit comments