<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7630 | 10년 전 | 635 | ||
| 7629 |
|
10년 전 | 2352 | |
| 7628 | 10년 전 | 771 | ||
| 7627 |
|
10년 전 | 1005 | |
| 7626 |
|
10년 전 | 1761 | |
| 7625 | 10년 전 | 665 | ||
| 7624 | 10년 전 | 685 | ||
| 7623 |
|
10년 전 | 3021 | |
| 7622 | 10년 전 | 701 | ||
| 7621 |
leeleeleelee
|
10년 전 | 572 | |
| 7620 | 10년 전 | 530 | ||
| 7619 | 10년 전 | 463 | ||
| 7618 | 10년 전 | 1001 | ||
| 7617 | 10년 전 | 714 | ||
| 7616 | 10년 전 | 617 | ||
| 7615 | 10년 전 | 713 | ||
| 7614 | 10년 전 | 1222 | ||
| 7613 |
|
10년 전 | 2060 | |
| 7612 | 10년 전 | 1125 | ||
| 7611 | 10년 전 | 1385 | ||
| 7610 |
|
10년 전 | 1881 | |
| 7609 |
|
10년 전 | 1295 | |
| 7608 |
mwdkim
|
10년 전 | 1100 | |
| 7607 |
|
10년 전 | 1026 | |
| 7606 |
mwdkim
|
10년 전 | 3905 | |
| 7605 | 10년 전 | 672 | ||
| 7604 | 10년 전 | 1009 | ||
| 7603 | 10년 전 | 1634 | ||
| 7602 |
|
10년 전 | 1043 | |
| 7601 |
AniNest
|
10년 전 | 2765 | |
| 7600 |
port443
|
10년 전 | 1002 | |
| 7599 | 10년 전 | 927 | ||
| 7598 | 10년 전 | 993 | ||
| 7597 | 10년 전 | 4559 | ||
| 7596 |
SeungYeon
|
10년 전 | 870 | |
| 7595 |
untitled
|
10년 전 | 2397 | |
| 7594 |
프로그래머7
|
10년 전 | 1700 | |
| 7593 |
untitled
|
10년 전 | 2342 | |
| 7592 |
untitled
|
10년 전 | 1920 | |
| 7591 |
untitled
|
10년 전 | 2658 | |
| 7590 |
아리마2001
|
10년 전 | 829 | |
| 7589 | 10년 전 | 1088 | ||
| 7588 |
|
10년 전 | 2902 | |
| 7587 | 10년 전 | 1283 | ||
| 7586 | 10년 전 | 647 | ||
| 7585 | 10년 전 | 1664 | ||
| 7584 | 10년 전 | 1399 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1142 | |
| 7582 |
|
10년 전 | 1076 | |
| 7581 | 10년 전 | 1307 | ||
| 7580 | 10년 전 | 950 | ||
| 7579 |
|
10년 전 | 590 | |
| 7578 | 10년 전 | 1400 | ||
| 7577 |
|
10년 전 | 1857 | |
| 7576 | 10년 전 | 1369 | ||
| 7575 |
멋진남자임
|
10년 전 | 1448 | |
| 7574 | 10년 전 | 2092 | ||
| 7573 | 10년 전 | 3222 | ||
| 7572 | 10년 전 | 750 | ||
| 7571 |
|
10년 전 | 774 | |
| 7570 |
|
10년 전 | 1296 | |
| 7569 | 10년 전 | 1525 | ||
| 7568 |
this1mg
|
10년 전 | 1027 | |
| 7567 |
|
10년 전 | 736 | |
| 7566 | 10년 전 | 905 | ||
| 7565 |
Angel하늘
|
10년 전 | 968 | |
| 7564 |
seoldi
|
10년 전 | 1211 | |
| 7563 |
|
10년 전 | 1354 | |
| 7562 |
멋진남자임
|
10년 전 | 2053 | |
| 7561 | 10년 전 | 684 | ||
| 7560 |
leeleeleelee
|
10년 전 | 884 | |
| 7559 | 10년 전 | 5018 | ||
| 7558 |
RinaP
|
10년 전 | 756 | |
| 7557 |
|
10년 전 | 1223 | |
| 7556 | 10년 전 | 1172 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1640 | |
| 7554 | 10년 전 | 1078 | ||
| 7553 |
senseme
|
10년 전 | 1323 | |
| 7552 |
ehdltdoit
|
10년 전 | 1420 | |
| 7551 |
|
10년 전 | 1806 | |
| 7550 |
leeleeleelee
|
10년 전 | 1569 | |
| 7549 | 10년 전 | 2399 | ||
| 7548 | 10년 전 | 1820 | ||
| 7547 |
멋진남자임
|
10년 전 | 1934 | |
| 7546 | 10년 전 | 977 | ||
| 7545 |
ILMare1003
|
10년 전 | 1259 | |
| 7544 |
|
10년 전 | 1215 | |
| 7543 | 10년 전 | 867 | ||
| 7542 | 10년 전 | 637 | ||
| 7541 |
울라라라우
|
10년 전 | 850 | |
| 7540 | 10년 전 | 1587 | ||
| 7539 | 10년 전 | 909 | ||
| 7538 |
|
10년 전 | 1818 | |
| 7537 | 10년 전 | 3596 | ||
| 7536 |
Gaumi
|
10년 전 | 1389 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1250 | |
| 7534 |
senseme
|
10년 전 | 1196 | |
| 7533 | 10년 전 | 1176 | ||
| 7532 | 10년 전 | 838 | ||
| 7531 | 10년 전 | 2031 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기