문자열에 힙을 만들고 다양한 기능을 수행하는 일을했습니다. 이제 코드가 제대로 삽입되는지 확인하기 위해 코드를 테스트하고 있습니다. 나는 단어 테스트 해요 :문자열 힙이 제대로 삽입되지 않았습니다.
public boolean insert(String key) {
if(currentSize == maxSize) {
return false;
}
Node newNode = new Node(key);
heapArray[currentSize] = newNode;
trickleUp(currentSize++);
return true;
}
public void trickleUp(int index) {
int parent = (index - 1)/2;
Node bottom = heapArray[index];
while(index > 0 && heapArray[parent].getKey().compareTo(bottom.getKey()) > 0) {
heapArray[index] = heapArray[parent];
index = parent;
parent = (parent - 1)/2;
}
heapArray[index] = bottom;
}
:
Alpha
Bravo Charlie
Foxtrot Delta Hotel Echo
Golf
다음
내가 작성한 코드입니다 : 내 힙을 인쇄 할 때를 삽입합니다 Golf, Bravo, Hotel, Alpha, Delta, Echo, Charlie, Foxtrot
을 알파벳 순으로 그러나 나는 끝낼 편집 : 빠른 검색을 수행하고 힙에 대한 다른 소스 코드를 찾은 후 그것을 테스트하고 동일한 출력을 받았습니다. 이것이 알파벳순으로 추가되지 않는 이유가 있습니까?