<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2288 | |
| 329 |
prosper
|
20년 전 | 1919 | |
| 328 |
prosper
|
20년 전 | 1711 | |
| 327 | 20년 전 | 3778 | ||
| 326 | 20년 전 | 4865 | ||
| 325 |
hwatta
|
20년 전 | 2493 | |
| 324 |
|
20년 전 | 3150 | |
| 323 | 20년 전 | 5875 | ||
| 322 |
hwatta
|
20년 전 | 2860 | |
| 321 |
hwatta
|
20년 전 | 2332 | |
| 320 |
yesmoa
|
20년 전 | 4583 | |
| 319 | 20년 전 | 2696 | ||
| 318 | 20년 전 | 2244 | ||
| 317 |
kyodon
|
20년 전 | 2765 | |
| 316 | 20년 전 | 2588 | ||
| 315 |
|
20년 전 | 2890 | |
| 314 |
|
20년 전 | 3348 | |
| 313 |
|
20년 전 | 2654 | |
| 312 |
yesmoa
|
20년 전 | 4734 | |
| 311 | 20년 전 | 3288 | ||
| 310 |
홀로남은자
|
20년 전 | 4579 | |
| 309 | 20년 전 | 3014 | ||
| 308 | 20년 전 | 4145 | ||
| 307 | 20년 전 | 4382 | ||
| 306 | 20년 전 | 7044 | ||
| 305 | 20년 전 | 3877 | ||
| 304 | 20년 전 | 2776 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4416 | |
| 302 | 20년 전 | 2217 | ||
| 301 |
|
20년 전 | 4334 | |
| 300 | 20년 전 | 3820 | ||
| 299 | 20년 전 | 2637 | ||
| 298 | 20년 전 | 4849 | ||
| 297 |
|
20년 전 | 2538 | |
| 296 | 20년 전 | 4545 | ||
| 295 | 20년 전 | 3577 | ||
| 294 | 20년 전 | 3598 | ||
| 293 | 20년 전 | 3827 | ||
| 292 | 20년 전 | 3222 | ||
| 291 |
yesmoa
|
20년 전 | 5962 | |
| 290 | 20년 전 | 2949 | ||
| 289 | 20년 전 | 5870 | ||
| 288 |
|
20년 전 | 2387 | |
| 287 |
|
20년 전 | 1819 | |
| 286 |
|
20년 전 | 2158 | |
| 285 |
|
20년 전 | 3551 | |
| 284 |
|
20년 전 | 2045 | |
| 283 |
|
20년 전 | 4406 | |
| 282 | 20년 전 | 3387 | ||
| 281 |
|
20년 전 | 2218 | |
| 280 |
|
20년 전 | 7822 | |
| 279 | 20년 전 | 5569 | ||
| 278 | 20년 전 | 3005 | ||
| 277 |
|
20년 전 | 5577 | |
| 276 | 20년 전 | 2368 | ||
| 275 | 20년 전 | 2625 | ||
| 274 | 20년 전 | 2405 | ||
| 273 | 20년 전 | 2241 | ||
| 272 | 20년 전 | 2169 | ||
| 271 | 20년 전 | 2636 | ||
| 270 | 20년 전 | 2676 | ||
| 269 | 20년 전 | 2518 | ||
| 268 | 20년 전 | 2704 | ||
| 267 | 20년 전 | 2386 | ||
| 266 | 20년 전 | 2576 | ||
| 265 | 20년 전 | 3519 | ||
| 264 |
|
20년 전 | 5382 | |
| 263 |
|
20년 전 | 3747 | |
| 262 | 20년 전 | 3204 | ||
| 261 |
허저비
|
20년 전 | 5948 | |
| 260 |
|
20년 전 | 5724 | |
| 259 | 20년 전 | 4151 | ||
| 258 | 20년 전 | 2385 | ||
| 257 | 20년 전 | 3207 | ||
| 256 | 20년 전 | 1920 | ||
| 255 | 20년 전 | 1590 | ||
| 254 | 20년 전 | 3160 | ||
| 253 | 20년 전 | 3555 | ||
| 252 | 20년 전 | 5140 | ||
| 251 | 20년 전 | 5831 | ||
| 250 | 20년 전 | 3689 | ||
| 249 | 20년 전 | 5036 | ||
| 248 | 20년 전 | 3305 | ||
| 247 | 20년 전 | 3659 | ||
| 246 |
|
20년 전 | 7968 | |
| 245 |
|
20년 전 | 5930 | |
| 244 | 20년 전 | 4498 | ||
| 243 |
|
20년 전 | 4078 | |
| 242 | 20년 전 | 2809 | ||
| 241 | 20년 전 | 2755 | ||
| 240 | 20년 전 | 2390 | ||
| 239 | 20년 전 | 1681 | ||
| 238 |
아우겐나이스
|
20년 전 | 2288 | |
| 237 |
email
|
20년 전 | 3702 | |
| 236 | 20년 전 | 4186 | ||
| 235 | 20년 전 | 10477 | ||
| 234 | 20년 전 | 5086 | ||
| 233 | 20년 전 | 3392 | ||
| 232 | 20년 전 | 3231 | ||
| 231 | 20년 전 | 3874 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기