heap 2

[Data Structure] ํž™ (Heap)์˜ ๊ตฌํ˜„ - ํž™๊ณผ ๋ฐฐ์—ด

ํž™๊ณผ ๋ฐฐ์—ด ์ผ๋ฐ˜์ ์œผ๋กœ ํž™ ๊ตฌํ˜„ ์‹œ, ๋ฐฐ์—ด ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, ํž™ ๊ตฌํ˜„์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด root ๋…ธ๋“œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ 1๋กœ ์ง€์ • ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) = ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(child node's index) //2 ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(left child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 ์˜ค๋ฅธ์ชฝ ์ž์‹  ๋…ธ๋“œ ์ธ๋ฑ์Šค(right child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 + 1 ํž™๊ณผ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…(Max Heap) class Heap: def __init__(self, data): self.heap_array = list() self.h..

[Data Structure] ํž™ (Heap)

ํž™ (Heap) ? ๋ฐ์ดํ„ฐ์—์„œ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ(Complete Binary Tree) - ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ : ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ, ์ตœํ•˜๋‹จ ์™ผ์ชฝ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•˜๋Š” ํŠธ๋ฆฌ ํž™์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ , ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์œผ๋ ค๋ฉด O(n)์ด ๊ฑธ๋ฆผ