Day 3

TIL - Queue, Tree & Heap

๐Ÿ“‹ย ๊ณต๋ถ€ ๋‚ด์šฉ

Queues

  • Queue?

    • FIFO (First In First Out)
    • operations
      • enqueue
      • dequeue
        • ์„ ํ˜• ๋ฐฐ์—ด์œผ๋กœ ๊ตฌํ˜„: O(n) -> ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒƒ์ด ๋” ์ข‹์Œ
  • Circular Queues

    • ๋ฐฐ์—ด์˜ ํ•œ์ชฝ ๋๊ณผ ๋‹ค๋ฅธ์ชฝ ๋์ด ๋‹ฟ์•„ ์žˆ๋Š” ๋ชจ์Šต
    • ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ์žˆ์Œ
    • front / rear ํฌ์ธํ„ฐ๋ฅผ ๊ธฐ์–ตํ•˜๊ณ , dequeue๋œ ์›์†Œ ์ €์žฅ์†Œ๋Š” ์žฌํ™œ์šฉ
    • ์„ ํ˜• ๋ฐฐ์—ด์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒƒ์ด ๋” ์ข‹์Œ
  • Priority Queues

    • ์›์†Œ๋“ค ์‚ฌ์ด์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ฆ„
    • ์šฐ์„ ์ˆœ์œ„ ๊ตฌํ˜„
      • ์›์†Œ๋ฅผ ๋„ฃ์„ ๋•Œ enqueue vs. ์›์†Œ๋ฅผ ๊บผ๋‚ผ ๋•Œ dequeue
        • ๋„ฃ์„ ๋•Œ(enqueue) ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๋Š”๊ฒƒ์ด ์กฐ๊ธˆ ๋” ๋‚˜์€ Time Complexity๋ฅผ ๊ฐ€์ง
        • ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ํ™œ์šฉํ•˜๋Š” ๋ฐ์—๋„ ๋” ํŽธ๋ฆฌํ•จ

Trees

  • Tree?

    • ๊ตฌ์„ฑ
      • root node
      • interner nodes
      • leaf nodes
    • ํŠน์„ฑ
      • parent node / child node
      • ๋…ธ๋“œ์˜ ์ˆ˜์ค€ (level) : root node๋กœ๋ถ€ํ„ฐ ๊ฑฐ๋ฆฌ
      • ๋…ธ๋“œ์˜ ์ฐจ์ˆ˜ (degree) : ๋…ธ๋“œ์˜ ์ž์‹ ์ˆ˜
      • ํŠธ๋ฆฌ์˜ ๋†’์ด(height) ๋˜๋Š” ๊นŠ์ด(depth) : ์ œ์ผ ํฐ level + 1
      • subtree
  • Binary Trees

    • ๋ชจ๋“  node์˜ degree <= 2
    • operations
      • size()
      • depth()
      • ์ˆœํšŒ traversal
        • Depth First Traversal : ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ†ตํ•ด ๊ตฌํ˜„
          • inorder
          • preorder
          • postorder
        • Breadth Tirst Traversal : queue ์‚ฌ์šฉ!
          • level์ด ๋‚ฎ์€ ๋…ธ๋“œ๋ฅผ ์šฐ์„ ์œผ๋กœ ๋ฐฉ๋ฌธ
          • ๊ฐ™์€ level์ธ ๊ฒฝ์šฐ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฆ„
    • ํฌํ™” ์ด์ง„ ํŠธ๋ฆฌ (full binary trees)
      • ๋ชจ๋“  node์˜ degree == 2
    • ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ (complete binary trees)
      • (depth k) level k-2๊นŒ์ง€๋Š” full binary tree, level k-1์€ ์™ผ์ชฝ๋ถ€ํ„ฐ node ์ฑ„์›Œ์ง
  • Binary Search Trees

    • ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด ๋‹ค์Œ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•˜๋Š” Binary Tree
      • ์™ผ์ชฝ subtree’s data < ํ˜„์žฌ node’s data < ์˜ค๋ฅธ์ชฝ subtree’s data
    • ์žฅ๋‹จ์ 
      • ์žฅ์  : ์›์†Œ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ๊ฐ€ ํŽธํ•จ
      • ๋‹จ์  : ํฐ ๊ณต๊ฐ„์„ ์†Œ์š”ํ•จ (์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„)
    • operations
      • insert()
      • remove()
      • lookup()
      • inorder()
      • min(), max()

Heap

  • Heaps?
    • Binary Tree์˜ ํ•œ ์ข…๋ฅ˜ (binary heap)

    • Min / Max heap

      • root node๊ฐ€ ํ•ญ์ƒ ์ตœ์†Œ/์ตœ๋Œ€ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค
      • complete binary tree
        • n nodes -> depth log(n)+1
        • insert / remove operation์˜ Time Complexity : O(log(n))
      • subtree ๋˜ํ•œ Min / Max heap
    • Binary Seacrh Tree vs. Heap?

      BSTHeap
      ๋ฐ์ดํ„ฐ ์ •๋ ฌํฌ๊ธฐ์ˆœ์„œ๋Œ€๋กœ ์™„์ „ ์ •๋ ฌ์™„์ „ ์ •๋ ฌ X
      ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰OX
      ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌXO
      ์—ฐ์‚ฐ ์‹œ๊ฐ„(์ตœ์•…์˜ ๊ฒฝ์šฐ) O(n)O(log(n))
      ์„ ํ˜• ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„XO
    • operations

      • insert()
      • remove()

