-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.java
More file actions
141 lines (118 loc) · 2.58 KB
/
MaxHeap.java
File metadata and controls
141 lines (118 loc) · 2.58 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
135
136
137
138
139
140
141
import java.util.Collections;
import java.util.Vector;
public class MaxHeap {
public Vector<Node> nodes;
public int size;
public MaxHeap(){
nodes = new Vector<Node>();
nodes.add(null);
size = 0;
}
public void insert(int id, int cost){
Node node = new Node(id, cost);
nodes.add(node);
size++;
goUp(size);
}
public void extract(){
Node node = nodes.get(1);
Collections.swap(nodes, 1, size);
nodes.remove(size);
size--;
goDown(1);
}
public void decrease(int id, int cost){
int index = indexOfId(id);
nodes.get(index).cost = cost;
goDown(index);
}
public void increase(int id, int cost){
int index = indexOfId(id);
nodes.get(index).cost = cost;
goUp(index);
}
public void goUp(int index){
int currentPos = index;
while(currentPos > 1 && costAt(currentPos) > costAt(currentPos/2)){
Collections.swap(nodes, currentPos, currentPos/2);
currentPos = currentPos/2;
}
}
public void goDown(int index){
if(index <= size/2){
int currentPos = index;
int posAux;
if(costAt(greaterChild(currentPos)) > costAt(currentPos)){
posAux = greaterChild(currentPos);
Collections.swap(nodes, currentPos, greaterChild(currentPos));
goDown(posAux);
}
}
}
public int rightChildCost(int index){
if(size >= index*2 +1)
return costAt(index*2 +1);
else
return -Integer.MAX_VALUE;
}
public int leftChildCost(int index){
if(size >= index*2)
return costAt(index*2);
else
return -Integer.MAX_VALUE;
}
public boolean hasChild(int index){
if(size >= index*2)
return true;
else
return false;
}
public int greaterChild(int index){
int a = rightChildCost(index);
int b = leftChildCost(index);
if(a > b){
return index*2+1;}
else{
return index*2;}
}
public int father(int index){
return index/2;
}
public int costAt(int index){
return nodes.get(index).cost;
}
public void swap(int a, int b){
Node aux = nodes.get(a);
nodes.add(a, nodes.get(b));
nodes.add(b, aux);
}
public int indexOfId(int id){
int res = -1;
for(int i = 1; i < size; i++){
if(id == nodes.get(i).id)
res = i;
}
return res;
}
public void printHeap(){
for(int i = 1; i < size; i++){
System.out.println(costAt(i));
System.out.println("oi");
}
}
public static void main(String[] args){
MaxHeap hp = new MaxHeap();
hp.insert(1, 100);
hp.insert(2, 300);
hp.insert(3, 250);
hp.insert(4, 1000);
hp.insert(5, 500);
hp.insert(6, 254);
hp.insert(7, 15);
//hp.decrease(5, 302);
hp.extract();
for(int i = 1; i <= hp.size; i++){
System.out.println(hp.nodes.get(i).cost);
}
}
}