forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OpenGenus#6716 from AlecNesatColak/master
Added minHeap.py Data Structure Algorithm
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
public class MaxHeap { | ||
private int[] heap; | ||
private int currSize; | ||
private int capacity; | ||
|
||
public MaxHeap(int capacity) { | ||
this.heap = new int[capacity]; | ||
this.currSize = 0; | ||
this.capacity = capacity; | ||
} | ||
|
||
private void heapifyAdd(int idx) { | ||
int root = (idx - 1) / 2; | ||
if(heap[idx] > heap[root]) { | ||
int temp = heap[root]; | ||
heap[root] = heap[idx]; | ||
heap[idx] = temp; | ||
heapifyAdd(root); | ||
} | ||
} | ||
|
||
private void heapifyRemove(int idx) { | ||
int max = idx; | ||
int leftIdx = idx * 2 + 1; | ||
int rightIdx = idx * 2 + 2; | ||
if(leftIdx < currSize && heap[leftIdx] > heap[idx]) { | ||
max = leftIdx; | ||
} | ||
if(rightIdx < currSize && heap[rightIdx] > heap[max]) { | ||
max = rightIdx; | ||
} | ||
if(max != idx) { // swap | ||
int temp = heap[max]; | ||
heap[max] = heap[idx]; | ||
heap[idx] = temp; | ||
heapifyRemove(max); | ||
} | ||
} | ||
|
||
public void insert(int itm) { | ||
if(currSize == capacity) { | ||
return; | ||
} | ||
heap[currSize] = itm; | ||
currSize++; | ||
heapifyAdd(currSize - 1); | ||
} | ||
|
||
public void remove() { | ||
if (currSize == 0) { | ||
return; | ||
} | ||
heap[0] = heap[currSize - 1]; | ||
currSize--; | ||
heapifyRemove(0); | ||
} | ||
|
||
public void print() { | ||
for(int i = 0; i < currSize; i++) { | ||
System.out.print(heap[i] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
MaxHeap maxHeap = new MaxHeap(10); | ||
maxHeap.insert(3); | ||
maxHeap.insert(1); | ||
maxHeap.insert(6); | ||
maxHeap.insert(5); | ||
maxHeap.insert(9); | ||
maxHeap.insert(8); | ||
|
||
maxHeap.print(); // Expected output: 9 5 8 1 3 6 | ||
|
||
maxHeap.remove(); | ||
|
||
maxHeap.print(); // Expected output: 8 5 6 1 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import sys | ||
|
||
class MinHeap: | ||
|
||
def __init__(self, maxsize): | ||
self.maxsize = maxsize | ||
self.size = 0 | ||
self.Heap = [0] * (self.maxsize + 1) | ||
self.Heap[0] = -1 * sys.maxsize | ||
self.FRONT = 1 | ||
|
||
def parent(self, pos): | ||
return pos // 2 | ||
|
||
def leftChild(self, pos): | ||
return 2 * pos | ||
|
||
def rightChild(self, pos): | ||
return (2 * pos) + 1 | ||
|
||
def isLeaf(self, pos): | ||
if pos >= (self.size//2) and pos <= self.size: | ||
return True | ||
return False | ||
|
||
def swap(self, fpos, spos): | ||
self.Heap[fpos], self.Heap[spos] = (self.Heap[spos], | ||
self.Heap[fpos]) | ||
|
||
def minHeapify(self, pos): | ||
if not self.isLeaf(pos): | ||
if (self.Heap[pos] > self.Heap[self.leftChild(pos)] or | ||
self.Heap[pos] > self.Heap[self.rightChild(pos)]): | ||
|
||
# Swap with the left child and heapify the left child | ||
if (self.Heap[self.leftChild(pos)] < | ||
self.Heap[self.rightChild(pos)]): | ||
self.swap(pos, self.leftChild(pos)) | ||
self.minHeapify(self.leftChild(pos)) | ||
|
||
# Swap with the right child and heapify the right child | ||
else: | ||
self.swap(pos, self.rightChild(pos)) | ||
self.minHeapify(self.rightChild(pos)) | ||
|
||
def insert(self, element): | ||
if self.size >= self.maxsize: | ||
return | ||
self.size += 1 | ||
self.Heap[self.size] = element | ||
|
||
current = self.size | ||
|
||
while (self.Heap[current] < | ||
self.Heap[self.parent(current)]): | ||
self.swap(current, self.parent(current)) | ||
current = self.parent(current) | ||
|
||
def Print(self): | ||
for i in range(1, (self.size // 2) + 1): | ||
print(" PARENT : " + str(self.Heap[i]) + | ||
" LEFT CHILD : " + str(self.Heap[2 * i]) + | ||
" RIGHT CHILD : " + str(self.Heap[2 * i + 1])) | ||
|
||
def extractMin(self): | ||
popped = self.Heap[self.FRONT] | ||
self.Heap[self.FRONT] = self.Heap[self.size] | ||
self.size -= 1 | ||
self.minHeapify(self.FRONT) | ||
return popped |