<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7130 | 11년 전 | 2304 | ||
| 7129 | 11년 전 | 711 | ||
| 7128 |
senseme
|
11년 전 | 1302 | |
| 7127 | 11년 전 | 1175 | ||
| 7126 | 11년 전 | 824 | ||
| 7125 | 11년 전 | 2953 | ||
| 7124 | 11년 전 | 1428 | ||
| 7123 |
잘살아보자
|
11년 전 | 1291 | |
| 7122 |
잘살아보자
|
11년 전 | 689 | |
| 7121 |
잘살아보자
|
11년 전 | 3052 | |
| 7120 |
잘살아보자
|
11년 전 | 2919 | |
| 7119 |
잘살아보자
|
11년 전 | 732 | |
| 7118 |
잘살아보자
|
11년 전 | 3433 | |
| 7117 |
잘살아보자
|
11년 전 | 854 | |
| 7116 | 11년 전 | 746 | ||
| 7115 | 11년 전 | 1035 | ||
| 7114 | 11년 전 | 853 | ||
| 7113 | 11년 전 | 667 | ||
| 7112 | 11년 전 | 1087 | ||
| 7111 | 11년 전 | 1800 | ||
| 7110 | 11년 전 | 1035 | ||
| 7109 |
버섯먹은나
|
11년 전 | 813 | |
| 7108 | 11년 전 | 648 | ||
| 7107 | 11년 전 | 2413 | ||
| 7106 | 11년 전 | 1963 | ||
| 7105 | 11년 전 | 2485 | ||
| 7104 | 11년 전 | 1425 | ||
| 7103 |
|
11년 전 | 2712 | |
| 7102 | 11년 전 | 3122 | ||
| 7101 | 11년 전 | 4447 | ||
| 7100 | 11년 전 | 5794 | ||
| 7099 | 11년 전 | 2143 | ||
| 7098 | 11년 전 | 1726 | ||
| 7097 | 11년 전 | 1464 | ||
| 7096 | 11년 전 | 1132 | ||
| 7095 |
잘살아보자
|
11년 전 | 859 | |
| 7094 |
잘살아보자
|
11년 전 | 1063 | |
| 7093 |
잘살아보자
|
11년 전 | 940 | |
| 7092 |
잘살아보자
|
11년 전 | 1413 | |
| 7091 |
잘살아보자
|
11년 전 | 2212 | |
| 7090 |
잘살아보자
|
11년 전 | 897 | |
| 7089 | 11년 전 | 1071 | ||
| 7088 | 11년 전 | 1715 | ||
| 7087 | 11년 전 | 1412 | ||
| 7086 | 11년 전 | 1314 | ||
| 7085 |
|
11년 전 | 1239 | |
| 7084 | 11년 전 | 1157 | ||
| 7083 | 11년 전 | 3507 | ||
| 7082 | 11년 전 | 1156 | ||
| 7081 | 11년 전 | 1739 | ||
| 7080 | 11년 전 | 2026 | ||
| 7079 | 11년 전 | 1334 | ||
| 7078 | 11년 전 | 1305 | ||
| 7077 | 11년 전 | 1314 | ||
| 7076 | 11년 전 | 772 | ||
| 7075 | 11년 전 | 1155 | ||
| 7074 |
네이비칼라
|
11년 전 | 1510 | |
| 7073 | 11년 전 | 1480 | ||
| 7072 |
|
11년 전 | 836 | |
| 7071 | 11년 전 | 1211 | ||
| 7070 | 11년 전 | 936 | ||
| 7069 | 11년 전 | 1710 | ||
| 7068 | 11년 전 | 1367 | ||
| 7067 |
TPSint
|
11년 전 | 750 | |
| 7066 | 11년 전 | 2155 | ||
| 7065 | 11년 전 | 6653 | ||
| 7064 | 11년 전 | 1298 | ||
| 7063 | 11년 전 | 1142 | ||
| 7062 | 11년 전 | 1081 | ||
| 7061 | 11년 전 | 920 | ||
| 7060 | 11년 전 | 1476 | ||
| 7059 | 11년 전 | 887 | ||
| 7058 |
EngineMan
|
11년 전 | 3408 | |
| 7057 | 11년 전 | 1251 | ||
| 7056 | 11년 전 | 1642 | ||
| 7055 | 11년 전 | 1022 | ||
| 7054 | 11년 전 | 2660 | ||
| 7053 |
네이비칼라
|
11년 전 | 1291 | |
| 7052 | 11년 전 | 2584 | ||
| 7051 | 11년 전 | 1944 | ||
| 7050 | 11년 전 | 2457 | ||
| 7049 | 11년 전 | 1414 | ||
| 7048 | 11년 전 | 2540 | ||
| 7047 | 11년 전 | 1363 | ||
| 7046 | 11년 전 | 1042 | ||
| 7045 | 11년 전 | 1069 | ||
| 7044 | 11년 전 | 5441 | ||
| 7043 | 11년 전 | 1234 | ||
| 7042 | 11년 전 | 794 | ||
| 7041 |
열라뽕똬이
|
11년 전 | 763 | |
| 7040 | 11년 전 | 1118 | ||
| 7039 | 11년 전 | 1470 | ||
| 7038 | 11년 전 | 1882 | ||
| 7037 | 11년 전 | 2182 | ||
| 7036 | 11년 전 | 1089 | ||
| 7035 | 11년 전 | 1412 | ||
| 7034 | 11년 전 | 1364 | ||
| 7033 |
열라뽕똬이
|
11년 전 | 1001 | |
| 7032 | 11년 전 | 756 | ||
| 7031 | 11년 전 | 2188 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기