๐Ÿ‘€ย CHECK

(์–ด๋ ต๊ฑฐ๋‚˜ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๋“ฑ ๋‹ค์‹œ ํ™•์ธํ•  ๊ฒƒ๋“ค)
  • ๋…ธ๋“œ์˜ ์ฐจ์ˆ˜(degree) : # of children
    • binary tree : ๋ชจ๋“  ๋…ธ๋“œ์˜ degree๊ฐ€ ํ•ญ์ƒ 2์ดํ•˜
    • leaf nodes : degree 0
  • ๋‹จ์ถ• ํ‰๊ฐ€
    • ๋‹จ์ถ•ํ‰๊ฐ€์— ๋Œ€ํ•ด ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ
    • 2๊ฐœ ์ด์ƒ์˜ ๋…ผ๋ฆฌ ์กฐ๊ฑด์‹์ด ์žˆ์„ ๊ฒฝ์šฐ์—, ์•ž ์กฐ๊ฑด์ด ๊ณ„์‚ฐํ•œ ๊ฐ’์— ์˜ํ•ด ๊ฒฐ๊ณผ๊ฐ€ ํ™•์‹คํ•ด์ง€๋ฉด ๋‘๋ฒˆ์งธ ์กฐ๊ฑด์€ ํ™•์ธํ•˜์ง€ ์•Š์Œ
    • False and (), True or () ์ธ ๊ฒฝ์šฐ ๋“ฑ์ด ํ•ด๋‹น๋จ

โ—ย ๋Š๋‚€ ์ 

์–ด์ œ์˜ ๊ตํ›ˆ์ด ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ์ง€, ์˜ค๋Š˜์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณผ์ •์— ์‹ค์ˆ˜๊ฐ€ ์ ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋” ๋‚˜์€ ๋ฐฉ์‹์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•˜๋Š” ์ƒํ™ฉ์ด ๋งŽ์•„์กŒ๋‹ค.

์˜ค๋Š˜ ์ฃผ๋กœ ๊ณ ๋ฏผํ•œ ๋ถ€๋ถ„์€

1
2
3
4
5
์ฝ”๋“œ๋ฅผ ๋” ์ง๊ด€์ ์œผ๋กœ ์ ๊ณ ์‹ถ์Œ
- ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ•ฉ์น  ์ˆ˜ ์žˆ๋Š”๊ฐ€?

๊ฐ™์€ ์ฝ”๋“œ ๋‹ค๋ฅธ ํšจ์œจ?
- ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ ์‚ด์ง ๋‹ค๋ฅธ ๋‘ ์ฝ”๋“œ ์ค‘ ๋” ๋‚˜์€๊ฒƒ์€?

์ •๋„์˜€๋‹ค.

์ „์ž๋Š” ๋‚ด๊ฐ€ ํ‰์†Œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•  ๋•Œ ์ž์ฃผ ํ•˜๊ธด ํ•˜๋Š”๋ฐ, ํ•ญ์ƒ ๊ณ ๋ฏผํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋ฐ˜๋ณต๋˜๋Š” ๊ธฐ๋Šฅ์„ ํ•จ์ˆ˜๋กœ ๋นผ๋˜๊ฐ€, ์ ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฌถ์–ด์„œ ๋ฐ˜๋ณตํ•˜๋Š” ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ์“ฐ๋Š”๋ฐ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋ญ๊ฐ€ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค.

ํ›„์ž๋Š” Complexity๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์˜๋ฌธ์ด ์•„๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด operation์ด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜๊ณ  ์žˆ๋Š”์ง€, ์™œ ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•œ ๊ฒƒ์ด๋‹ค.
์‹ค์Šต ๋ฌธ์ œ์—์„œ ์šฐ์„ ์ˆœ์œ„ queue๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ, skeleton code์˜ dequeue ํ•จ์ˆ˜๋Š” ๋งˆ์ง€๋ง‰ elements๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ๋˜์–ด์žˆ์—ˆ๋‹ค.(getAt(data.size()))
๋‚˜๋Š” ์ฒ˜์Œ element๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ (getAt(1)) ์ด์— ํ•ด๋‹นํ•˜๋Š” enqueue ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž ๊น ๋ง‰ํ˜”๋‹ค๊ฐ€, ๊ฒฐ๊ตญ์€ ๋ˆˆ์น˜์ฑ„๊ณ  ํ•ด๊ฒฐํ–ˆ๋‹ค.
๊ทธ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๋” ๋‚˜์€๊ฑด์ง€ ๊ถ๊ธˆ์ฆ์ด ๋“ค์—ˆ๊ณ , ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ ๊ฒฐํ•จ์ด ์ ์€(?) ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ž๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

์˜ค๋Š˜์€ ์ถ”๊ฐ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋“ค์ด ์žˆ๊ณ  ์•„์ง ํ’€์ง€ ์•Š์•˜๋Š”๋ฐ, ์‰ฌ์šด ๋ฌธ์ œ๋“ค์ด์ง€๋งŒ ์ด ๋‘๊ฐ€์ง€๋ฅผ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ’€๋ ค๊ณ  ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ๋‹ค.

Hugo๋กœ ๋งŒ๋“ฆ
Jimmy์˜ Stack ํ…Œ๋งˆ ์‚ฌ์šฉ ์ค‘