-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
143 lines (121 loc) · 2.78 KB
/
Copy pathHeap.java
File metadata and controls
143 lines (121 loc) · 2.78 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
142
143
package sortingAnalysis;
public class Heap<E extends Comparable<E>>
{
private java.util.ArrayList<E> list = new java.util.ArrayList<>();
private static long comparisons = 0;
private static long movements = 0;
// Create a default heap
public Heap()
{
}
// Getters
public static long getComparisons()
{
return comparisons;
}
public static long getMovements()
{
return movements;
}
// Setters
public static void setComparisons(int num)
{
comparisons = num;
}
public static void setMovements(int num)
{
movements = num;
}
// Create a heap from an array of objects
public Heap(E[] objects)
{
for (int i = 0; i < objects.length; i++)
{
add(objects[i]);
}
}
// Add a new object into the heap
public void add(E newObject)
{
list.add(newObject); // Append to the heap
int currentIndex = list.size() - 1; // Index of the last node
movements++;
while (currentIndex > 0)
{
int parentIndex = (currentIndex - 1) / 2;
// Swap if the current object is greater than its parent
comparisons++;
movements++;
if (list.get(currentIndex).compareTo(list.get(parentIndex)) > 0)
{
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
movements++;
movements++;
movements++;
comparisons++;
}
else
break; // The tree is a heap now
currentIndex = parentIndex;
}
}
// Remove the root from the heap
public E remove()
{
comparisons++;
if (list.size() == 0) return null;
E removedObject = list.get(0);
movements++;
list.set(0, list.get(list.size() - 1));
movements++;
list.remove(list.size() - 1);
movements++;
int currentIndex = 0;
movements++;
while (currentIndex < list.size())
{
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
movements++;
movements++;
// Find maximum between two children
comparisons++;
if (leftChildIndex >= list.size()) break; // Tree is a heap
int maxIndex = leftChildIndex;
movements++;
comparisons++;
if (rightChildIndex < list.size())
{
comparisons++;
if(list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0)
{
maxIndex = rightChildIndex;
movements++;
}
}
// Swap if the current node is less than maximum
comparisons++;
if (list.get(currentIndex).compareTo(list.get(maxIndex)) < 0)
{
E temp = list.get(maxIndex);
movements++;
list.set(maxIndex, list.get(currentIndex));
movements++;
list.set(currentIndex, temp);
movements++;
currentIndex = maxIndex;
movements++;
}
else
break; // Tree is a heap
}
return removedObject;
}
// Get the number of nodes in the tree
public int getSize()
{
return list.size();
}
}