-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminheap.java
More file actions
134 lines (126 loc) · 3.73 KB
/
minheap.java
File metadata and controls
134 lines (126 loc) · 3.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
public class minheap {
public minheapnode[] Heaparray;
public int size;
public int capacity;
public static final int first=1;
//Initializing the min heap
public minheap()
{
this.capacity= 2000;
this.size = 0;
this.Heaparray = new minheapnode[this.capacity + 1];
Heaparray[0] = new minheapnode(-999, -999,0);
Heaparray[0].executed_time= Integer.MIN_VALUE;
}
//This is the minheapify at index i
private void minheapify(int i)
{ int smallest = i;
//Make the smallest as the current index
int l = 2*i;
int r = 2*i+1;
//set the left child and right child index
//if the left child executed time is less than the executed time of the parent
//set smallest as l
//consider the case when the executed time are equal compare their building numbers
{
if(l<=size && Heaparray[l].executed_time<Heaparray[i].executed_time)
{
smallest = l;
}
if(l<=size && Heaparray[l].executed_time == Heaparray[i].executed_time)
{
if(Heaparray[l].buildingNum<Heaparray[i].buildingNum)
{
smallest = l;
}
}
//Similarly if r is less than smallest then r is the smallest
if(r<=size && Heaparray[r].executed_time<Heaparray[smallest].executed_time)
{
smallest = r;
}
if(r<=size && Heaparray[r].executed_time == Heaparray[smallest].executed_time)
{
if(Heaparray[r].buildingNum<Heaparray[smallest].buildingNum)
{
smallest = r;
}
}
// swap function
if(smallest !=i)
{
minheapnode temp;
temp = Heaparray[i];
Heaparray[i] = Heaparray[smallest];
Heaparray[smallest] = temp;
minheapify(smallest);
}
}
}
//Insert and at the last point in the heap and heapify at that point
public void insert(minheapnode node)
{
Heaparray[++size] = node;
int current = size;
insertheapify(current);
}
// the insert heapify will simply compare the executed time child and its parent and swap when required
public void insertheapify(int child)
{
if(child == 1)
return;
int parent = child/2;
if(Heaparray[child].executed_time<Heaparray[parent].executed_time)
{
minheapnode temp ;
temp = Heaparray[child];
Heaparray[child]= Heaparray[parent];
Heaparray[parent]=temp;
insertheapify(parent);
}
if(Heaparray[child].executed_time == Heaparray[parent].executed_time)
{
if(Heaparray[child].buildingNum<Heaparray[parent].buildingNum)
{
minheapnode temp2 ;
temp2 = Heaparray[child];
Heaparray[child]= Heaparray[parent];
Heaparray[parent]=temp2;
insertheapify(parent);
}
}
}
// this is the insert function that returns a minheapnode that is used to in the redblack node as a minheap pointer
public minheapnode insertminheap(int a, int b, int c)
{
minheapnode x = new minheapnode(a,b,c);
insert(x);
return x;
}
//This simply prints the minheap
public void print()
{
for (int i = 1; i <= size / 2; i++ )
{
System.out.print(" PARENT : " + Heaparray[i].executed_time +"Parent Building Number:"+Heaparray[i].buildingNum+ " LEFT CHILD : " + Heaparray[2*i].executed_time
+ " RIGHT CHILD :" + Heaparray[2 * i + 1].executed_time);
System.out.println();
}
}
public void minHeap()
{
for (int pos = (size / 2); pos >= 1 ; pos--)
{
minheapify(pos);
}
}
//This function puts the last element at index 1
//After that it does the heapify at index 1
public minheapnode extractmin()
{
minheapnode popped = Heaparray[first];
Heaparray[first] = Heaparray[size--];
minheapify(first);
return popped;
}
}