์ž๋ฃŒ๊ตฌ์กฐ) Tree - Binary Search Tree, Segment Tree, Binary Indexed Tree

Binary Search Tree (์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ) def ๋ชจ๋“  ์›์†Œ๋Š” ์„œ๋กœ๋‹ค๋ฅธ ์œ ์ผํ•œ ๊ฐ’. ์™ผ์ชฝ subtree์˜ ๊ฐ’์€ ๊ทธ root๋ณด๋‹ค ์ž‘๋‹ค. ์˜ค๋ฅธ์ชฝ subtree์˜ ๊ฐ’์€ ๊ทธ root๋ณด๋‹ค ํฌ๋‹ค. ์™ผ/์˜ค๋ฅธ์ชฝ subtree๋„ Binary Search Tree์ด๋‹ค. ํƒ์ƒ‰/์‚ฝ์ž…/์‚ญ์ œ ์ค‘ ์‚ญ์ œ์—ฐ์‚ฐ์ด ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•œ ํ›„์—๋„ BST๋ฅผ ์œ ์ง€ํ•ด์•ผํ•˜๋ฏ€๋กœ ํ›„์†์กฐ์น˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์‚ญ์ œํ•  ๋…ธ๋“œ๊ฐ€ 1. degree=0(leaf node)์ธ์ง€ 2. degree=1์ธ์ง€ 3. degree=2์ธ์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง. Segment Tree ๊ตฌ๊ฐ„์˜ ์ •๋ณด๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ตฌํ• ์ˆ˜์žˆ๋Š”, Complete Binary Tree ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ Example) ๊ตฌ๊ฐ„ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” Segment Tree ๊ตฌํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์„ ์™„์ „์ด์ง„ํŠธ๋ฆฌ์˜ ์ตœํ•˜๋‹จ์—..

Data Structure 2019. 9. 12. 19:20
์ž๋ฃŒ๊ตฌ์กฐ) Tree - Heap, Priority Queue (ํž™, ์šฐ์„ ์ˆœ์œ„ ํ)

Heap (ํž™) Max Heap / Min Heap Parent Node๊ฐ€ Child Node๋ณด๋‹ค ํ•ญ์ƒ ํฌ๊ฑฐ๋‚˜(์ž‘๊ฑฐ๋‚˜) ๊ฐ™์€ Complete Binary Tree (์™„์ „์ด์ง„ํŠธ๋ฆฌ) (์ž์‹๋…ธ๋“œ๋ผ๋ฆฌ๋Š” ์ƒ๊ด€์—†์Œ) (ํ•ญ์ƒ root๊ฐ€ max/min์ด๋ฏ€๋กœ ์ตœ๋Œ€-์ตœ์†Œ๊ฐ’์„ ์ฐพ๊ธฐ ํŽธํ•˜๋‹ค) ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(logn) (ํž™์˜ ์‚ฝ์ž…-์‚ญ์ œ๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ, ์ตœํ•˜๋‹จ๋…ธ๋“œ~์ตœ์ƒ๋‹จ๋…ธ๋“œ๊นŒ์ง€ swap์ด ํ•„์š”ํ•˜๋‹ค) ์‚ฝ์ž…&์‚ญ์ œ ์‹œ์—๋„ '์™„์ „์ด์ง„ํŠธ๋ฆฌ'ํ˜•ํƒœ๋ฅผ ์œ ์ง€ํ•ด์•ผํ•œ๋‹ค. ์‚ฝ์ž… : ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋‹ค์Œ์ž๋ฆฌ๋ฅผ ํ™•์žฅํ•˜๊ณ  ์ž„์‹œ๋กœ ์ €์žฅํ›„, ๋ถ€๋ชจ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด๊ฐ€๋ฉฐ swapํ•œ๋‹ค. ์‚ญ์ œ : ํ•ญ์ƒ root๋ฅผ ์‚ญ์ œํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ root์ž๋ฆฌ์— ์ž„์‹œ๋กœ ์ €์žฅํ›„ ์‚ญ์ œํ•˜๊ณ , root์— ์žˆ๋Š” ๊ฐ’์„ ์ž์‹๋“ค๊ณผ ๋น„๊ตํ•ด๊ฐ€๋ฉฐ ์ ๋‹นํ•œ ์ž๋ฆฌ๊นŒ์ง€ swapํ•œ๋‹ค...

Data Structure 2019. 9. 12. 19:16
์ž๋ฃŒ๊ตฌ์กฐ) Hash (ํ•ด์‹œ)

Hash (ํ•ด์‹œ) Input Output ์ฒซ ์ค„์— ๋ช…๋ น์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. (1 ≤ V ≤ 100,000) ๋‘๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ช…๋ น์–ด๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 1 s: ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด s๋ฅผ set์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ set์— ์กด์žฌํ•œ๋‹ค๋ฉด ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2 s: ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด s๋ฅผ set์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ฌด ๋™์ž‘๋„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 3 s: ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด s๊ฐ€ set์— ์žˆ๋‹ค๋ฉด 1์„, ์—†๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ช…๋ น์–ด์— ์•Œ๋งž์€ ๊ฒฐ๊ณผ๋ฅผ ํ•œ์ค„์”ฉ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. import java.util.*; public class Test { public static void main(String args[]) { Scanner sc = new Scanner(System...

Data Structure 2019. 9. 12. 19:02
์ž๋ฃŒ๊ตฌ์กฐ) Stack, Queue (์Šคํƒ, ํ)

Stack (์Šคํƒ) LIFO (Last In First Out) push, pop(๋ฐ˜ํ™˜,์ œ๊ฑฐ), peek(๋ฐ˜ํ™˜), search ... ๋ฐฐ์—ด or ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๊ตฌํ˜„ > ์ž๋ฐ”๋Š” ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์Šคํƒ ์ œ๊ณต ์ด์šฉ) ์—ญ์ˆœ ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ, ์‹œ์Šคํ…œ ์Šคํƒ(ํ•จ์ˆ˜ ํ˜ธ์ถœ,๋ณต๊ท€ ๊ด€๋ฆฌ), ์ˆ˜์‹์˜ ๊ด„ํ˜ธ๊ฒ€์‚ฌ, ์ˆ˜์‹์˜ ํ›„์œ„ํ‘œ๊ธฐ๋ฒ• ๋“ฑ .. InputOutput์ฒซ ์ค„์— ๋ช…๋ น์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. (1 ≤ N ≤ 1,000,000)๋‘๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.push x : x๋ฅผ ์Šคํƒ์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.pop : ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋“ค์–ด์˜จ ์ธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.size : ํ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.top : ํ์˜ ๋งจ ์•ž ์ธ์ž ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.๊ฐ ๋ช…๋ น ์ˆœ์„œ์— ๋”ฐ๋ผ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. import java.util..

Data Structure 2019. 9. 12. 19:00
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