We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a752802 commit 64fce94Copy full SHA for 64fce94
yoonexample/src/main/java/heap/ArraySimpleHeap.java
@@ -18,7 +18,23 @@ public boolean isEmpty() {
18
19
@Override
20
public void insert(E data, int priority) {
21
+ int index = numOfData + 1;
22
+ HeapElement<E> nextElement = new HeapElement<>(data, priority);
23
+
24
+ while (index != 1) {
25
+ int parentIndex = getParentIndex(index);
26
27
+ // 부모 노드와 비교했을 때, 부모노드보다 우선순위가 높다면 서로의 위치를 바꾼다.
28
+ if (priority < this.heapArr[parentIndex].priority) {
29
+ this.heapArr[index] = this.heapArr[parentIndex];
30
+ index = parentIndex;
31
+ } else {
32
+ break;
33
+ }
34
35
36
+ this.heapArr[index] = nextElement;
37
+ this.numOfData += 1;
38
}
39
40
0 commit comments