You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Binary tree where all levels except the last are completely filled orelse if any levels are partially filled then all nodes in that level should be as left as possible.
HEAP DS
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
It follows the Heap Property –
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
WAYS OF IMPLEMENTATION
NOTE: We are going to continue with Min Heap implementations. For Max Heap, every approach will be as simillar as MinHeap with some small changes you yourself will know the changes once you complete MinHeap implementations.
Functions Overview in MinHeap
1. Insertion in MinHeap
2. Display Heap
3. Height of Heap Tree
4. Current Heap Size
5. minExtract()
Heapify
6. minDeleteKey()
DecreaseKey()
7. Heap Sort
1. Insertion in MinHeap TC - O(log N)
//Main calling functionMin_Heaphp=newMin_Heap(hCap); // object of Min Heap classcase1: System.out.println("Enter value");
valu=sc.nextInt();
hp.minInsert(valu);
break;
//minInsert STARTSpublicvoidminInsert(intval) {
if(hSize==hCap) {
System.out.println("Heap overflow");
return;
}
System.out.println("Value Inserted in Heap");
hSize++;
inti=hSize-1;
hArr[i]=val;
//here checking if currInsertedNode is lesser than parent node -->then SWAPwhile(i!=0 && hArr[i] < hArr[parent(i)]) {
swap(i, parent(i));
i=parent(i);
}
}
//minInsert ENDS
//Main calling functionMin_Heaphp=newMin_Heap(hCap); // object of Min Heap classcase6: System.out.println("Enter key to be deleted");
valu=sc.nextInt();
hp.minDeleteKey(valu);
break;
// minDeleteKey operation STARTSpublicvoidminDeleteKey(inti) {
if(i>=hSize) {
System.out.println("Enter valid key"); return;
}
decreaseKey(i,-9999);
minExtract();
}
//this () will set the value in deletingIndex with minimum value than will keep on swaping that deletingIndex value with its parents untill it reaches root.privatevoiddecreaseKey(inti, intminVal){
hArr[i]=minVal;
while( i!=0 && hArr[i] < hArr[parent(i)] ) {
swap(i, parent(i));
i=parent(i);
}
}
// minDeleteKey operation ENDS