<?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년 전 | 404 | ||
| 7929 | 9년 전 | 322 | ||
| 7928 | 9년 전 | 413 | ||
| 7927 | 9년 전 | 329 | ||
| 7926 | 9년 전 | 661 | ||
| 7925 | 9년 전 | 346 | ||
| 7924 | 9년 전 | 333 | ||
| 7923 | 9년 전 | 337 | ||
| 7922 | 9년 전 | 372 | ||
| 7921 | 9년 전 | 395 | ||
| 7920 | 9년 전 | 318 | ||
| 7919 | 9년 전 | 330 | ||
| 7918 | 9년 전 | 487 | ||
| 7917 | 9년 전 | 324 | ||
| 7916 | 9년 전 | 400 | ||
| 7915 | 9년 전 | 400 | ||
| 7914 | 9년 전 | 409 | ||
| 7913 | 9년 전 | 567 | ||
| 7912 | 9년 전 | 413 | ||
| 7911 | 9년 전 | 361 | ||
| 7910 | 9년 전 | 402 | ||
| 7909 | 9년 전 | 502 | ||
| 7908 | 9년 전 | 419 | ||
| 7907 | 9년 전 | 364 | ||
| 7906 | 9년 전 | 385 | ||
| 7905 | 9년 전 | 361 | ||
| 7904 | 9년 전 | 350 | ||
| 7903 | 9년 전 | 343 | ||
| 7902 | 9년 전 | 557 | ||
| 7901 |
|
9년 전 | 727 | |
| 7900 | 9년 전 | 579 | ||
| 7899 | 9년 전 | 373 | ||
| 7898 | 9년 전 | 379 | ||
| 7897 | 9년 전 | 333 | ||
| 7896 | 9년 전 | 354 | ||
| 7895 | 9년 전 | 462 | ||
| 7894 | 9년 전 | 384 | ||
| 7893 | 9년 전 | 328 | ||
| 7892 | 9년 전 | 381 | ||
| 7891 | 9년 전 | 756 | ||
| 7890 | 9년 전 | 1188 | ||
| 7889 | 9년 전 | 742 | ||
| 7888 |
limsy1987
|
9년 전 | 543 | |
| 7887 | 9년 전 | 541 | ||
| 7886 | 9년 전 | 426 | ||
| 7885 | 9년 전 | 397 | ||
| 7884 | 9년 전 | 402 | ||
| 7883 | 9년 전 | 396 | ||
| 7882 | 9년 전 | 442 | ||
| 7881 | 9년 전 | 432 | ||
| 7880 | 9년 전 | 553 | ||
| 7879 | 9년 전 | 456 | ||
| 7878 | 9년 전 | 1210 | ||
| 7877 | 9년 전 | 746 | ||
| 7876 | 9년 전 | 476 | ||
| 7875 | 9년 전 | 553 | ||
| 7874 |
|
9년 전 | 801 | |
| 7873 | 9년 전 | 514 | ||
| 7872 | 9년 전 | 668 | ||
| 7871 | 9년 전 | 476 | ||
| 7870 | 9년 전 | 599 | ||
| 7869 | 9년 전 | 430 | ||
| 7868 | 9년 전 | 436 | ||
| 7867 | 9년 전 | 417 | ||
| 7866 | 9년 전 | 480 | ||
| 7865 | 9년 전 | 445 | ||
| 7864 | 9년 전 | 507 | ||
| 7863 | 9년 전 | 506 | ||
| 7862 | 9년 전 | 461 | ||
| 7861 | 9년 전 | 624 | ||
| 7860 | 9년 전 | 615 | ||
| 7859 | 9년 전 | 406 | ||
| 7858 | 9년 전 | 698 | ||
| 7857 | 9년 전 | 1069 | ||
| 7856 | 9년 전 | 513 | ||
| 7855 | 9년 전 | 744 | ||
| 7854 | 9년 전 | 726 | ||
| 7853 | 9년 전 | 575 | ||
| 7852 | 9년 전 | 503 | ||
| 7851 | 9년 전 | 499 | ||
| 7850 | 9년 전 | 579 | ||
| 7849 | 9년 전 | 354 | ||
| 7848 | 9년 전 | 407 | ||
| 7847 | 9년 전 | 646 | ||
| 7846 | 9년 전 | 450 | ||
| 7845 | 9년 전 | 408 | ||
| 7844 | 9년 전 | 387 | ||
| 7843 | 9년 전 | 408 | ||
| 7842 | 9년 전 | 393 | ||
| 7841 | 9년 전 | 378 | ||
| 7840 | 9년 전 | 400 | ||
| 7839 | 9년 전 | 430 | ||
| 7838 | 9년 전 | 514 | ||
| 7837 | 9년 전 | 350 | ||
| 7836 | 9년 전 | 394 | ||
| 7835 | 9년 전 | 468 | ||
| 7834 |
|
9년 전 | 1187 | |
| 7833 | 9년 전 | 419 | ||
| 7832 | 9년 전 | 408 | ||
| 7831 | 9년 전 | 555 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기