ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๊นŠ์ด ์šฐ์„  ์ˆœํšŒ (Depth First Traversal)

  • ์ค‘์œ„ ์ˆœํšŒ (in-order travelsal) : ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒํ•œ ๋’ค ๋…ธ๋“œ x๋ฅผ ๋ฐฉ๋ฌธ, ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ
  • ์ „์œ„ ์ˆœํšŒ (pre-order travelsal) : ๋…ธ๋“œ x๋ฅผ ๋ฐฉ๋ฌธํ•œ ํ›„์— ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ, ๋งˆ์ง€๋ง‰์œผ๋กœ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ
  • ํ›„์œ„ ์ˆœํšŒ (post-order traversal) : ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ ํ›„ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋…ธ๋“œ x๋ฅผ ๋ฐฉ๋ฌธ

 

class Node:

    def __init__(self, item):
        self.data = item
        self.left = None
        self.right = None


    def inorder(self):
        traversal = []
        if self.left:
            traversal += self.left.inorder()
        traversal.append(self.data)
        if self.right:
            traversal += self.right.inorder()
        return 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
        
        
        
    def postorder(self):
        traversal = []
        if self.left:
            traversal += self.left.postorder()
        
        if self.right:
            traversal += self.right.postorder()
            
        traversal.append(self.data)
        return traversal


class BinaryTree:

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


    def inorder(self):
        if self.root:
            return self.root.inorder()
        else:
            return []


    def preorder(self):
        if self.root:
            return self.root.preorder()
        else:
            return []
            
            
    def postorder(self):
        if self.root:
            return self.root.postorder()
        else:
            return []

def solution(x):
    return 0
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€