๋ชฉ๋กheap (1)

YS's develop story

Heap (ํž™) ์ •๋ฆฌ

๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป Heap (ํž™) ์ •๋ฆฌ With Python ๐Ÿฅ Heap ์ด๋ž€? ๋ฐ์ดํ„ฐ์—์„œ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์™„์ „ ์ด์ง„ํŠธ๋ฆฌ(Complete Binary Tree)์ž…๋‹ˆ๋‹ค. ์™„์ „ ์ด์ง„ํŠธ๋ฆฌ? Node๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ์ตœํ•˜๋‹จ ์™ผ์ชฝ Node๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•˜๋Š” Tree ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ , ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์œผ๋ ค๋ฉด O(n) ์ด ๊ฑธ๋ฆฌ์ง€๋งŒ ํž™์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ , ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์œผ๋ฉด O(logn) ์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์•„์•ผ ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค. ํž™์€ ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋…ธ๋“œ์˜ ๊ฐ’์€ ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์™„์ „ ์ด์ง„ํŠธ๋ฆฌ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๐Ÿ‹ Heap VS Binary Search Tree ๊ณตํ†ต์ ..

Data Structure 2021. 7. 28. 09:37