๐ฒpriority queue ์๋ฃ๊ตฌ์กฐ ํฌ์คํ ์์ heap์ ์ผ์ข ์ CBT(Complete Binary Tree) ํํ๋ก ๊ตฌํ๋๋ค๊ณ ์ธ๊ธํ ์ ์ด ์๋ค.
priority queue - heap
1. concepts> โ๏ธ ์ฐ์ ์์ ํ = '์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฅ ๋จผ์ ์ญ์ ํ๋ ์๋ฃ๊ตฌ์กฐ' โ๏ธ ๋ฐ์ดํฐ๋ฅผ ์ฐ์ ์์์ ๋ฐ๋ผ ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋ ์ฌ์ฉ ex) ๋ฌผ๊ฑด ๋ฐ์ดํฐ๋ฅผ ์๋ฃ๊ตฌ์กฐ์ ๋ฃ์๋ค๊ฐ ๊ฐ
sh-avid-learner.tistory.com
๐ฒ ์ฌ๊ธฐ์ ๋ค์ํ ์ข ๋ฅ์ Binary Tree์ ๋ํด ๊ฐ๋จํ๊ฒ ์์๋ณด์.
๐ฒ Binary Tree๋ tree ์๋ฃ๊ตฌ์กฐ์ ์ผ์ข ์ผ๋ก, ๊ฐ node์ ์์ node๋ ์ต๋ 2๊ฐ ์กด์ฌํ๋ฉฐ, ์ด๋ฅผ left & right child๋ผ๊ณ ๋ถ๋ฅธ๋ค.
โ representation) binary tree์ ํํ ํํ๋ ๊ฐ์ฅ ์ ์ชฝ node์ pointer๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก, tree๊ฐ ๋น์ด์๋ค๋ ๊ฑด, ์ ์ชฝ node์ value๋ NULL์ด๋ผ๊ณ ํํํ ์ ์๋ค.
→ ๋ฐ๋ผ์ ๊ฐ node์ binary tree๋ ์ธ ๊ฐ์ง๋ก ๊ตฌ๋ถ๋๋ค. 1) ๋ฐ์ดํฐ + 2) left child๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ + 3) right child๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
โก binary tree ์ฐ์ฐ)
→ insertiing an element
→ removing an element
→ searching for an element
→ traversing the tree
→ tree์ ๋์ด ๊ตฌํ๊ธฐ
→ tree์ node level ๊ตฌํ๊ธฐ
→ ์ ์ฒด tree์ size ๊ตฌํ๊ธฐ
โป tree ๊ด๋ จํ ์์ธํ ๋ด์ฉ์ ์ถํ ํฌ์คํ ์ฐธ์กฐ! โป
๐ฒ Binary Tree๋ ํฌ๊ฒ 6๊ฐ์ง๋ก ๋๋ ์ ์๋ค(์ถ๊ฐ๋ก ๋ ๋๋ ์ ์์ง๋ง, ์ผ๋จ 6๊ฐ์ง๋ง ์์๋ณด์)
1. Full Binary Tree
๐ฒ Full Binary Tree๋ ์์ node๊ฐ 0๊ฐ์ด๊ฑฐ๋ 2๊ฐ์ธ Tree๋ฅผ ๋ปํ๋ค. proper binary tree๋ผ๊ณ ๋ ๋ถ๋ฅด๋ฉฐ, leaf node ์ ์ธ ๋ชจ๋ node๊ฐ 2๊ฐ์ ์์ node๋ฅผ ๊ฐ๊ณ ์๋ tree๋ผ๊ณ ๋ ์ ์ํ ์ ์๋ค.
๐ฒ internal node ๊ฐ์๊ฐ l์ด๊ณ , leaf node ๊ฐ์๊ฐ L์ด๋ผ๋ฉด, L = l + 1 ๊ณต์์ด ์ฑ๋ฆฝํ๋ค.
(Full Binary Tree์ ๊ตญํํ์ง ์๊ณ ๋ชจ๋ tree์ ์ ์ฉ๋๋ ๊ณต์์ L = l + 1 (l์ internal node์ ๊ฐ์))
2. Degenerate(pathological) Tree
๐ฒ๋ชจ๋ internal node๊ฐ ๋จ 1๊ฐ์ ์์ node๋ง ๊ฐ๊ณ ์๋ Tree๋ฅผ ๋ปํ๋ค. (์ผ์ชฝ์ด๋ ์ค๋ฅธ์ชฝ์ด๋ )
3. Skewed Binary Tree
๐ฒ์ 2๋ฒ์ degnerated tree์ ์ผ์ข ์ผ๋ก, ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก๋ง ์งํ๋๊ฑฐ๋, ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก๋ง ์งํ๋ tree๋ฅผ ๋ปํ๋ค.
4. Complete Binary Tree
๐ฒfull binary tree์ ํผ๋ํ ์ ์๋๋ฐ, complete binary tree๋ ๋งจ ๋ง์ง๋ง level ์ ์ธํ๊ณ ๋ชจ๋ level์ด ์ฑ์์ ธ ์๊ณ ๋ง์ง๋ง level์๋ ์ผ์ชฝ๋ถํฐ ์ต๋ํ element๊ฐ ์ฑ์์ง tree๋ฅผ ๋ปํ๋ค.
๐ฒheap์ด ํ์ฑํ๋ Complete Binary Tree
5. Perfect Binary Tree
๐ฒ๋ชจ๋ internal node๋ ๋ ์์ node๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ๋ชจ๋ leaf node๋ ๋์ผ level์ ์๋, ๋ง ๊ทธ๋๋ก perfectํ๊ฒ ์ฑ์์ง tree๋ฅผ ๋ปํ๋ค.
๐ฒ ๋ฐ๋ผ์ a perfect binary tree of height h๋ ์ด $2^{h+1} -1$๊ฐ์ node๋ฅผ ๊ฐ๊ณ ์์์ height๋ก๋ node ๊ฐ์๋ฅผ ์ฝ๊ฒ ์ ์ ์๋ค.
6. Balanced Binary Tree
๐ฒ์ฑ๋ฅ๋ฉด์ผ๋ก ์ข์ tree๋ก, search & insert & delete ๋ชจ๋ $O(log n)$์ ์๊ฐ ๋ณต์ก๋๊ฐ ๊ฑธ๋ฆฐ๋ค.
* ์ถ์ฒ1) https://www.geeksforgeeks.org/handshaking-lemma-and-interesting-tree-properties/
* ์ถ์ฒ2) https://www.geeksforgeeks.org/types-of-binary-tree/
'Computer Science > Data Structures' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ณ Binary Tree ๐ณ (1) | 2023.12.19 |
---|---|
๐ Map (0) | 2023.08.03 |
๐ฏPriority Queue(feat. Binary Heap) (0) | 2023.01.09 |
hash table / hashing (0) | 2022.11.06 |
๐stack & queue & deque (0) | 2022.09.23 |
๋๊ธ