@@ -5,7 +5,11 @@ public class ArraySimpleHeap<E> implements SimpleHeap<E> {
5
5
private static final int INITIAL_CAPACITY = 100 + 1 ;
6
6
7
7
private int numOfData ;
8
- private Object [] heapArr = new Object [INITIAL_CAPACITY ];
8
+ private HeapElement <E >[] heapArr ;
9
+
10
+ public ArraySimpleHeap () {
11
+ this .heapArr = new HeapElement [INITIAL_CAPACITY ];
12
+ }
9
13
10
14
@ Override
11
15
public boolean isEmpty () {
@@ -21,4 +25,71 @@ public void insert(E data, int priority) {
21
25
public E delete () {
22
26
return null ;
23
27
}
28
+
29
+ /**
30
+ * 부모 노드의 인덱스 반환 요청
31
+ *
32
+ * @param currentIndex 현재 자식 노드의 인덱스
33
+ * @return 부모노드의 인덱스
34
+ */
35
+ private int getParentIndex (int currentIndex ) {
36
+ return currentIndex / 2 ;
37
+ }
38
+
39
+ /**
40
+ * 왼쪽 자식 노드의 인덱스 반환 요청
41
+ *
42
+ * @param parentIndex 부모 노드의 인덱스
43
+ * @return 왼쪽 자식 노드의 인덱스
44
+ */
45
+ private int getLeftChildIndex (int parentIndex ) {
46
+ return parentIndex * 2 ;
47
+ }
48
+
49
+ /**
50
+ * 오른쪽 자식 노드의 인덱스 반환 요청
51
+ *
52
+ * @param parentIndex 부모 노드의 인덱스
53
+ * @return 오른쪽 자식 노드의 인덱스
54
+ */
55
+ private int getRightChildIndex (int parentIndex ) {
56
+ return parentIndex * 2 + 1 ;
57
+ }
58
+
59
+ /**
60
+ * 두 개의 자식 노드 중 우선순위가 더 높은 자식의 인덱스 반환 요청
61
+ *
62
+ * @param currentIndex 판단의 기준이 되는 노드
63
+ * @return 우선순위가 더 높은 자식 노드의 인덱스
64
+ */
65
+ private int getHighPrioirtyChildIndex (int currentIndex ) {
66
+ int leftChildIndex = this .getLeftChildIndex (currentIndex );
67
+ if (leftChildIndex > this .numOfData ) { // 존재하지 않는 노드라면
68
+ return 0 ;
69
+ }
70
+ if (leftChildIndex == this .numOfData ) { // 왼쪽노드가 마지막 노드라면
71
+ return leftChildIndex ;
72
+ }
73
+
74
+ int rightChildIndex = this .getRightChildIndex (currentIndex );
75
+ int leftChildNodePriority = this .heapArr [leftChildIndex ].priority ;
76
+ int rightChildNodePriority = this .heapArr [rightChildIndex ].priority ;
77
+
78
+ // 우선순위는 낮은것이 더 우위이므로 왼쪽노드가 더 우선순위가 낮다면
79
+ if (leftChildNodePriority > rightChildNodePriority ) {
80
+ return rightChildIndex ;
81
+ }
82
+ return leftChildIndex ;
83
+ }
84
+
85
+ private static class HeapElement <T > {
86
+
87
+ private final T data ;
88
+ private final int priority ;
89
+
90
+ public HeapElement (T data , int priority ) {
91
+ this .data = data ;
92
+ this .priority = priority ;
93
+ }
94
+ }
24
95
}
0 commit comments