<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 189 | ||
| 8229 | 9년 전 | 169 | ||
| 8228 |
커네드커네드
|
9년 전 | 208 | |
| 8227 | 9년 전 | 249 | ||
| 8226 | 9년 전 | 269 | ||
| 8225 | 9년 전 | 248 | ||
| 8224 | 9년 전 | 254 | ||
| 8223 | 9년 전 | 232 | ||
| 8222 |
|
9년 전 | 294 | |
| 8221 | 9년 전 | 193 | ||
| 8220 | 9년 전 | 236 | ||
| 8219 | 9년 전 | 207 | ||
| 8218 | 9년 전 | 253 | ||
| 8217 |
star3840
|
9년 전 | 215 | |
| 8216 | 9년 전 | 288 | ||
| 8215 | 9년 전 | 228 | ||
| 8214 | 9년 전 | 336 | ||
| 8213 | 9년 전 | 295 | ||
| 8212 | 9년 전 | 209 | ||
| 8211 | 9년 전 | 379 | ||
| 8210 | 9년 전 | 371 | ||
| 8209 | 9년 전 | 453 | ||
| 8208 | 9년 전 | 341 | ||
| 8207 | 9년 전 | 350 | ||
| 8206 |
|
9년 전 | 296 | |
| 8205 | 9년 전 | 271 | ||
| 8204 | 9년 전 | 257 | ||
| 8203 | 9년 전 | 338 | ||
| 8202 | 9년 전 | 250 | ||
| 8201 | 9년 전 | 290 | ||
| 8200 | 9년 전 | 294 | ||
| 8199 | 9년 전 | 317 | ||
| 8198 | 9년 전 | 281 | ||
| 8197 | 9년 전 | 264 | ||
| 8196 | 9년 전 | 689 | ||
| 8195 | 9년 전 | 274 | ||
| 8194 | 9년 전 | 389 | ||
| 8193 | 9년 전 | 303 | ||
| 8192 | 9년 전 | 309 | ||
| 8191 | 9년 전 | 267 | ||
| 8190 | 9년 전 | 250 | ||
| 8189 | 9년 전 | 309 | ||
| 8188 | 9년 전 | 241 | ||
| 8187 | 9년 전 | 260 | ||
| 8186 | 9년 전 | 255 | ||
| 8185 | 9년 전 | 425 | ||
| 8184 | 9년 전 | 212 | ||
| 8183 | 9년 전 | 420 | ||
| 8182 | 9년 전 | 288 | ||
| 8181 | 9년 전 | 245 | ||
| 8180 | 9년 전 | 821 | ||
| 8179 | 9년 전 | 594 | ||
| 8178 | 9년 전 | 452 | ||
| 8177 |
kiplayer
|
9년 전 | 450 | |
| 8176 | 9년 전 | 481 | ||
| 8175 | 9년 전 | 366 | ||
| 8174 | 9년 전 | 359 | ||
| 8173 | 9년 전 | 452 | ||
| 8172 | 9년 전 | 331 | ||
| 8171 | 9년 전 | 294 | ||
| 8170 | 9년 전 | 409 | ||
| 8169 |
커네드커네드
|
9년 전 | 363 | |
| 8168 | 9년 전 | 447 | ||
| 8167 | 9년 전 | 439 | ||
| 8166 | 9년 전 | 338 | ||
| 8165 | 9년 전 | 275 | ||
| 8164 | 9년 전 | 411 | ||
| 8163 | 9년 전 | 415 | ||
| 8162 | 9년 전 | 398 | ||
| 8161 | 9년 전 | 413 | ||
| 8160 |
|
9년 전 | 634 | |
| 8159 | 9년 전 | 579 | ||
| 8158 | 9년 전 | 372 | ||
| 8157 | 9년 전 | 492 | ||
| 8156 | 9년 전 | 363 | ||
| 8155 | 9년 전 | 376 | ||
| 8154 |
00년생용띠
|
9년 전 | 704 | |
| 8153 | 9년 전 | 343 | ||
| 8152 |
|
9년 전 | 520 | |
| 8151 | 9년 전 | 511 | ||
| 8150 | 9년 전 | 630 | ||
| 8149 |
Jangfolk
|
9년 전 | 487 | |
| 8148 | 9년 전 | 303 | ||
| 8147 | 9년 전 | 487 | ||
| 8146 | 9년 전 | 568 | ||
| 8145 | 9년 전 | 523 | ||
| 8144 | 9년 전 | 493 | ||
| 8143 | 9년 전 | 318 | ||
| 8142 | 9년 전 | 538 | ||
| 8141 | 9년 전 | 485 | ||
| 8140 | 9년 전 | 1053 | ||
| 8139 | 9년 전 | 389 | ||
| 8138 |
전갈자리남자
|
9년 전 | 494 | |
| 8137 | 9년 전 | 532 | ||
| 8136 | 9년 전 | 863 | ||
| 8135 |
|
9년 전 | 905 | |
| 8134 |
PlayPixel
|
9년 전 | 646 | |
| 8133 |
|
9년 전 | 551 | |
| 8132 | 9년 전 | 588 | ||
| 8131 | 9년 전 | 943 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기