<?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);
?>
게시글 목록
| 번호 | 제목 |
|---|---|
| 12333 |
JavaScript
한글만 되게 하는 자바스크립트 입니다.
|
| 12332 |
JavaScript
간단한 팝업창 중앙에 띄우기 입니다.
|
| 12331 |
JavaScript
2단계 드롭다운 레이어 메뉴 입니다.
|
| 12329 |
JavaScript
동적 막대그래프
1
|
| 12328 |
JavaScript
간단한 위로가기 자바스크립트 입니다.
|
| 12327 |
JavaScript
따라다니는 link 퀵메뉴 입니다.
|
| 12326 |
JavaScript
글씨의 색을 자동으로 변환시켜주는 자바스크립트 입니다.
|
| 12325 |
JavaScript
인스타그램 사진가져오기
|
| 12324 |
jQuery
checkedbox 컨트롤
|
| 12323 |
jQuery
radio 컨트롤
|
| 12322 |
jQuery
select box 컨트롤
|
| 12321 | |
| 12320 |
node.js
Node.js - 실시간 위치 추적 (2)
|
| 12319 | |
| 12318 |
JavaScript
특정 이메일 입력 금지및 이메일 유효성 검사하기 입니다.
|
| 12317 |
JavaScript
마우스 오버시 그림 변하게 하는 자바스크립트 입니다
|
| 12316 |
JavaScript
시작과 멈춤이 가능한 스크롤 텍스트 입니다.
|
| 12315 |
JavaScript
이미지 없이 라운딩 박스 + 보더 표현하기 입니다.
|
| 12314 |
JavaScript
Trim 함수 자바스크립트로 사용하기 입니다.
|
| 12313 |
JavaScript
실제 이미지 크기 알아내기 입니다.
|
| 12312 |
JavaScript
present 자바스크립트 입니다.
|
| 12311 |
JavaScript
이미지 미리 로딩시켜놓기 입니다.
|
| 12310 |
JavaScript
다각형 이미지슬라이드쇼 자바스크립트 입니다.
|
| 12309 |
JavaScript
가운데 정렬된 홈페이지에서 따라다니는 레이어 자바스크립트 입니다.
|
| 12308 |
JavaScript
클릭시 펼쳐지는 메뉴 입니다.
|
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기