Skip to content

Commit c33aea9

Browse files
committed
feat: 배열기반의 힙 클래스 삭제 기능 구현
1 parent 64fce94 commit c33aea9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,25 @@ public void insert(E data, int priority) {
3939

4040
@Override
4141
public E delete() {
42-
return null;
42+
int rootIndex = 1;
43+
E retData = this.heapArr[rootIndex].data; // 루트노드에 존재하던 데이터
44+
HeapElement<E> lastElement = this.heapArr[this.numOfData]; // 마지막 노드
45+
46+
int parentIndex = rootIndex; // 루트로 옮기는 것을 의미
47+
int childIndex = getHighPrioirtyChildIndex(parentIndex); // 더 우선순위인 자식노드
48+
49+
while (childIndex > 0) { // 부모노드가 단말노드가 아니라면
50+
if (lastElement.priority <= this.heapArr[childIndex].priority) { // 마지막 노드가 우선순위가 높다면
51+
break;
52+
}
53+
this.heapArr[parentIndex] = this.heapArr[childIndex]; // 자식 노드와 부모노드의 위치를 변경
54+
parentIndex = childIndex;
55+
childIndex = getHighPrioirtyChildIndex(parentIndex);
56+
}
57+
58+
this.heapArr[parentIndex] = lastElement; // 마지막에 위치했던 노드를 한 번에 옮긴다.
59+
this.numOfData -= 1;
60+
return retData;
4361
}
4462

4563
/**

0 commit comments

Comments
 (0)