<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 430 | 19년 전 | 4099 | ||
| 429 | 19년 전 | 3301 | ||
| 428 | 19년 전 | 4132 | ||
| 427 | 19년 전 | 3473 | ||
| 426 | 19년 전 | 3104 | ||
| 425 | 19년 전 | 3407 | ||
| 424 | 19년 전 | 2438 | ||
| 423 | 19년 전 | 2862 | ||
| 422 | 19년 전 | 2351 | ||
| 421 | 19년 전 | 3708 | ||
| 420 | 19년 전 | 4740 | ||
| 419 | 19년 전 | 3801 | ||
| 418 |
|
19년 전 | 1749 | |
| 417 | 19년 전 | 2747 | ||
| 416 | 19년 전 | 2390 | ||
| 415 | 19년 전 | 2606 | ||
| 414 | 19년 전 | 4410 | ||
| 413 |
|
19년 전 | 2599 | |
| 412 | 19년 전 | 3047 | ||
| 411 |
|
19년 전 | 2996 | |
| 410 |
|
19년 전 | 3685 | |
| 409 |
|
19년 전 | 3634 | |
| 408 |
|
19년 전 | 1858 | |
| 407 | 19년 전 | 2230 | ||
| 406 | 19년 전 | 2782 | ||
| 405 | 19년 전 | 2448 | ||
| 404 | 19년 전 | 4303 | ||
| 403 | 19년 전 | 3304 | ||
| 402 |
NeoGenesis
|
19년 전 | 4089 | |
| 401 | 19년 전 | 2606 | ||
| 400 |
|
19년 전 | 2495 | |
| 399 | 19년 전 | 2957 | ||
| 398 | 19년 전 | 2485 | ||
| 397 | 19년 전 | 2780 | ||
| 396 | 19년 전 | 2476 | ||
| 395 | 19년 전 | 3179 | ||
| 394 | 19년 전 | 1720 | ||
| 393 | 19년 전 | 2994 | ||
| 392 | 19년 전 | 2253 | ||
| 391 | 19년 전 | 2176 | ||
| 390 | 19년 전 | 2286 | ||
| 389 | 19년 전 | 2645 | ||
| 388 | 19년 전 | 2180 | ||
| 387 | 19년 전 | 4484 | ||
| 386 |
|
19년 전 | 2692 | |
| 385 |
|
19년 전 | 2481 | |
| 384 | 19년 전 | 3016 | ||
| 383 | 19년 전 | 3054 | ||
| 382 | 19년 전 | 3107 | ||
| 381 |
|
19년 전 | 2631 | |
| 380 |
|
19년 전 | 3019 | |
| 379 | 19년 전 | 2549 | ||
| 378 | 19년 전 | 2206 | ||
| 377 | 19년 전 | 2778 | ||
| 376 | 19년 전 | 2469 | ||
| 375 |
|
19년 전 | 2562 | |
| 374 | 19년 전 | 3819 | ||
| 373 | 19년 전 | 3269 | ||
| 372 | 19년 전 | 4986 | ||
| 371 |
세은아빠2
|
19년 전 | 2415 | |
| 370 | 19년 전 | 4500 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2907 | ||
| 367 | 19년 전 | 3725 | ||
| 366 | 19년 전 | 2655 | ||
| 365 | 19년 전 | 3736 | ||
| 364 | 19년 전 | 4011 | ||
| 363 | 19년 전 | 3439 | ||
| 362 | 19년 전 | 3480 | ||
| 361 | 19년 전 | 4107 | ||
| 360 |
hwatta
|
19년 전 | 2362 | |
| 359 | 19년 전 | 5101 | ||
| 358 | 19년 전 | 3656 | ||
| 357 | 19년 전 | 2598 | ||
| 356 |
sdesign1s
|
19년 전 | 2276 | |
| 355 | 20년 전 | 2756 | ||
| 354 | 20년 전 | 3029 | ||
| 353 | 20년 전 | 2790 | ||
| 352 |
|
20년 전 | 5773 | |
| 351 |
|
20년 전 | 2702 | |
| 350 |
|
20년 전 | 4293 | |
| 349 |
hwatta
|
20년 전 | 2182 | |
| 348 | 20년 전 | 7307 | ||
| 347 | 20년 전 | 2413 | ||
| 346 | 20년 전 | 3505 | ||
| 345 | 20년 전 | 4307 | ||
| 344 | 20년 전 | 2648 | ||
| 343 | 20년 전 | 3920 | ||
| 342 | 20년 전 | 3067 | ||
| 341 | 20년 전 | 4091 | ||
| 340 |
|
20년 전 | 5141 | |
| 339 |
|
20년 전 | 4234 | |
| 338 | 20년 전 | 5869 | ||
| 337 | 20년 전 | 2045 | ||
| 336 |
|
20년 전 | 3321 | |
| 335 |
|
20년 전 | 3538 | |
| 334 |
|
20년 전 | 2927 | |
| 333 |
hwatta
|
20년 전 | 2435 | |
| 332 | 20년 전 | 4655 | ||
| 331 | 20년 전 | 2272 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기