<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 378 | ||
| 7929 | 9년 전 | 273 | ||
| 7928 | 9년 전 | 380 | ||
| 7927 | 9년 전 | 301 | ||
| 7926 | 9년 전 | 649 | ||
| 7925 | 9년 전 | 323 | ||
| 7924 | 9년 전 | 294 | ||
| 7923 | 9년 전 | 325 | ||
| 7922 | 9년 전 | 349 | ||
| 7921 | 9년 전 | 379 | ||
| 7920 | 9년 전 | 301 | ||
| 7919 | 9년 전 | 317 | ||
| 7918 | 9년 전 | 478 | ||
| 7917 | 9년 전 | 319 | ||
| 7916 | 9년 전 | 386 | ||
| 7915 | 9년 전 | 375 | ||
| 7914 | 9년 전 | 386 | ||
| 7913 | 9년 전 | 512 | ||
| 7912 | 9년 전 | 387 | ||
| 7911 | 9년 전 | 335 | ||
| 7910 | 9년 전 | 372 | ||
| 7909 | 9년 전 | 476 | ||
| 7908 | 9년 전 | 377 | ||
| 7907 | 9년 전 | 332 | ||
| 7906 | 9년 전 | 344 | ||
| 7905 | 9년 전 | 336 | ||
| 7904 | 9년 전 | 309 | ||
| 7903 | 9년 전 | 310 | ||
| 7902 | 9년 전 | 523 | ||
| 7901 |
|
9년 전 | 720 | |
| 7900 | 9년 전 | 548 | ||
| 7899 | 9년 전 | 352 | ||
| 7898 | 9년 전 | 344 | ||
| 7897 | 9년 전 | 302 | ||
| 7896 | 9년 전 | 329 | ||
| 7895 | 9년 전 | 432 | ||
| 7894 | 9년 전 | 355 | ||
| 7893 | 9년 전 | 294 | ||
| 7892 | 9년 전 | 340 | ||
| 7891 | 9년 전 | 728 | ||
| 7890 | 9년 전 | 1180 | ||
| 7889 | 9년 전 | 738 | ||
| 7888 |
limsy1987
|
9년 전 | 531 | |
| 7887 | 9년 전 | 511 | ||
| 7886 | 9년 전 | 399 | ||
| 7885 | 9년 전 | 370 | ||
| 7884 | 9년 전 | 379 | ||
| 7883 | 9년 전 | 366 | ||
| 7882 | 9년 전 | 384 | ||
| 7881 | 9년 전 | 407 | ||
| 7880 | 9년 전 | 529 | ||
| 7879 | 9년 전 | 426 | ||
| 7878 | 9년 전 | 1169 | ||
| 7877 | 9년 전 | 717 | ||
| 7876 | 9년 전 | 446 | ||
| 7875 | 9년 전 | 519 | ||
| 7874 |
|
9년 전 | 786 | |
| 7873 | 9년 전 | 486 | ||
| 7872 | 9년 전 | 626 | ||
| 7871 | 9년 전 | 453 | ||
| 7870 | 9년 전 | 579 | ||
| 7869 | 9년 전 | 396 | ||
| 7868 | 9년 전 | 392 | ||
| 7867 | 9년 전 | 390 | ||
| 7866 | 9년 전 | 453 | ||
| 7865 | 9년 전 | 410 | ||
| 7864 | 9년 전 | 474 | ||
| 7863 | 9년 전 | 462 | ||
| 7862 | 9년 전 | 441 | ||
| 7861 | 9년 전 | 599 | ||
| 7860 | 9년 전 | 598 | ||
| 7859 | 9년 전 | 375 | ||
| 7858 | 9년 전 | 678 | ||
| 7857 | 9년 전 | 1032 | ||
| 7856 | 9년 전 | 493 | ||
| 7855 | 9년 전 | 711 | ||
| 7854 | 9년 전 | 703 | ||
| 7853 | 9년 전 | 550 | ||
| 7852 | 9년 전 | 475 | ||
| 7851 | 9년 전 | 456 | ||
| 7850 | 9년 전 | 554 | ||
| 7849 | 9년 전 | 325 | ||
| 7848 | 9년 전 | 378 | ||
| 7847 | 9년 전 | 598 | ||
| 7846 | 9년 전 | 419 | ||
| 7845 | 9년 전 | 377 | ||
| 7844 | 9년 전 | 370 | ||
| 7843 | 9년 전 | 383 | ||
| 7842 | 9년 전 | 372 | ||
| 7841 | 9년 전 | 358 | ||
| 7840 | 9년 전 | 367 | ||
| 7839 | 9년 전 | 404 | ||
| 7838 | 9년 전 | 486 | ||
| 7837 | 9년 전 | 331 | ||
| 7836 | 9년 전 | 367 | ||
| 7835 | 9년 전 | 448 | ||
| 7834 |
|
9년 전 | 1176 | |
| 7833 | 9년 전 | 384 | ||
| 7832 | 9년 전 | 387 | ||
| 7831 | 9년 전 | 518 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기