<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 4016 | ||
| 129 | 20년 전 | 2954 | ||
| 128 | 20년 전 | 3693 | ||
| 127 | 20년 전 | 3533 | ||
| 126 | 20년 전 | 3785 | ||
| 125 | 20년 전 | 8606 | ||
| 124 | 20년 전 | 2610 | ||
| 123 | 20년 전 | 3761 | ||
| 122 | 20년 전 | 3219 | ||
| 121 | 20년 전 | 2621 | ||
| 120 | 20년 전 | 2685 | ||
| 119 | 20년 전 | 2595 | ||
| 118 | 20년 전 | 2871 | ||
| 117 |
|
20년 전 | 3071 | |
| 116 | 20년 전 | 5334 | ||
| 115 | 20년 전 | 3936 | ||
| 114 | 20년 전 | 4986 | ||
| 113 | 20년 전 | 6228 | ||
| 112 | 21년 전 | 7334 | ||
| 111 | 21년 전 | 18446 | ||
| 110 | 21년 전 | 6887 | ||
| 109 | 21년 전 | 2896 | ||
| 108 | 21년 전 | 4151 | ||
| 107 |
prosper
|
21년 전 | 2512 | |
| 106 |
prosper
|
21년 전 | 4332 | |
| 105 |
아우겐나이스
|
21년 전 | 2929 | |
| 104 | 21년 전 | 2276 | ||
| 103 | 21년 전 | 2487 | ||
| 102 | 21년 전 | 2274 | ||
| 101 | 21년 전 | 2591 | ||
| 100 | 21년 전 | 1765 | ||
| 99 | 21년 전 | 1583 | ||
| 98 | 21년 전 | 1631 | ||
| 97 | 21년 전 | 2147 | ||
| 96 | 21년 전 | 1895 | ||
| 95 | 21년 전 | 2393 | ||
| 94 | 21년 전 | 3579 | ||
| 93 | 21년 전 | 1575 | ||
| 92 | 21년 전 | 1770 | ||
| 91 | 21년 전 | 3193 | ||
| 90 | 21년 전 | 2351 | ||
| 89 | 21년 전 | 3188 | ||
| 88 | 21년 전 | 2878 | ||
| 87 | 21년 전 | 3306 | ||
| 86 | 21년 전 | 5153 | ||
| 85 | 21년 전 | 2538 | ||
| 84 | 21년 전 | 4838 | ||
| 83 | 21년 전 | 2517 | ||
| 82 | 21년 전 | 3130 | ||
| 81 | 21년 전 | 7644 | ||
| 80 | 21년 전 | 3843 | ||
| 79 | 21년 전 | 3223 | ||
| 78 | 21년 전 | 4706 | ||
| 77 | 21년 전 | 2916 | ||
| 76 | 21년 전 | 6231 | ||
| 75 | 21년 전 | 4482 | ||
| 74 | 21년 전 | 5799 | ||
| 73 | 21년 전 | 3637 | ||
| 72 | 21년 전 | 5981 | ||
| 71 | 21년 전 | 3132 | ||
| 70 | 21년 전 | 2860 | ||
| 69 | 21년 전 | 2650 | ||
| 68 | 21년 전 | 2459 | ||
| 67 | 21년 전 | 2666 | ||
| 66 | 21년 전 | 2686 | ||
| 65 | 21년 전 | 3796 | ||
| 64 | 21년 전 | 2839 | ||
| 63 | 21년 전 | 2468 | ||
| 62 | 21년 전 | 2278 | ||
| 61 | 21년 전 | 3099 | ||
| 60 | 21년 전 | 3146 | ||
| 59 | 21년 전 | 2528 | ||
| 58 | 21년 전 | 2602 | ||
| 57 | 21년 전 | 2985 | ||
| 56 | 21년 전 | 2346 | ||
| 55 | 21년 전 | 2776 | ||
| 54 | 21년 전 | 2145 | ||
| 53 | 21년 전 | 2373 | ||
| 52 | 21년 전 | 2715 | ||
| 51 |
prosper
|
21년 전 | 2368 | |
| 50 |
prosper
|
21년 전 | 2184 | |
| 49 | 21년 전 | 2197 | ||
| 48 | 21년 전 | 2357 | ||
| 47 | 21년 전 | 1950 | ||
| 46 | 21년 전 | 1937 | ||
| 45 | 21년 전 | 2145 | ||
| 44 | 21년 전 | 2375 | ||
| 43 | 21년 전 | 4589 | ||
| 42 |
prosper
|
21년 전 | 2723 | |
| 41 |
prosper
|
21년 전 | 2126 | |
| 40 | 21년 전 | 2188 | ||
| 39 | 21년 전 | 2160 | ||
| 38 | 21년 전 | 2435 | ||
| 37 | 21년 전 | 2579 | ||
| 36 | 21년 전 | 1790 | ||
| 35 | 21년 전 | 4089 | ||
| 34 | 21년 전 | 3864 | ||
| 33 | 21년 전 | 3001 | ||
| 32 |
prosper
|
21년 전 | 2916 | |
| 31 | 21년 전 | 5303 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기