Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1468ab

Browse files
heap
1 parent 44e2f10 commit b1468ab

File tree

6 files changed

+224
-3
lines changed

6 files changed

+224
-3
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package geeksForGeeks.binarySearchTree;
2+
3+
4+
// Java implementation of Min Heap
5+
public class MinHeap {
6+
private int[] Heap;
7+
private int size;
8+
private int maxsize;
9+
10+
private static final int FRONT = 1;
11+
12+
public MinHeap(int maxsize)
13+
{
14+
this.maxsize = maxsize;
15+
this.size = 0;
16+
Heap = new int[this.maxsize + 1];
17+
Heap[0] = Integer.MIN_VALUE;
18+
}
19+
20+
// Function to return the position of
21+
// the parent for the node currently
22+
// at pos
23+
private int parent(int pos)
24+
{
25+
return pos / 2;
26+
}
27+
28+
// Function to return the position of the
29+
// left child for the node currently at pos
30+
private int leftChild(int pos)
31+
{
32+
return (2 * pos);
33+
}
34+
35+
// Function to return the position of
36+
// the right child for the node currently
37+
// at pos
38+
private int rightChild(int pos)
39+
{
40+
return (2 * pos) + 1;
41+
}
42+
43+
// Function that returns true if the passed
44+
// node is a leaf node
45+
private boolean isLeaf(int pos)
46+
{
47+
if (pos >= (size / 2) && pos <= size) {
48+
return true;
49+
}
50+
return false;
51+
}
52+
53+
// Function to swap two nodes of the heap
54+
private void swap(int fpos, int spos)
55+
{
56+
int tmp;
57+
tmp = Heap[fpos];
58+
Heap[fpos] = Heap[spos];
59+
Heap[spos] = tmp;
60+
}
61+
62+
// Function to heapify the node at pos
63+
private void minHeapify(int pos)
64+
{
65+
66+
// If the node is a non-leaf node and greater
67+
// than any of its child
68+
if (!isLeaf(pos)) {
69+
if (Heap[pos] > Heap[leftChild(pos)]
70+
|| Heap[pos] > Heap[rightChild(pos)]) {
71+
72+
// Swap with the left child and heapify
73+
// the left child
74+
if (Heap[leftChild(pos)] < Heap[rightChild(pos)]) {
75+
swap(pos, leftChild(pos));
76+
minHeapify(leftChild(pos));
77+
}
78+
79+
// Swap with the right child and heapify
80+
// the right child
81+
else {
82+
swap(pos, rightChild(pos));
83+
minHeapify(rightChild(pos));
84+
}
85+
}
86+
}
87+
}
88+
89+
// Function to insert a node into the heap
90+
public void insert(int element)
91+
{
92+
if (size >= maxsize) {
93+
return;
94+
}
95+
Heap[++size] = element;
96+
int current = size;
97+
98+
while (Heap[current] < Heap[parent(current)]) {
99+
swap(current, parent(current));
100+
current = parent(current);
101+
}
102+
}
103+
104+
// Function to print the contents of the heap
105+
public void print()
106+
{
107+
for (int i = 1; i <= size / 2; i++) {
108+
System.out.print(" PARENT : " + Heap[i]
109+
+ " LEFT CHILD : " + Heap[2 * i]
110+
+ " RIGHT CHILD :" + Heap[2 * i + 1]);
111+
System.out.println();
112+
}
113+
}
114+
115+
// Function to build the min heap using
116+
// the minHeapify
117+
public void minHeap()
118+
{
119+
for (int pos = (size / 2); pos >= 1; pos--) {
120+
minHeapify(pos);
121+
}
122+
}
123+
124+
// Function to remove and return the minimum
125+
// element from the heap
126+
public int remove()
127+
{
128+
int popped = Heap[FRONT];
129+
Heap[FRONT] = Heap[size--];
130+
minHeapify(FRONT);
131+
return popped;
132+
}
133+
134+
// Driver code
135+
public static void main(String[] arg)
136+
{
137+
System.out.println("The Min Heap is ");
138+
MinHeap minHeap = new MinHeap(15);
139+
minHeap.insert(5);
140+
minHeap.insert(3);
141+
minHeap.insert(17);
142+
minHeap.insert(10);
143+
minHeap.insert(84);
144+
minHeap.insert(19);
145+
minHeap.insert(6);
146+
minHeap.insert(22);
147+
minHeap.insert(9);
148+
minHeap.minHeap();
149+
150+
minHeap.print();
151+
System.out.println("The Min val is " + minHeap.remove());
152+
}
153+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package javaConcepts.collection;
2+
3+
// Java program to demonstrate working of PriorityQueue
4+
// The highest priority number or the smallest number would be removed by poll()
5+
// Technically it does auto sorting. removes element in log(n)
6+
7+
import java.util.*;
8+
9+
public class PriroityQueue {
10+
11+
// Driver code
12+
public static void main(String args[])
13+
{
14+
// Creating empty priority queue
15+
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
16+
17+
// Adding items to the pQueue using add()
18+
pQueue.add(30);
19+
pQueue.add(10);
20+
pQueue.add(20);
21+
pQueue.add(2);
22+
pQueue.add(400);
23+
24+
// Printing the most priority element
25+
System.out.println("Head value using peek function:" + pQueue.peek());
26+
27+
// Printing all elements
28+
System.out.println("The queue elements:");
29+
Iterator itr = pQueue.iterator();
30+
while (itr.hasNext())
31+
System.out.println(itr.next());
32+
33+
// Removing the top priority element (or head) and
34+
// printing the modified pQueue using poll()
35+
pQueue.poll();
36+
System.out.println("After removing top priority element "
37+
+ "with poll function:");
38+
Iterator<Integer> itr2 = pQueue.iterator();
39+
while (itr2.hasNext())
40+
System.out.println(itr2.next());
41+
42+
// Removing 30 using remove()
43+
pQueue.remove(30);
44+
System.out.println("after removing 30 with"
45+
+ " remove function:");
46+
Iterator<Integer> itr3 = pQueue.iterator();
47+
while (itr3.hasNext())
48+
System.out.println(itr3.next());
49+
50+
// Check if an element is present using contains()
51+
boolean b = pQueue.contains(20);
52+
System.out.println("Priority queue contains 20 "
53+
+ "or not?: " + b);
54+
55+
// Getting objects from the queue using toArray()
56+
// in an array and print the array
57+
Object[] arr = pQueue.toArray();
58+
System.out.println("Value in array: ");
59+
for (int i = 0; i < arr.length; i++)
60+
System.out.println("Value: " + arr[i].toString());
61+
}
62+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.premaseem.datastructure.heap;
2+
3+
public class MinHeap {
4+
5+
6+
}

‎src/me/premaseem/datastructure/heap/Heap.java renamed to ‎src/me/premaseem/datastructure/heap/imp1/Heap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.premaseem.datastructure.heap;
1+
package me.premaseem.datastructure.heap.imp1;
22

33
public class Heap {
44

‎src/me/premaseem/datastructure/heap/Node.java renamed to ‎src/me/premaseem/datastructure/heap/imp1/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.premaseem.datastructure.heap;
1+
package me.premaseem.datastructure.heap.imp1;
22

33
public class Node {
44
int key;

‎src/me/premaseem/datastructure/heap/Test.java renamed to ‎src/me/premaseem/datastructure/heap/imp1/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.premaseem.datastructure.heap;
1+
package me.premaseem.datastructure.heap.imp1;
22

33
public class Test {
44
Heap heap = new Heap(20);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /