JUST DO IT!

[TIL] KDT_20230412 ๋ณธ๋ฌธ

TIL

[TIL] KDT_20230412

sunhokimDev 2023. 4. 12. 13:40

๐Ÿ“š KDT WEEK 2 DAY 3 TIL

  1. ํ(Queue) 
  2. ์ด์ง„ ํŠธ๋ฆฌ(Binary Trees)
  3. ํž™(Heaps)

 


 

ํ(Queue)์˜ ํŠน์ง•

  • ์„ ์ž…์„ ์ถœ(FIFO)์˜ ์ž๋ฃŒ ๊ตฌ์กฐ "ํŒŒ์ดํฌ" ๋ผ๊ณ ๋„ ๋ถ€๋ฆ„
  • dequeue ์—ฐ์‚ฐ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ ํšจ์œจ์„ ์œ„ํ•ด ์„ ํ˜• ๋ฐฐ์—ด๋ณด๋‹ค ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ์œ ๋ฆฌ

 

ํ™˜ํ˜• ํ(Circular Queues)์˜ ํŠน์ง•

  • ํ์˜ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๋ฅผ ์ œํ•œํ•ด๋‘๊ณ , ํ•œ์ชฝ ๋๊ณผ ๋‹ค๋ฅธ ์ชฝ ๋์ด ๋งž๋‹ฟ์€ ํ˜•ํƒœ
  • ์„ ํ˜• ๋ฐฐ์—ด๋กœ๋„ ํšจ๊ณผ์ ์œผ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

Circular Queue, Front์™€ Rear์˜ ์œ„์น˜๋Š” ๊ตฌํ˜„์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์„ ์ˆœ์œ„ ํ(Priority Queues)์˜ ํŠน์ง•

  • enqueue ์—ฐ์‚ฐ์— ์šฐ์„ ์ˆœ์œ„ ์ˆœ์„œ๋Œ€๋กœ ํ์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…
    • dequue ์—ฐ์‚ฐ์—์„œ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ฒƒ์„ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์ด ๊ฒฝ์šฐ ๋งค ๋ฒˆ O(n) ์†Œ์š”
  • ์šด์˜์ฒด์ œ์—์„œ CPU ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ํ™œ์šฉ๋จ

 


 

์ด์ง„ ํŠธ๋ฆฌ(Binary Trees)์˜ ํŠน์ง•

  • ํŠธ๋ฆฌ์— ํฌํ•จ๋˜๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ฐจ์ˆ˜๊ฐ€ 2 ์ดํ•˜์ธ ํŠธ๋ฆฌ
  • ์ •์˜ ์ž์ฒด๊ฐ€ ์žฌ๊ท€์ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์—ฐ์‚ฐ๋“ค๋„ ๋Œ€๋ถ€๋ถ„ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

์ด์ง„ ํŠธ๋ฆฌ์˜ ๊นŠ์ด ์šฐ์„  ์ˆœํšŒ

  • ์ค‘์œ„ ์ˆœํšŒ(in-order traversal) : ์™ผ์ชฝ, ์ž์‹ , ์˜ค๋ฅธ์ชฝ ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰
  • ์ „์œ„ ์ˆœํšŒ(pre-order traversal) : ์ž์‹ ์„ ๋จผ์ € ๋ฐฉ๋ฌธ ํ›„ ์™ผ์ชฝ ํƒ์ƒ‰, ์˜ค๋ฅธ์ชฝ ํƒ์ƒ‰
  • ํ›„์œ„ ์ˆœํšŒ(post-order traversal) : ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์ž์‹  ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰

์ฐจ๋ก€๋กœ ์ „์œ„, ์ค‘์œ„, ํ›„์œ„ ์ˆœํšŒ์˜ ๊ฒฝ์šฐ์ด๋‹ค.

์˜ค๋Š˜ ๊ฐ•์ขŒ๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ์ฝ”๋“œ ์˜ˆ์‹œ..

๋”๋ณด๊ธฐ

์ „์œ„ ์ˆœํšŒ์˜ ๊ฒฝ์šฐ

    def preorder(self):
        traversal = []
        traversal.append(self.data)
        if self.left:
            traversal += self.left.preorder()
        if self.right:
            traversal += self.right.preorder()
        return traversal

 

์ค‘์œ„, ํ›„์œ„ ์ˆœํšŒ์˜ ๊ฒฝ์šฐ ์œ„ ์ฝ”๋“œ์—์„œ ์•Œ๋งž๊ฒŒ ์œ„์น˜๋งŒ ๋ฐ”๊ฟ”์ค˜๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๊นŠ์ด ์šฐ์„  ์ˆœํšŒ์˜ ๊ฒฝ์šฐ ๊ตฌํ˜„์ด ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š์ง€๋งŒ.. ๋„“์ด ์šฐ์„  ์ˆœํšŒ์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค๋ฅด๋‹ค..


์ด์ง„ ํŠธ๋ฆฌ์˜ ๋„“์ด ์šฐ์„  ์ˆœํšŒ

  • ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ, ๋‚ฎ์€ ๋ ˆ๋ฒจ์—์„œ ๋†’์€ ๋ ˆ๋ฒจ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ์ˆœํšŒํ•œ๋‹ค.
  • ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„์€ ์–ด๋ ต๊ณ , ํ(Queue)๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๊ตฌํ˜„ํ–ˆ๋˜ ์ฝ”๋“œ ์˜ˆ์‹œ.. ํ๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์ธ์ƒ์ ์ด์—ˆ๋‹ค.

๋”๋ณด๊ธฐ

class BinaryTree:

    def __init__(self, r):
        self.root = r


    def bft(self): # ๋„“์ด ์šฐ์„  ์ˆœํšŒ
        traversal = []
        q = ArrayQueue()
        
        if self.root:
            q.enqueue(self.root)
        
        while not q.isEmpty(): # ํ ์•ˆ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋™์•ˆ์€ ๋ฃจํ”„
            curr = q.dequeue()
            traversal.append(curr.data)
            
            # ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์ž์‹์ด ์กด์žฌํ•˜๋ฉด ์ฐจ๋ก€๋กœ ํ์— ๋„ฃ๋Š”๋‹ค.
            if curr.left: 
                q.enqueue(curr.left)
            if curr.right:
                q.enqueue(curr.right)
                   
        return traversal

์ด๋ ‡๊ฒŒ ํ์— ์ฐจ๋ก€๋Œ€๋กœ ๋…ธ๋“œ๋ฅผ ๋„ฃ๊ฒŒ ๋˜๋ฉด, ํ์˜ ์„ ์ž…์„ ์ถœ์— ์˜ํ•ด ๊ฒฐ๊ตญ ๋‚ฎ์€ ๋ ˆ๋ฒจ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋œ๋‹ค! ๐Ÿ‘


์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(Binary Search Trees)์˜ ํŠน์ง•

  • ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์— ์ž์‹ ๋ณด๋‹ค ๋‚ฎ์€ ๊ฐ’์„, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์— ์ž์‹ ๋ณด๋‹ค ๋†’์€ ๊ฐ’์ž„์„ ๋งŒ์กฑํ•˜๋Š” ํŠธ๋ฆฌ
  • ํƒ์ƒ‰, ์‚ฝ์ž…, ์‚ญ์ œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ log(n)์— ๋น„๋ก€ํ•˜๋‹ค. (ํŠธ๋ฆฌ์˜ ์ขŒ์šฐ ๊ท ํ˜•์ด ๋น„์Šทํ•  ๋•Œ)

์‚ญ์ œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ–ˆ๋˜ ์ฝ”๋“œ, ์ƒ๊ฐํ•ด ๋ณผ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ข€ ์žˆ์—ˆ๋‹ค.

๋”๋ณด๊ธฐ

def remove(self, key):
        node, parent = self.lookup(key)
        if node:
            nChildren = node.countChildren()
            # The simplest case of no children
            if nChildren == 0:
                # ๋งŒ์•ฝ parent ๊ฐ€ ์žˆ์œผ๋ฉด
                # node ๊ฐ€ ์™ผ์ชฝ ์ž์‹์ธ์ง€ ์˜ค๋ฅธ์ชฝ ์ž์‹์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ
                # parent.left ๋˜๋Š” parent.right ๋ฅผ None ์œผ๋กœ ํ•˜์—ฌ
                # leaf node ์˜€๋˜ ์ž์‹์„ ํŠธ๋ฆฌ์—์„œ ๋Š์–ด๋‚ด์–ด ์—†์•ฑ๋‹ˆ๋‹ค.
                if parent:
                    if parent.left == node:
                        parent.left = None
                    else:
                        parent.right = None
                # ๋งŒ์•ฝ parent ๊ฐ€ ์—†์œผ๋ฉด (node ๋Š” root ์ธ ๊ฒฝ์šฐ)
                # self.root ๋ฅผ None ์œผ๋กœ ํ•˜์—ฌ ๋นˆ ํŠธ๋ฆฌ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
                else:
                    self.root = None
            # When the node has only one child
            elif nChildren == 1:
                # ํ•˜๋‚˜ ์žˆ๋Š” ์ž์‹์ด ์™ผ์ชฝ์ธ์ง€ ์˜ค๋ฅธ์ชฝ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ
                # ๊ทธ ์ž์‹์„ ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
                if node.left:
                    child = node.left
                else:
                    child = node.right
                # ๋งŒ์•ฝ parent ๊ฐ€ ์žˆ์œผ๋ฉด
                # node ๊ฐ€ ์™ผ์ชฝ ์ž์‹์ธ์ง€ ์˜ค๋ฅธ์ชฝ ์ž์‹์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ
                # ์œ„์—์„œ ๊ฐ€๋ฆฌํ‚จ ์ž์‹์„ ๋Œ€์‹  node ์˜ ์ž๋ฆฌ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
                if parent:
                    if parent.left == node:
                        parent.left = child
                    else:
                        parent.right = child
                # ๋งŒ์•ฝ parent ๊ฐ€ ์—†์œผ๋ฉด (node ๋Š” root ์ธ ๊ฒฝ์šฐ)
                # self.root ์— ์œ„์—์„œ ๊ฐ€๋ฆฌํ‚จ ์ž์‹์„ ๋Œ€์‹  ๋„ฃ์Šต๋‹ˆ๋‹ค.
                else:
                    self.root = child
            # When the node has both left and right children
            else:
                parent = node
                successor = node.right
                # parent ๋Š” node ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ณ ,
                # successor ๋Š” node ์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฏ€๋กœ
                # successor ๋กœ๋ถ€ํ„ฐ ์™ผ์ชฝ ์ž์‹์˜ ๋งํฌ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ๋”ฐ๋ผ๊ฐ์œผ๋กœ์จ
                # ์ˆœํ™˜๋ฌธ์ด ์ข…๋ฃŒํ•  ๋•Œ successor ๋Š” ๋ฐ”๋กœ ๋‹ค์Œ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ,
                # ๊ทธ๋ฆฌ๊ณ  parent ๋Š” ๊ทธ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค.
                while successor.left:
                    parent = successor
                    successor = successor.left
                # ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์ธ node ์— successor ์˜ key ์™€ data ๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
                node.key = successor.key
                node.data = successor.data
                # ์ด์ œ, successor ๊ฐ€ parent ์˜ ์™ผ์ชฝ ์ž์‹์ธ์ง€ ์˜ค๋ฅธ์ชฝ ์ž์‹์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ
                # ๊ทธ์— ๋”ฐ๋ผ parent.left ๋˜๋Š” parent.right ๋ฅผ
                # successor ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ (์—†์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ) ์ž์‹์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
                if successor.left:
                    child = successor.left
                else:
                    child = successor.right
                                  
                if parent.left == successor:
                    parent.left = child
                else:
                    parent.right = child

            return True

        else:
            return False

์ž์‹์˜ ์ˆ˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์™ผ์ชฝ ์ž์‹์ธ์ง€.. ์˜ค๋ฅธ์ชฝ ์ž์‹์ธ์ง€.. ๋“ฑ ๊ตฌ๋ณ„ํ•ด์•ผ ํ•œ๋‹ค.


 


 

ํž™(Heaps)์˜ ํŠน์ง•

  • ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ํ•ญ์ƒ ์ตœ๋Œ€๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ง€๋Š๋ƒ์— ๋”ฐ๋ผ ์ตœ๋Œ€ํž™, ์ตœ์†Œํž™์œผ๋กœ ๋‚˜๋‰œ๋‹ค.
  • ์„œ๋ธŒํŠธ๋ฆฌ ๋˜ํ•œ ์ตœ๋Œ€ํž™์ด๋‚˜ ์ตœ์†Œํž™์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.
  • ์™„์ „ํžˆ ํฌ๊ธฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์ง„ ์•Š์œผ๋‚˜, ํž™ ์ •๋ ฌ(Heap sort) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ์ •๋ ฌ์— ์‚ฌ์šฉ๋œ๋‹ค.
  • ์‚ฝ์ž…, ์‚ญ์ œ ์‹œ๊ฐ„์— log(n) ๋งŒํผ ์†Œ์š”ํ•œ๋‹ค.
  • ๋…ธ๋“œ์˜ ์ถ”๊ฐ€์™€ ์‚ญ์ œ๊ฐ€ ํ•ญ์ƒ ๋งˆ์ง€๋ง‰ ์›์†Œ์—์„œ๋งŒ ์ผ์–ด๋‚œ๋‹ค. โžก ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๋งŒ์กฑํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ!

 


 

๐Ÿค” ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์–ด๋ ค์› ๋˜ ๋‚ด์šฉ

์˜ค๋Š˜๋งŒ ์„ธ ๊ฐ€์ง€์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์› ๋‹ค..๐Ÿ˜‚

๊ฐ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ํŠน์ง•์ด ๋šœ๋ ทํ•˜๊ณ  ์“ฐ์ž„์ƒˆ๊ฐ€ ๋ณด์—ฌ์„œ ์žฌ๋ฐŒ๊ธด ํ–ˆ์—ˆ์ง€๋งŒ..

์ด ๊ตฌ์กฐ๋“ค์„ ์•Œ๋งž์€ ์ƒํ™ฉ์— ๋”ฑ๋”ฑ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ •๋ง ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋Š” ๊ฒŒ ๋‹ต์ธ ๊ฒƒ๊ฐ™๋‹ค..๐Ÿคค

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TIL]KDT_20230417  (0) 2023.04.17
[TIL] KDT_20230414  (0) 2023.04.14
[TIL] KDT_20230413  (0) 2023.04.13
[TIL] KDT_20230411  (0) 2023.04.11
[TIL] KDT_20230410  (0) 2023.04.10