<?php
class Heap {
var $numOfData;
var $heapArr = array();
var $comp;
function __construct($pc) { // 힙의 초기화
$this->numOfData = 0;
$this->comp = $pc;
}
function HIsEmpty() { // 힙이 비었는지 확인
if ($this->numOfData == 0)
return TRUE;
else
return FALSE;
}
function GetParentIDX($idx) { // 부모 노드의 인덱스 값 반환
return floor($idx/2);
}
function GetLChildIDX($idx) { // 왼쪽 자식 노드의 인덱스 값 반환
return $idx*2;
}
function GetRChildIDX($idx) { // 오른쪽 자식 노드의 인덱스 값 반환
return $this->GetLChildIDX($idx)+1;
}
// 두 개의 자식 노드중 높은 우선 순위의 자식 노드 인덱스 값 반환
function GetHiPriChildIDX($idx) {
if ($this->GetLChildIDX($idx) > $this->numOfData)
return 0;
else if ($this->GetLChildIDX($idx) == $this->numOfData)
return $this->GetLChildIDX($idx);
else
{
$comp = $this->comp;
if ($comp($this->heapArr[$this->GetLChildIDX($idx)],
$this->heapArr[$this->GetRChildIDX($idx)]) < 0)
return $this->GetRChildIDX($idx);
else
return $this->GetLChildIDX($idx);
}
}
// 힙에 데이터 저장
function HInsert($data) {
$idx = $this->numOfData+1;
$comp = $this->comp;
while ($idx != 1) {
if($comp($data, $this->heapArr[$this->GetParentIDX($idx)]) > 0) {
$this->heapArr[$idx] = $this->heapArr[$this->GetParentIDX($idx)];
$idx = $this->GetParentIDX($idx);
}
else
break;
}
$this->heapArr[$idx] = $data;
$this->numOfData += 1;
}
// 힙에서 데이터 삭제
function HDelete() {
$retData = $this->heapArr[1];
$lastElem = $this->heapArr[$this->numOfData];
$parentIdx = 1;
$childIdx;
$comp = $this->comp;
while ($childIdx = $this->GetHiPriChildIDX($parentIdx)) {
if ($comp($lastElem, $this->heapArr[$childIdx]) >= 0)
break;
$this->heapArr[$parentIdx] = $this->heapArr[$childIdx];
$parentIdx = $childIdx;
}
$this->heapArr[$parentIdx] = $lastElem;
$this->numOfData -= 1;
return $retData;
}
}
$arr = array(3, 4, 2, 1);
// 오름차순
function PriorityComp($d1, $d2) {
return $d2 - $d1;
}
$heap = new Heap(PriorityComp);
foreach ($arr as $val)
$heap->HInsert($val);
foreach ($arr as $k => $v)
$arr[$k] = $heap->Hdelete();
print_r($arr);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7830 | 9년 전 | 378 | ||
| 7829 |
|
9년 전 | 555 | |
| 7828 | 9년 전 | 491 | ||
| 7827 | 9년 전 | 373 | ||
| 7826 | 9년 전 | 383 | ||
| 7825 | 9년 전 | 436 | ||
| 7824 | 9년 전 | 417 | ||
| 7823 | 9년 전 | 324 | ||
| 7822 | 9년 전 | 320 | ||
| 7821 | 9년 전 | 264 | ||
| 7820 | 9년 전 | 320 | ||
| 7819 |
|
9년 전 | 723 | |
| 7818 | 10년 전 | 337 | ||
| 7817 | 10년 전 | 458 | ||
| 7816 | 10년 전 | 358 | ||
| 7815 | 10년 전 | 564 | ||
| 7814 | 10년 전 | 391 | ||
| 7813 | 10년 전 | 333 | ||
| 7812 | 10년 전 | 347 | ||
| 7811 | 10년 전 | 362 | ||
| 7810 | 10년 전 | 495 | ||
| 7809 | 10년 전 | 438 | ||
| 7808 | 10년 전 | 303 | ||
| 7807 | 10년 전 | 362 | ||
| 7806 |
프로그래머7
|
10년 전 | 1309 | |
| 7805 | 10년 전 | 1226 | ||
| 7804 |
zahir1312
|
10년 전 | 746 | |
| 7803 |
|
10년 전 | 1347 | |
| 7802 | 10년 전 | 404 | ||
| 7801 | 10년 전 | 831 | ||
| 7800 | 10년 전 | 1056 | ||
| 7799 | 10년 전 | 507 | ||
| 7798 | 10년 전 | 456 | ||
| 7797 | 10년 전 | 454 | ||
| 7796 | 10년 전 | 304 | ||
| 7795 | 10년 전 | 456 | ||
| 7794 | 10년 전 | 480 | ||
| 7793 | 10년 전 | 1001 | ||
| 7792 | 10년 전 | 409 | ||
| 7791 | 10년 전 | 493 | ||
| 7790 | 10년 전 | 457 | ||
| 7789 |
fbastore
|
10년 전 | 1403 | |
| 7788 | 10년 전 | 490 | ||
| 7787 | 10년 전 | 356 | ||
| 7786 | 10년 전 | 509 | ||
| 7785 | 10년 전 | 527 | ||
| 7784 | 10년 전 | 594 | ||
| 7783 | 10년 전 | 394 | ||
| 7782 | 10년 전 | 449 | ||
| 7781 | 10년 전 | 852 | ||
| 7780 | 10년 전 | 780 | ||
| 7779 | 10년 전 | 749 | ||
| 7778 | 10년 전 | 316 | ||
| 7777 | 10년 전 | 401 | ||
| 7776 | 10년 전 | 406 | ||
| 7775 | 10년 전 | 342 | ||
| 7774 | 10년 전 | 602 | ||
| 7773 | 10년 전 | 326 | ||
| 7772 | 10년 전 | 669 | ||
| 7771 | 10년 전 | 329 | ||
| 7770 | 10년 전 | 614 | ||
| 7769 | 10년 전 | 332 | ||
| 7768 | 10년 전 | 548 | ||
| 7767 | 10년 전 | 1117 | ||
| 7766 | 10년 전 | 444 | ||
| 7765 | 10년 전 | 479 | ||
| 7764 |
잘살아보자
|
10년 전 | 328 | |
| 7763 |
|
10년 전 | 1401 | |
| 7762 |
Tosea
|
10년 전 | 1019 | |
| 7761 | 10년 전 | 614 | ||
| 7760 |
잘살아보자
|
10년 전 | 607 | |
| 7759 |
잘살아보자
|
10년 전 | 424 | |
| 7758 |
잘살아보자
|
10년 전 | 536 | |
| 7757 | 10년 전 | 1182 | ||
| 7756 |
ITBANK
|
10년 전 | 1218 | |
| 7755 | 10년 전 | 1897 | ||
| 7754 | 10년 전 | 1001 | ||
| 7753 | 10년 전 | 845 | ||
| 7752 | 10년 전 | 1349 | ||
| 7751 |
잘살아보자
|
10년 전 | 475 | |
| 7750 |
잘살아보자
|
10년 전 | 441 | |
| 7749 |
잘살아보자
|
10년 전 | 442 | |
| 7748 |
잘살아보자
|
10년 전 | 423 | |
| 7747 |
잘살아보자
|
10년 전 | 515 | |
| 7746 |
잘살아보자
|
10년 전 | 636 | |
| 7745 |
잘살아보자
|
10년 전 | 877 | |
| 7744 |
잘살아보자
|
10년 전 | 392 | |
| 7743 | 10년 전 | 912 | ||
| 7742 |
starbros
|
10년 전 | 788 | |
| 7741 |
잘살아보자
|
10년 전 | 609 | |
| 7740 |
잘살아보자
|
10년 전 | 479 | |
| 7739 |
잘살아보자
|
10년 전 | 437 | |
| 7738 |
잘살아보자
|
10년 전 | 490 | |
| 7737 |
잘살아보자
|
10년 전 | 449 | |
| 7736 |
잘살아보자
|
10년 전 | 471 | |
| 7735 |
잘살아보자
|
10년 전 | 803 | |
| 7734 |
잘살아보자
|
10년 전 | 398 | |
| 7733 |
잘살아보자
|
10년 전 | 498 | |
| 7732 |
잘살아보자
|
10년 전 | 653 | |
| 7731 |
잘살아보자
|
10년 전 | 580 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기