-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinFourAryHeap.java
124 lines (101 loc) · 3.01 KB
/
MinFourAryHeap.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
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
package huffman;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MinFourAryHeap {
private HuffmanTreeNode[] heap;
private int size;
private int capacity;
private static final int TOP = 3;
//static final String code_table = "";// TO FILL
public MinFourAryHeap(HuffmanTreeNode[] aheapStorage) {
this.heap = new HuffmanTreeNode[aheapStorage.length + 3];
heap[TOP] = null;
this.size = aheapStorage.length+2;
capacity = size;
System.arraycopy(aheapStorage, 0, heap, 3, aheapStorage.length);
//System.
buildHeap(size);
}
private void buildHeap(int asize) {
for (int i = getParent(asize); i >= TOP; i--) {
heapifyUp(i);
}
}
public HuffmanTreeNode extractMin() {
HuffmanTreeNode result = heap[TOP];
heap[TOP] = heap[size];
heap[size] = null;
size--;
heapifyUp(TOP);
return result;
}
public void insertItem(HuffmanTreeNode item) {
if (size < capacity) {
heap[++size] = item;
int current = size;
while (isValid(current) && isValid(getParent(current)) && heap[getParent(current)].getFrequency() > heap[current].getFrequency()) {
swapTwoValues(getParent(current), current);
current = current / 4;
}
} else {
System.out.println("cannot insert : no space left");
}
}
private void heapifyUp(int index) {
int firstChildIdx = getFirstChild(index);
int secondChildIdx = getSecondChild(index);
int thirdChildIdx = getThirdChild(index);
int fourthChildIdx = getFourthChild(index);
int smallestIdx = firstChildIdx;
if (isValid(secondChildIdx)) {
//System.out.println("secondchildindex = " + secondChildIdx);
if (heap[secondChildIdx].getFrequency() < heap[smallestIdx].getFrequency()) {
smallestIdx = secondChildIdx;
}
}
if (isValid(thirdChildIdx)) {
if (heap[thirdChildIdx].getFrequency() < heap[smallestIdx].getFrequency()) {
smallestIdx = thirdChildIdx;
}
}
if (isValid(fourthChildIdx)) {
if (heap[fourthChildIdx].getFrequency() < heap[smallestIdx].getFrequency()) {
smallestIdx = fourthChildIdx;
}
}
if (isValid(smallestIdx)&& heap[smallestIdx].getFrequency() < heap[index].getFrequency()) {
swapTwoValues(smallestIdx, index);
heapifyUp(smallestIdx);
}
}
private void swapTwoValues(int smallestIdx, int index) {
HuffmanTreeNode temp = heap[smallestIdx];
heap[smallestIdx] = heap[index];
heap[index] = temp;
}
private boolean isValid(int index) {
return index >= TOP && index <= size;
}
private int getParent(int index) {
return (index / 4) +2;
}
private int getFirstChild(int index) {
return (4 * index) -8;
}
private int getSecondChild(int index) {
return (4 * index) -7;
}
private int getThirdChild(int index) {
return (4 * index) -6 ;
}
private int getFourthChild(int index) {
return (4 * index) - 5;
}
public int getSize() {
return size;
}
}