forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
112 lines (95 loc) · 3.03 KB
/
Copy pathHeap.java
File metadata and controls
112 lines (95 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.*;
//heap implementation using arraylist
public class Heap
{
ArrayList<Integer> data;
public Heap()
{
data=new ArrayList<Integer>();
data.add(null); //0 index has null starting adding from 1 index
}
public void insert(int priority)
{
data.add(priority); //add
int childIndex=data.size()-1; //getting index
int parentIndex=childIndex/2; //getting parent of new
while(childIndex!=-1 && parentIndex!=0)
{
if(data.get(childIndex)>=data.get(parentIndex))
{
break;
}
int childData=data.get(childIndex);
int parentData=data.get(parentIndex);
data.set(childIndex,parentData); //swap
data.set(parentIndex,childData);
childIndex=parentIndex; //new index
parentIndex=childIndex/2;
}
}
public int getMin()
{
if(getSize()==0)
{
return -1;
}
return data.get(1);
}
public int getSize()
{
return data.size()-1;
}
// if we have to remove min ek option h remove kro sara elememts shift kro but o(N) time complexity hogi
// 2nd hum krenga ki first ko last de replace last remove and again setting krlemga O(logn) me hojayega
public int removeMin()
{
int min=data.get(1); //first
int last=data.get(data.size()-1); //last
data.set(1,last); //swap
// data.set(data.size()-1,min); // or isko na hi kro 1 set hogya bs last apne aap hojega
data.remove(data.size()-1); //remove last now which is first that we swapped
int currentIndex=1;
int leftChildIndex=2*currentIndex;
int rightChildIndex=2*currentIndex+1;
while(leftChildIndex<data.size()-1)
{
int minIndex=currentIndex;
int currentData=data.get(currentIndex);
int leftData=data.get(leftChildIndex);
int rightData=data.get(rightChildIndex);
if(leftData<currentData)
{
minIndex=leftChildIndex;
}
if(rightChildIndex<data.size())
{
if(rightData<data.get(minIndex))
{
minIndex=rightChildIndex;
}
}
if(minIndex==currentIndex) //agar zarurat nhi aage
{
break;
}
data.set(currentIndex,data.get(minIndex));
data.set(minIndex,currentData);
currentIndex=minIndex;
leftChildIndex=2*currentIndex;
rightChildIndex=leftChildIndex+1;
}
return min;
}
public static void main(String[] args)
{
Heap p=new Heap();
p.insert(11);
p.insert(15);
p.insert(10);
p.insert(14);
p.insert(21);
System.out.println(p.getMin()); //min
System.out.println(p.removeMin()); //remove
System.out.println(p.getMin()); //again min
}
}