<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7530 | 10년 전 | 813 | ||
| 7529 |
파랑새1597
|
10년 전 | 1230 | |
| 7528 |
파랑새1597
|
10년 전 | 1326 | |
| 7527 |
integrity7
|
10년 전 | 1407 | |
| 7526 | 10년 전 | 2432 | ||
| 7525 |
다빈치코드777
|
10년 전 | 1118 | |
| 7524 | 10년 전 | 1584 | ||
| 7523 | 10년 전 | 973 | ||
| 7522 |
|
10년 전 | 1003 | |
| 7521 |
blackkil
|
10년 전 | 1887 | |
| 7520 | 10년 전 | 1306 | ||
| 7519 |
Gaumi
|
10년 전 | 1089 | |
| 7518 | 10년 전 | 1497 | ||
| 7517 | 10년 전 | 840 | ||
| 7516 | 10년 전 | 1306 | ||
| 7515 | 10년 전 | 1422 | ||
| 7514 |
|
10년 전 | 4496 | |
| 7513 |
멋진남자임
|
10년 전 | 1139 | |
| 7512 |
다빈치코드777
|
10년 전 | 884 | |
| 7511 |
|
10년 전 | 3413 | |
| 7510 | 10년 전 | 1379 | ||
| 7509 | 10년 전 | 1155 | ||
| 7508 | 10년 전 | 722 | ||
| 7507 |
senseme
|
10년 전 | 761 | |
| 7506 |
멋진남자임
|
10년 전 | 1657 | |
| 7505 | 10년 전 | 4028 | ||
| 7504 | 10년 전 | 2162 | ||
| 7503 | 10년 전 | 999 | ||
| 7502 | 10년 전 | 525 | ||
| 7501 | 10년 전 | 1452 | ||
| 7500 | 10년 전 | 1503 | ||
| 7499 | 10년 전 | 3407 | ||
| 7498 | 10년 전 | 1240 | ||
| 7497 |
dethos79
|
10년 전 | 2971 | |
| 7496 | 10년 전 | 2171 | ||
| 7495 | 10년 전 | 899 | ||
| 7494 |
CHAVO
|
10년 전 | 1146 | |
| 7493 | 10년 전 | 2661 | ||
| 7492 | 10년 전 | 1292 | ||
| 7491 | 10년 전 | 1500 | ||
| 7490 | 10년 전 | 2348 | ||
| 7489 | 10년 전 | 2127 | ||
| 7488 |
toptopon
|
10년 전 | 904 | |
| 7487 |
|
10년 전 | 1045 | |
| 7486 | 10년 전 | 3368 | ||
| 7485 | 10년 전 | 1325 | ||
| 7484 | 10년 전 | 1382 | ||
| 7483 | 10년 전 | 1040 | ||
| 7482 | 10년 전 | 670 | ||
| 7481 | 10년 전 | 865 | ||
| 7480 | 10년 전 | 1241 | ||
| 7479 | 10년 전 | 2616 | ||
| 7478 | 10년 전 | 1181 | ||
| 7477 |
멋진남자임
|
10년 전 | 1532 | |
| 7476 |
zeppeto
|
10년 전 | 1149 | |
| 7475 |
200점아빠
|
10년 전 | 923 | |
| 7474 | 10년 전 | 4014 | ||
| 7473 | 10년 전 | 1006 | ||
| 7472 |
나르시스1
|
10년 전 | 1257 | |
| 7471 | 10년 전 | 885 | ||
| 7470 | 10년 전 | 1302 | ||
| 7469 |
플라이SINJI
|
10년 전 | 998 | |
| 7468 |
|
10년 전 | 567 | |
| 7467 |
|
10년 전 | 679 | |
| 7466 | 10년 전 | 1140 | ||
| 7465 | 10년 전 | 1197 | ||
| 7464 |
|
10년 전 | 1209 | |
| 7463 | 10년 전 | 1267 | ||
| 7462 |
진짜별사탕
|
10년 전 | 874 | |
| 7461 | 10년 전 | 962 | ||
| 7460 | 10년 전 | 3752 | ||
| 7459 |
멋진남자임
|
10년 전 | 1569 | |
| 7458 |
멋진남자임
|
10년 전 | 495 | |
| 7457 | 10년 전 | 929 | ||
| 7456 | 10년 전 | 775 | ||
| 7455 | 10년 전 | 2182 | ||
| 7454 | 10년 전 | 641 | ||
| 7453 | 10년 전 | 844 | ||
| 7452 |
중국어사이트제작
|
10년 전 | 512 | |
| 7451 | 10년 전 | 919 | ||
| 7450 | 10년 전 | 641 | ||
| 7449 |
울라라라우
|
10년 전 | 961 | |
| 7448 | 10년 전 | 1641 | ||
| 7447 |
멋진남자임
|
10년 전 | 516 | |
| 7446 | 10년 전 | 561 | ||
| 7445 |
네이비칼라
|
10년 전 | 1694 | |
| 7444 |
senseme
|
10년 전 | 1416 | |
| 7443 | 10년 전 | 1351 | ||
| 7442 | 10년 전 | 738 | ||
| 7441 |
멋진남자임
|
10년 전 | 1450 | |
| 7440 | 10년 전 | 924 | ||
| 7439 |
|
10년 전 | 778 | |
| 7438 |
|
10년 전 | 951 | |
| 7437 |
basement
|
10년 전 | 1047 | |
| 7436 |
잘살아보자
|
11년 전 | 1143 | |
| 7435 | 11년 전 | 1098 | ||
| 7434 | 11년 전 | 3799 | ||
| 7433 |
|
11년 전 | 2759 | |
| 7432 |
alexkim
|
11년 전 | 870 | |
| 7431 |
이웃집초보
|
11년 전 | 1323 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기