라이브러리

[PHP] SplPriorityQueue::top - 큐 맨 위에서 노드를 살펴봅니다.




SplPriorityQueue::top

`SplPriorityQueue::top` 메서드는 SplPriorityQueue 객체의 가장 높은 우선 순위를 가진 요소를 반환합니다. 우선 순위는 SplPriorityQueue 객체에 추가된 요소의 우선 순위에 따라 결정됩니다.

SplPriorityQueue::top 사용법


`SplPriorityQueue::top` 메서드는 다음과 같은 형태로 사용할 수 있습니다.

#hostingforum.kr
php

SplPriorityQueue::top() : mixed



SplPriorityQueue::top 예제


#hostingforum.kr
php

// SplPriorityQueue 객체 생성

$pq = new SplPriorityQueue();



// 요소 추가 (우선 순위는 1, 2, 3 순으로 추가)

$pq->insert('apple', 1);

$pq->insert('banana', 2);

$pq->insert('cherry', 3);



// 가장 높은 우선 순위를 가진 요소 반환

$top = $pq->top();

echo "가장 높은 우선 순위를 가진 요소: $top
";



// 요소 삭제

$pq->extract();



// 가장 높은 우선 순위를 가진 요소 반환

$top = $pq->top();

echo "가장 높은 우선 순위를 가진 요소: $top
";



SplPriorityQueue::top 사용 시 주의점


- SplPriorityQueue 객체에 요소가 추가되지 않은 경우, `SplPriorityQueue::top` 메서드는 `Exception` 예외를 발생시킵니다.
- SplPriorityQueue 객체에 요소가 하나 이상 추가된 경우, `SplPriorityQueue::top` 메서드는 가장 높은 우선 순위를 가진 요소를 반환합니다. 만약 요소가 여러 개가 동일한 우선 순위를 가진 경우, SplPriorityQueue 객체는 가장 최근에 추가된 요소를 반환합니다.

SplPriorityQueue::top 사용 시 예외


- `SplPriorityQueue::top` 메서드는 SplPriorityQueue 객체에 요소가 추가되지 않은 경우 `Exception` 예외를 발생시킵니다.

#hostingforum.kr
php

try {

    $pq = new SplPriorityQueue();

    $pq->top();

} catch (Exception $e) {

    echo "SplPriorityQueue 객체에 요소가 추가되지 않았습니다.
";

}



SplPriorityQueue::top 사용 시 성능


- SplPriorityQueue 객체에 요소가 추가된 경우, `SplPriorityQueue::top` 메서드는 O(1) 시간 복잡도로 수행됩니다.
- SplPriorityQueue 객체에 요소가 추가되지 않은 경우, `SplPriorityQueue::top` 메서드는 O(1) 시간 복잡도로 수행됩니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 204 페이지

검색

게시물 검색