3.7.3 Post-Order Traversal

2024. 2. 10. 07:50ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.7 Tree - 3.7.3 Post-Order Traversal

Post-Order Traversal

๐Ÿ’กPost-order traversal is a type of tree traversal that visits the left subtree, then the right subtree, and finally the root node. This is the opposite of pre-order traversal, which visits the root node first, then the left subtree, and finally the right subtree.

ํ›„์œ„ ์ˆœ์€ ํŠธ๋ฆฌ์˜ ์ผ์ข…์œผ๋กœ ์™ผ์ชฝ ๋ถ€๋ถ„ ํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„ ํŠธ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ์™ผ์ชฝ ๋ถ€๋ถ„ ํŠธ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„ ํŠธ๋ฆฌ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ์ „์œ„ ์ˆœํšŒ์™€ ๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค.

 

์ด์ง„ ํŠธ๋ฆฌ์—์„œ์˜ ์ˆœํšŒ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ Post-Order Traversal์€ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ 'ํ›„์œ„ ์ˆœํšŒ'๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค
์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ -> ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ -> ๋ฃจํŠธ ๋…ธ๋“œ. ์ด ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜๋ฉด, ๋…ธ๋“œ์˜ ๊ฐ’๋“ค์ด 'ํ›„์œ„' ์ˆœ์„œ๋กœ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

In Python

 

Post-Order Traversal์„ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

def printPostorder(root):
    if root:
        printPostorder(root.left)
        printPostorder(root.right)
        print(root.val)

 

ํ›„์œ„ ์ˆœํšŒ vs ์ค‘์œ„ ์ˆœํšŒ

์ •์˜์—๋”ฐ๋ผ ์ˆœ์„œ๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

 

 

ํ›„์œ„ ์ˆœํšŒ์˜ ์ •์˜์— ๋”ฐ๋ผ์„œ ์–ด๋–ค ์ ˆ์ฐจ์— ์˜ํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋Š”์ง€ ์•Œ์•„๋ณด์ž.

 


1. ์ฒ˜์Œ์—๋Š” ๋ฃจํŠธ ๋…ธ๋“œ์ธ 1์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Post-Order Traversal์€ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ, ๋จผ์ € ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 2๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค.
2. ๋…ธ๋“œ 2์—์„œ ๋˜ ๋‹ค์‹œ Post-Order Traversal์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 4๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ์ด ๋…ธ๋“œ๋Š” ๋” ์ด์ƒ ํ•˜์œ„ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4)
3. ๊ทธ ๋‹ค์Œ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 5๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ , ์ด ๋…ธ๋“œ ์—ญ์‹œ ํ•˜์œ„ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4, 5)
4. ์ด์ œ ๋…ธ๋“œ 2์˜ ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฏ€๋กœ, ๋…ธ๋“œ 2๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4,5,2)
5. ์ด์ œ ๋ฃจํŠธ ๋…ธ๋“œ 1์˜ ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฏ€๋กœ, ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 3์„ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค. (4,5,2)
6. ๋…ธ๋“œ 3์—์„œ ๋˜ ๋‹ค์‹œ Post-Order Traversal์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 6์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ์ด ๋…ธ๋“œ๋Š” ๋” ์ด์ƒ ํ•˜์œ„ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4,5,2,6)
7. ๊ทธ ๋‹ค์Œ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์ธ 7์„ ๋ฐฉ๋ฌธํ•˜๊ณ , ์ด ๋…ธ๋“œ ์—ญ์‹œ ํ•˜์œ„ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4,5,2,6,7)
8. ์ด์ œ ๋…ธ๋“œ 3์˜ ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฏ€๋กœ, ๋…ธ๋“œ 3์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4,5,2,6,7,3)
9. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฃจํŠธ ๋…ธ๋“œ 1์˜ ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฏ€๋กœ, ๋ฃจํŠธ ๋…ธ๋“œ 1์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

(4,5,2,6,7,3,1)

 

 

728x90