A binary heap is a heap data structure created using a binary tree.
Binary tree has a rules-
- Binary Heap have to is complete binary tree at all levels except. This is called the shapeproperty.
- All nodes is either greater than equal to (max-heap) or less than equal to (min-heap) to each of its CH ILD nodes. This is called the heapproperty.
Implementation:
- Use array to store the data.
- Start storing from index 1, not 0.
- For any given node at position I:
- Its left-child is at [2*i] if available.
- It right child is at [2*i+1] if available.
- Its Parent Node was at [I/2]if available.
Heap Majorly has 3 operations-
- Insert operation
- Delete operation
- Extract-min (OR Extract-max)
Insert Operation:
- Add the element at the bottom leaf of the Heap.
- Perform the bubble-up operation.
- All inserts Operations must perform the bubble-up operation (It is also called as Up-heap, percolate-up, S Ift-up, trickle-up, heapify-up, or cascade-up)
bubble-up Operation:
- If inserted element is smaller than it parent node in case of Min-heap OR greater than it parent node in case of Max-heap, swap the element with its parent.
- Keep repeating the above step, if node reaches its correct position, STOP.
Insert ()-bubble-up min-heap
Extract-min OR Extract-max operation:
- Take the-out of the element from the root. (It'll be minimum in case of min-heap and maximum in case of max-heap).
- The last element is the last, from the same as the heap and replace the root with the element.
- Perform Sink-down
- All delete operation must perform Sink-down operation (also known as Bubble-down, percolate-down, Sift-down, trickledown, heapify-down, cascade-down).
Sink-down Operation:
- If replaced element is greater than any of it child node in case of Min-heap OR smaller than any if it child node In case of Max-heap, the swap the element has a smallest child (min-heap) or a with its greatest child (MAX-HEAP).
- Keep repeating the above step, if node reaches its correct position, STOP.
Delete OR Extract Min from Heap
Delete Operation:
- Find the index for the element to be deleted.
- The last element is the last, from the same as the heap and replace the index with this element.
- Perform Sink-down
Time and Space complexity:
| Space |
O (N) |
| Search |
O (N) |
| Insert |
O (log n) |
| Delete |
O (log n) |
Packagedata_structure_testing;Importjava.util.ArrayList;ImportJava.util.HashSet; Public classMinheap { PublicArraylist<integer> minheap =NULL; Publicminheap () { This. Minheap =NewArraylist<integer> (); This. Minheap.add (Integer.min_value); /***-------------------------------------------------------------------------* integer.min_value | | | | | | | | | | | | | | | | | | | * ------------------------------------------------------------------------- */ } Public voidSwapintIintj) {if(i = = j)return; intTMP =Minheap.get (i); Minheap.set (I, Minheap.get (j)); Minheap.set (J, TMP); } Public intGetSize () {returnminheap.size (); } Public intParentinti) {returnI/2; } Public intLeftchild (inti) {returnI*2; } Public intRightchild (inti) {returnI*2 + 1; } Public voidOfferintx) {minheap.add (x); intPosition = Minheap.size ()-1; BubbleUp (position); } Public voidBubbleUp (intposition) { intPA =parent (position); if(PA! = 0 && minheap.get (position) <Minheap.get (PA)) {Swap (position, PA); BubbleUp (PA); } } Public intPeek () {if(GetSize () = = 1) {System.out.println ("The Min Heap is empty!"); returnMinheap.get (0); } Else{ returnMinheap.get (1); } } Public voidPushdown (inti) {intMin_position = i, Min_value =Minheap.get (i); intLC = Leftchild (i), rc = LC + 1; if(LC >=GetSize ()) { return; } if(LC <GetSize ()) { if(Minheap.get (LC) <min_value) {min_position=LC; Min_value=Minheap.get (LC); } if(RC < GetSize () && minheap.get (RC) <min_value) {min_position=RC; }} swap (min_position, i); if(Min_position! =i) {pushdown (min_position); } } Public intpoll () {if(GetSize () = = 1) {System.out.println ("Since the min heap is empty, so we cannot poll any element from it!"); returnMinheap.get (0); } inttop = Minheap.get (1); Swap (1, GetSize ()-1); Minheap.remove (GetSize ()-1); if(GetSize () > 1) {pushdown (1); } returntop; } Public Static voidMain (string[] args) {minheap minheap=Newminheap (); Minheap.offer (5); Minheap.offer (4); Minheap.offer (2); Minheap.offer (4); Minheap.offer (3); System.out.println (Minheap.poll ()); System.out.println (Minheap.poll ()); System.out.println (Minheap.poll ()); System.out.println (Minheap.poll ()); System.out.println (Minheap.poll ()); System.out.println (Minheap.poll ()); }}min Heap
[email protected] Implementation of minimal heap