์ด์ง„ ํž™(Binary Heaps)

Doozuuยท2023๋…„ 3์›” 21์ผ
0

๐Ÿ“Œ Binary Heaps

: ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(BST)์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ทœ์น™์ด ์žˆ๋‹ค.

  • Max Binary Heap : ๋ถ€๋ชจ ๋…ธ๋“œ๋“ค์ด ํ•ญ์ƒ ์ž์‹ ๋…ธ๋“œ๋“ค ๋ณด๋‹ค ํฌ๋‹ค.
  • Min Binary Heap : ๋ถ€๋ชจ ๋…ธ๋“œ๋“ค์ด ํ•ญ์ƒ ์ž์‹ ๋…ธ๋“œ๋“ค ๋ณด๋‹ค ์ž‘๋‹ค.

์ฐธ๊ณ  | ํž™์€ ํŠธ๋ฆฌ์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค.



Max Binary Heap

  • ๊ฐ๊ฐ์˜ ๋ถ€๋ชจ๋Š” ์ตœ๋Œ€ 2๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ํ•ญ์ƒ ํฌ๋‹ค.
  • ์–ธ์ œ๋‚˜ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค.
  • ์™ผ์ชฝ ๋…ธ๋“œ๊ฐ€ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋ณด๋‹ค ๋จผ์ € ์ฑ„์›Œ์ ธ์•ผ ํ•œ๋‹ค.
  • ํ˜•์ œ ๋…ธ๋“œ ๊ฐ„์—๋Š” ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

Min Binary Heap

  • ๊ฐ๊ฐ์˜ ๋ถ€๋ชจ๋Š” ์ตœ๋Œ€ 2๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ํ•ญ์ƒ ์ž‘๋‹ค.
  • ์–ธ์ œ๋‚˜ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค.
  • ์™ผ์ชฝ ๋…ธ๋“œ๊ฐ€ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋ณด๋‹ค ๋จผ์ € ์ฑ„์›Œ์ ธ์•ผ ํ•œ๋‹ค.
  • ํ˜•์ œ ๋…ธ๋“œ ๊ฐ„์—๋Š” ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

โญ๏ธ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์™€ ์ด์ง„ ํž™ ๊ตฌ๋ณ„ํ•˜๊ธฐ

์•„๋ž˜๋Š” ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ๋กœ, ์ด์ง„ ํž™์ด ๋  ์ˆ˜ ์—†๋‹ค.
๋ถ€๋ชจ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ์ž์‹ ๋…ธ๋“œ๋“ค์ด ๋” ํฐ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— max binary heap์ด ๋  ์ˆ˜ ์—†๊ณ , ๋ถ€๋ชจ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ์ž์‹๋…ธ๋“œ๋“ค์ด ๋” ์ž‘์€ ๊ฒฝ์šฐ๋„ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— min binary heap๋„ ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.



ํ™œ์šฉ ๋ถ„์•ผ

  1. ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์ค‘ ํ•˜๋‚˜์ธ ์šฐ์„ ์ˆœ์œ„ ํ ๋ผ๋Š” ๊ฒƒ์„ ๋งŒ๋“ค๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.
  2. ํŠธ๋ฆฌ ์ˆœํšŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ข…์ข… ์“ฐ์ธ๋‹ค.



Max Binary Heap ๊ตฌํ˜„

๋ฐฐ์—ด/๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

1. insert( ) ๋ฉ”์†Œ๋“œ

์›๋ฆฌ : ๋งจ ๋์— ์ƒˆ๋กœ์šด ๊ฐ’์„ pushํ•˜๊ณ  ๋ถ€๋ชจ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ํด ๊ฒฝ์šฐ bubble up ํ•ด์„œ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พผ๋‹ค.

โญ๏ธ ๋ถ€๋ชจ์˜ ์œ„์น˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž์‹ ์ฐพ๊ธฐ

: ๋ถ€๋ชจ ์œ„์น˜๊ฐ€ n ์ผ ๋•Œ ์ž์‹์˜ ์œ„์น˜๋Š” 2n+1, 2n+2, ์ž์‹ ์œ„์น˜๊ฐ€ n ์ผ ๋•Œ๋Š” ๋ถ€๋ชจ ์œ„์น˜๊ฐ€ (n-1)/2 ์— ๋‚ด๋ฆผํ•œ ๊ฐ’

