<?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년 전 | 484 | ||
| 7929 | 9년 전 | 419 | ||
| 7928 | 9년 전 | 491 | ||
| 7927 | 9년 전 | 401 | ||
| 7926 | 9년 전 | 710 | ||
| 7925 | 9년 전 | 434 | ||
| 7924 | 9년 전 | 416 | ||
| 7923 | 9년 전 | 408 | ||
| 7922 | 9년 전 | 428 | ||
| 7921 | 9년 전 | 445 | ||
| 7920 | 9년 전 | 353 | ||
| 7919 | 9년 전 | 365 | ||
| 7918 | 9년 전 | 514 | ||
| 7917 | 9년 전 | 372 | ||
| 7916 | 9년 전 | 460 | ||
| 7915 | 9년 전 | 475 | ||
| 7914 | 9년 전 | 481 | ||
| 7913 | 9년 전 | 657 | ||
| 7912 | 9년 전 | 495 | ||
| 7911 | 9년 전 | 417 | ||
| 7910 | 9년 전 | 466 | ||
| 7909 | 9년 전 | 582 | ||
| 7908 | 9년 전 | 514 | ||
| 7907 | 9년 전 | 455 | ||
| 7906 | 9년 전 | 476 | ||
| 7905 | 9년 전 | 439 | ||
| 7904 | 9년 전 | 427 | ||
| 7903 | 9년 전 | 436 | ||
| 7902 | 9년 전 | 614 | ||
| 7901 |
|
9년 전 | 782 | |
| 7900 | 9년 전 | 671 | ||
| 7899 | 9년 전 | 459 | ||
| 7898 | 9년 전 | 454 | ||
| 7897 | 9년 전 | 411 | ||
| 7896 | 9년 전 | 428 | ||
| 7895 | 9년 전 | 548 | ||
| 7894 | 9년 전 | 455 | ||
| 7893 | 9년 전 | 429 | ||
| 7892 | 9년 전 | 468 | ||
| 7891 | 9년 전 | 824 | ||
| 7890 | 9년 전 | 1248 | ||
| 7889 | 9년 전 | 788 | ||
| 7888 |
limsy1987
|
9년 전 | 601 | |
| 7887 | 9년 전 | 646 | ||
| 7886 | 9년 전 | 532 | ||
| 7885 | 9년 전 | 501 | ||
| 7884 | 9년 전 | 494 | ||
| 7883 | 10년 전 | 499 | ||
| 7882 | 10년 전 | 556 | ||
| 7881 | 10년 전 | 534 | ||
| 7880 | 10년 전 | 653 | ||
| 7879 | 10년 전 | 542 | ||
| 7878 | 10년 전 | 1305 | ||
| 7877 | 10년 전 | 834 | ||
| 7876 | 10년 전 | 572 | ||
| 7875 | 10년 전 | 644 | ||
| 7874 |
|
10년 전 | 849 | |
| 7873 | 10년 전 | 572 | ||
| 7872 | 10년 전 | 734 | ||
| 7871 | 10년 전 | 549 | ||
| 7870 | 10년 전 | 669 | ||
| 7869 | 10년 전 | 487 | ||
| 7868 | 10년 전 | 528 | ||
| 7867 | 10년 전 | 533 | ||
| 7866 | 10년 전 | 589 | ||
| 7865 | 10년 전 | 533 | ||
| 7864 | 10년 전 | 587 | ||
| 7863 | 10년 전 | 585 | ||
| 7862 | 10년 전 | 556 | ||
| 7861 | 10년 전 | 727 | ||
| 7860 | 10년 전 | 704 | ||
| 7859 | 10년 전 | 471 | ||
| 7858 | 10년 전 | 779 | ||
| 7857 | 10년 전 | 1167 | ||
| 7856 | 10년 전 | 592 | ||
| 7855 | 10년 전 | 835 | ||
| 7854 | 10년 전 | 773 | ||
| 7853 | 10년 전 | 673 | ||
| 7852 | 10년 전 | 588 | ||
| 7851 | 10년 전 | 590 | ||
| 7850 | 10년 전 | 667 | ||
| 7849 | 10년 전 | 441 | ||
| 7848 | 10년 전 | 505 | ||
| 7847 | 10년 전 | 740 | ||
| 7846 | 10년 전 | 524 | ||
| 7845 | 10년 전 | 499 | ||
| 7844 | 10년 전 | 466 | ||
| 7843 | 10년 전 | 504 | ||
| 7842 | 10년 전 | 483 | ||
| 7841 | 10년 전 | 451 | ||
| 7840 | 10년 전 | 476 | ||
| 7839 | 10년 전 | 518 | ||
| 7838 | 10년 전 | 587 | ||
| 7837 | 10년 전 | 416 | ||
| 7836 | 10년 전 | 457 | ||
| 7835 | 10년 전 | 546 | ||
| 7834 |
|
10년 전 | 1244 | |
| 7833 | 10년 전 | 502 | ||
| 7832 | 10년 전 | 490 | ||
| 7831 | 10년 전 | 660 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기