Skip to content

Commit 64fce94

Browse files
committed
feat: 배열기반의 힙 클래스 삽입 기능 구현
1 parent a752802 commit 64fce94

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

yoonexample/src/main/java/heap/ArraySimpleHeap.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@ public boolean isEmpty() {
1818

1919
@Override
2020
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+
}
2135

36+
this.heapArr[index] = nextElement;
37+
this.numOfData += 1;
2238
}
2339

2440
@Override

0 commit comments

Comments
 (0)