class MaxBinaryHeap {
    constructor(){
        this.values = [];
    }
    insert(element){
        this.values.push(element);
        this.bubbleUp();
    }
    bubbleUp(){
        let idx = this.values.length - 1;
        const element = this.values[idx];
        while(idx > 0){
            let parentIdx = Math.floor((idx - 1)/2);
            let parent = this.values[parentIdx];
            if(element <= parent) break;
            this.values[parentIdx] = element;
            this.values[idx] = parent;
            idx = parentIdx;
        }
    }
}

let heap = new MaxBinaryHeap();
heap.insert(41);
heap.insert(39);
heap.insert(33);
heap.insert(18);
heap.insert(27);
heap.insert(12);
heap.insert(55);

2. remove( ) ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ๋ช…์€ remove, extractMax ํ˜น์€ extractMaximum ๋“ฑ์œผ๋กœ ๋ถˆ๋ฆฐ๋‹ค.

  1. max binary heap ์—์„œ๋Š” ์ตœ๋Œ“๊ฐ’(root)์„ ์ œ๊ฑฐํ•ด ๋ฐ˜ํ™˜ํ•˜๊ณ , min binary heap์—์„œ๋Š” ์ตœ์†Ÿ๊ฐ’(root)์„ ์ œ๊ฑฐํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. ๋งˆ์ง€๋ง‰ ๊ฐ’์„ root์— ๋Œ€์‹  ๋„ฃ์–ด์ค€๋‹ค.
  3. bubble down(sink down ๋“ฑ ๋‹ค์–‘ํ•œ ์ด๋ฆ„์œผ๋กœ ๋ถˆ๋ฆผ) ํ•ด์ค€๋‹ค.(์ƒˆ๋กœ์šด root๋ฅผ ์ˆœ์„œ์— ๋งž๊ฒŒ ์ •๋ฆฌ)
class MaxBinaryHeap {
    constructor(){
        this.values = [];
    }
    insert(element){
        this.values.push(element);
        this.bubbleUp();
    }
    bubbleUp(){
        let idx = this.values.length - 1;
        const element = this.values[idx];
        while(idx > 0){
            let parentIdx = Math.floor((idx - 1)/2);
            let parent = this.values[parentIdx];
            if(element <= parent) break;
            this.values[parentIdx] = element;
            this.values[idx] = parent;
            idx = parentIdx;
        }
    }
  	extractMax(){
     	const max = this.values[0];
      	const end = this.values.pop();
      	if(this.values.length > 0){
         	this.values[0] = end;
          	this.sinkDown();
        }
      	return max;
    }
  	bubbleDown(){
     	let idx = 0;
      	const length = this.values.length;
        const element = this.values[0];
      	while(true){
         	let leftChildIdx = 2 * idx + 1;
          	let rightChildIdx = 2 * idx + 2;
          	let leftChild,rightChild;
          	let swap = null;
          	
          	if(leftChildIdx < length){
             	leftChild = this.values[leftChildIdx];
              	if(leftChild > element){
                 	swap = leftChildIdx; 
                }
            }
            if(rightChildIdx < length){
             	rightChild = this.values[rightChildIdx];
              	if(
                  	(swap === null && rightChild > element) || 
                  	(swap !== null && rightChild > leftChild)
                ){
                  	swap = rightChildIdx;
                }
            }
         	if(swap === null) break;
          	this.values[idx] = this.values[swap];
          	this.values[swap] = element;
          	idx = swap;
        }
    }
}

let heap = new MaxBinaryHeap();
heap.insert(41);
heap.insert(39);
heap.insert(33);
heap.insert(18);
heap.insert(27);
heap.insert(12);
heap.insert(55);



โญ๏ธ ์ด์ง„ ํž™์˜ Big O

์‚ฝ์ž…,์‚ญ์ œ : O(log n)

ํƒ์ƒ‰ : O(n)

์‚ฝ์ž…๊ณผ ์‚ญ์ œ์— ์žˆ์–ด ์„ฑ๋Šฅ์ด ๋งค์šฐ ์ข‹๋‹ค.
๊ฐ ๋ ˆ๋ฒจ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋น„๊ต๋ฅผ ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— log n ์ด๋‹ค.

ํƒ์ƒ‰์€ BST๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค. ํž™์€ ์ขŒ/์šฐ๋กœ ๋Œ€์†Œ ๊ด€๊ณ„์— ๋”ฐ๋ฅธ ์ •๋ ฌ์ด ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

profile
๋ชจ๋“ ๊ฒŒ ์ƒˆ๋กญ๊ณ  ์žฌ๋ฐŒ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ์ƒˆ์‹น

0๊ฐœ์˜ ๋Œ“๊ธ€