<?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년 전 | 784 | ||
| 7529 |
파랑새1597
|
10년 전 | 1203 | |
| 7528 |
파랑새1597
|
10년 전 | 1308 | |
| 7527 |
integrity7
|
10년 전 | 1390 | |
| 7526 | 10년 전 | 2404 | ||
| 7525 |
다빈치코드777
|
10년 전 | 1098 | |
| 7524 | 10년 전 | 1562 | ||
| 7523 | 10년 전 | 955 | ||
| 7522 |
|
10년 전 | 987 | |
| 7521 |
blackkil
|
10년 전 | 1866 | |
| 7520 | 10년 전 | 1287 | ||
| 7519 |
Gaumi
|
10년 전 | 1076 | |
| 7518 | 10년 전 | 1475 | ||
| 7517 | 10년 전 | 823 | ||
| 7516 | 10년 전 | 1281 | ||
| 7515 | 10년 전 | 1394 | ||
| 7514 |
|
10년 전 | 4483 | |
| 7513 |
멋진남자임
|
10년 전 | 1135 | |
| 7512 |
다빈치코드777
|
10년 전 | 866 | |
| 7511 |
|
10년 전 | 3388 | |
| 7510 | 10년 전 | 1360 | ||
| 7509 | 10년 전 | 1139 | ||
| 7508 | 10년 전 | 713 | ||
| 7507 |
senseme
|
10년 전 | 738 | |
| 7506 |
멋진남자임
|
10년 전 | 1631 | |
| 7505 | 10년 전 | 3999 | ||
| 7504 | 10년 전 | 2139 | ||
| 7503 | 10년 전 | 988 | ||
| 7502 | 10년 전 | 516 | ||
| 7501 | 10년 전 | 1438 | ||
| 7500 | 10년 전 | 1487 | ||
| 7499 | 10년 전 | 3383 | ||
| 7498 | 10년 전 | 1202 | ||
| 7497 |
dethos79
|
10년 전 | 2962 | |
| 7496 | 10년 전 | 2146 | ||
| 7495 | 10년 전 | 857 | ||
| 7494 |
CHAVO
|
10년 전 | 1127 | |
| 7493 | 10년 전 | 2644 | ||
| 7492 | 10년 전 | 1261 | ||
| 7491 | 10년 전 | 1468 | ||
| 7490 | 10년 전 | 2333 | ||
| 7489 | 10년 전 | 2118 | ||
| 7488 |
toptopon
|
10년 전 | 891 | |
| 7487 |
|
10년 전 | 1028 | |
| 7486 | 10년 전 | 3348 | ||
| 7485 | 10년 전 | 1307 | ||
| 7484 | 10년 전 | 1370 | ||
| 7483 | 10년 전 | 1019 | ||
| 7482 | 10년 전 | 647 | ||
| 7481 | 10년 전 | 854 | ||
| 7480 | 10년 전 | 1223 | ||
| 7479 | 10년 전 | 2597 | ||
| 7478 | 10년 전 | 1161 | ||
| 7477 |
멋진남자임
|
10년 전 | 1514 | |
| 7476 |
zeppeto
|
10년 전 | 1141 | |
| 7475 |
200점아빠
|
10년 전 | 909 | |
| 7474 | 10년 전 | 4001 | ||
| 7473 | 10년 전 | 988 | ||
| 7472 |
나르시스1
|
10년 전 | 1239 | |
| 7471 | 10년 전 | 871 | ||
| 7470 | 10년 전 | 1271 | ||
| 7469 |
플라이SINJI
|
10년 전 | 967 | |
| 7468 |
|
10년 전 | 541 | |
| 7467 |
|
10년 전 | 651 | |
| 7466 | 10년 전 | 1113 | ||
| 7465 | 10년 전 | 1179 | ||
| 7464 |
|
10년 전 | 1186 | |
| 7463 | 10년 전 | 1242 | ||
| 7462 |
진짜별사탕
|
10년 전 | 854 | |
| 7461 | 10년 전 | 941 | ||
| 7460 | 10년 전 | 3725 | ||
| 7459 |
멋진남자임
|
10년 전 | 1552 | |
| 7458 |
멋진남자임
|
10년 전 | 478 | |
| 7457 | 10년 전 | 916 | ||
| 7456 | 10년 전 | 759 | ||
| 7455 | 10년 전 | 2147 | ||
| 7454 | 10년 전 | 628 | ||
| 7453 | 10년 전 | 819 | ||
| 7452 |
중국어사이트제작
|
10년 전 | 503 | |
| 7451 | 10년 전 | 901 | ||
| 7450 | 10년 전 | 625 | ||
| 7449 |
울라라라우
|
10년 전 | 946 | |
| 7448 | 10년 전 | 1629 | ||
| 7447 |
멋진남자임
|
10년 전 | 497 | |
| 7446 | 10년 전 | 547 | ||
| 7445 |
네이비칼라
|
10년 전 | 1669 | |
| 7444 |
senseme
|
10년 전 | 1402 | |
| 7443 | 10년 전 | 1335 | ||
| 7442 | 10년 전 | 724 | ||
| 7441 |
멋진남자임
|
10년 전 | 1429 | |
| 7440 | 10년 전 | 902 | ||
| 7439 |
|
10년 전 | 753 | |
| 7438 |
|
10년 전 | 930 | |
| 7437 |
basement
|
10년 전 | 1028 | |
| 7436 |
잘살아보자
|
10년 전 | 1121 | |
| 7435 | 10년 전 | 1080 | ||
| 7434 | 10년 전 | 3788 | ||
| 7433 |
|
10년 전 | 2730 | |
| 7432 |
alexkim
|
10년 전 | 857 | |
| 7431 |
이웃집초보
|
11년 전 | 1303 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기