-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeap.java
52 lines (45 loc) · 1.44 KB
/
MinHeap.java
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
/*
Creating a min heap by extending from the generic heap class.
Implements some of the heap methods to specifically make it
a min heap.
*/
public class MinHeap extends Heap {
// recursive method to swap nodes up tree
public void heapify(Node<Student> node) {
if (node.getParent() == null) { // top of tree
return;
}
// if smaller name is on bottom then switch them
if (node.getData().getName().compareTo(node.getParent().getData().getName()) < 0) {
swapData(node, node.getParent());
heapify(node.getParent()); // move up the tree
}
}
// gets the min name between two nodes
private Node<Student> min(Node<Student> a, Node<Student> b) {
if (a == null) {
return b;
} else if (b == null) {
return a;
} else { // if both nodes are not null
// return the node with the smaller name
if (a.getData().getName().compareTo(b.getData().getName()) > 0) {
return b;
} else {
return a;
}
}
}
// heapify down the tree starting from the root
public void reverseHeapify(Node<Student> node) {
if (node.getLeft() == null && node.getRight() == null) { // bottom of tree
return;
}
// if bigger name is at the top then switch them
Node<Student> minChild = min(node.getLeft(), node.getRight());
if (node.getData().getName().compareTo(minChild.getData().getName()) > 0) {
swapData(node, minChild);
reverseHeapify(minChild); // move down the tree
}
}
}