From 14bbb96a2f0f95a87e71b30c999221ba6c7d7129 Mon Sep 17 00:00:00 2001 From: adeel-dev639 Date: Mon, 14 Oct 2024 23:40:41 +0500 Subject: [PATCH] Create Heap,java --- Java_Programs-master/Heap,java | 96 ++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Java_Programs-master/Heap,java diff --git a/Java_Programs-master/Heap,java b/Java_Programs-master/Heap,java new file mode 100644 index 00000000..58bd31f0 --- /dev/null +++ b/Java_Programs-master/Heap,java @@ -0,0 +1,96 @@ +import java.util.*; + + +class Heap{ + + int heapSize; + + void build_max_heap(int[] a) + { + heapSize=a.length; + for(int i=(heapSize/2);i>=0;i--) + max_heapify(a,i); + + } + + void max_heapify(int[] a,int i) + { + int l=2*i+1; + int r=2*i+2; + int largest=i; + if(la[largest]) + largest=l; + if(ra[largest]) + largest=r; + if(largest!=i) + { + int t=a[i]; + a[i]=a[largest]; + a[largest]=t; + max_heapify(a,largest); + } + + } + + //to delete the max element + + int extract_max(int[] a) + { + if(heapSize<0) + System.out.println("underflow"); + int max=a[0]; + a[0]=a[heapSize-1]; + heapSize--; + max_heapify(a,0); + return max; + } + + void increase_key(int[] a,int i,int key) + { + if(key=0 && a[(i-1)/2]