<?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);
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7230 | 11년 전 | 3596 | ||
| 7229 | 11년 전 | 3717 | ||
| 7228 | 11년 전 | 3777 | ||
| 7227 | 11년 전 | 3902 | ||
| 7226 | 11년 전 | 2359 | ||
| 7225 | 11년 전 | 17433 | ||
| 7224 |
AngryDev
|
11년 전 | 1341 | |
| 7223 |
돌아온깡통
|
11년 전 | 1085 | |
| 7222 |
돌아온깡통
|
11년 전 | 1023 | |
| 7221 |
돌아온깡통
|
11년 전 | 863 | |
| 7220 |
돌아온깡통
|
11년 전 | 985 | |
| 7219 |
돌아온깡통
|
11년 전 | 806 | |
| 7218 |
돌아온깡통
|
11년 전 | 652 | |
| 7217 |
돌아온깡통
|
11년 전 | 1041 | |
| 7216 |
돌아온깡통
|
11년 전 | 778 | |
| 7215 |
돌아온깡통
|
11년 전 | 695 | |
| 7214 |
돌아온깡통
|
11년 전 | 1037 | |
| 7213 |
돌아온깡통
|
11년 전 | 905 | |
| 7212 |
돌아온깡통
|
11년 전 | 695 | |
| 7211 |
돌아온깡통
|
11년 전 | 853 | |
| 7210 |
돌아온깡통
|
11년 전 | 874 | |
| 7209 |
돌아온깡통
|
11년 전 | 881 | |
| 7208 |
돌아온깡통
|
11년 전 | 981 | |
| 7207 |
돌아온깡통
|
11년 전 | 619 | |
| 7206 |
돌아온깡통
|
11년 전 | 666 | |
| 7205 |
돌아온깡통
|
11년 전 | 852 | |
| 7204 |
돌아온깡통
|
11년 전 | 708 | |
| 7203 |
돌아온깡통
|
11년 전 | 735 | |
| 7202 |
돌아온깡통
|
11년 전 | 698 | |
| 7201 |
돌아온깡통
|
11년 전 | 633 | |
| 7200 |
돌아온깡통
|
11년 전 | 692 | |
| 7199 |
돌아온깡통
|
11년 전 | 1306 | |
| 7198 |
돌아온깡통
|
11년 전 | 639 | |
| 7197 |
돌아온깡통
|
11년 전 | 952 | |
| 7196 |
돌아온깡통
|
11년 전 | 841 | |
| 7195 |
돌아온깡통
|
11년 전 | 585 | |
| 7194 |
돌아온깡통
|
11년 전 | 614 | |
| 7193 |
돌아온깡통
|
11년 전 | 690 | |
| 7192 |
돌아온깡통
|
11년 전 | 705 | |
| 7191 |
joe031
|
11년 전 | 1243 | |
| 7190 | 11년 전 | 4123 | ||
| 7189 | 11년 전 | 1198 | ||
| 7188 |
잘살아보자
|
11년 전 | 924 | |
| 7187 | 11년 전 | 1402 | ||
| 7186 |
kiplayer
|
11년 전 | 7523 | |
| 7185 | 11년 전 | 1114 | ||
| 7184 |
잘살아보자
|
11년 전 | 2322 | |
| 7183 |
잘살아보자
|
11년 전 | 1137 | |
| 7182 |
잘살아보자
|
11년 전 | 1189 | |
| 7181 | 11년 전 | 1471 | ||
| 7180 |
하얀비요일
|
11년 전 | 972 | |
| 7179 |
잘살아보자
|
11년 전 | 973 | |
| 7178 | 11년 전 | 975 | ||
| 7177 | 11년 전 | 987 | ||
| 7176 | 11년 전 | 1649 | ||
| 7175 |
|
11년 전 | 1031 | |
| 7174 |
kiplayer
|
11년 전 | 1166 | |
| 7173 | 11년 전 | 953 | ||
| 7172 |
잘살아보자
|
11년 전 | 4631 | |
| 7171 |
잘살아보자
|
11년 전 | 683 | |
| 7170 | 11년 전 | 1060 | ||
| 7169 |
초심의설렘
|
11년 전 | 1461 | |
| 7168 | 11년 전 | 994 | ||
| 7167 |
잘살아보자
|
11년 전 | 5274 | |
| 7166 |
잘살아보자
|
11년 전 | 3401 | |
| 7165 | 11년 전 | 4945 | ||
| 7164 | 11년 전 | 820 | ||
| 7163 | 11년 전 | 1177 | ||
| 7162 |
울라라라우
|
11년 전 | 1359 | |
| 7161 | 11년 전 | 1240 | ||
| 7160 |
skyler
|
11년 전 | 1175 | |
| 7159 |
|
11년 전 | 668 | |
| 7158 |
|
11년 전 | 3329 | |
| 7157 |
잘살아보자
|
11년 전 | 2873 | |
| 7156 |
잘살아보자
|
11년 전 | 2214 | |
| 7155 |
잘살아보자
|
11년 전 | 1442 | |
| 7154 |
잘살아보자
|
11년 전 | 1443 | |
| 7153 | 11년 전 | 2869 | ||
| 7152 |
울라라라우
|
11년 전 | 817 | |
| 7151 | 11년 전 | 1040 | ||
| 7150 |
잘살아보자
|
11년 전 | 2376 | |
| 7149 |
잘살아보자
|
11년 전 | 3279 | |
| 7148 |
잘살아보자
|
11년 전 | 1177 | |
| 7147 |
잘살아보자
|
11년 전 | 749 | |
| 7146 |
잘살아보자
|
11년 전 | 1380 | |
| 7145 |
잘살아보자
|
11년 전 | 689 | |
| 7144 |
잘살아보자
|
11년 전 | 1272 | |
| 7143 |
잘살아보자
|
11년 전 | 737 | |
| 7142 |
잘살아보자
|
11년 전 | 1436 | |
| 7141 |
잘살아보자
|
11년 전 | 1188 | |
| 7140 |
잘살아보자
|
11년 전 | 1987 | |
| 7139 |
잘살아보자
|
11년 전 | 3652 | |
| 7138 |
잘살아보자
|
11년 전 | 3147 | |
| 7137 |
잘살아보자
|
11년 전 | 3619 | |
| 7136 |
잘살아보자
|
11년 전 | 1374 | |
| 7135 |
gooroo
|
11년 전 | 1604 | |
| 7134 |
열라뽕똬이
|
11년 전 | 2304 | |
| 7133 |
|
11년 전 | 1010 | |
| 7132 | 11년 전 | 1424 | ||
| 7131 | 11년 전 | 3564 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기