-
Notifications
You must be signed in to change notification settings - Fork 1
7-dohyeondol1 #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
7-dohyeondol1 #26
Conversation
|
νΈλ¦¬λ μ λ§ μ€λλ§μ 보λ€μ. μ²μμ νΈλ¦¬λ₯Ό μΈμ νλ ¬λ‘ λ°μΌλ €κ³ νλ€κ°, νμ΄μ¬μ λμ
λλ¦¬κ° λ μ¬λΌμ κ·Έκ±Έ μ¬μ©νμ΅λλ€. Python
def preorder(tree, node, result):
if node == '.':
return
result.append(node)
preorder(tree, tree[node][0], result)
preorder(tree, tree[node][1], result)
def inorder(tree, node, result):
if node == '.':
return
inorder(tree, tree[node][0], result)
result.append(node)
inorder(tree, tree[node][1], result)
def postorder(tree, node, result):
if node == '.':
return
postorder(tree, tree[node][0], result)
postorder(tree, tree[node][1], result)
result.append(node)
import sys
input = sys.stdin.readline
tree = {}
for _ in range(int(input())):
parent, left, right = input().split()
tree[parent] = (left, right)
pre_result = []
in_result = []
post_result = []
preorder(tree, 'A', pre_result)
inorder(tree, 'A', in_result)
postorder(tree, 'A', post_result)
print(''.join(pre_result))
print(''.join(in_result))
print(''.join(post_result)) |
dohyeondol1/νΈλ¦¬/7-dohyeondol1.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return 0 λΉ μ‘μ΄μ γ
γ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ§λ€! γ
γ
γ
γ
λλΆμ return 0 μμΌλλ°λ μ λμκ°λμ§μ λν΄μ μ°Ύμλ΄€μ΅λλ€...γ
return 0μ νλ‘κ·Έλ¨μ΄ μ μμ μΌλ‘ μ μ’
λ£λμμμ μ΄μ체μ μ μ리기 μν¨μΈλ°,
Cλ C99 μ΄νλ‘, C++μ C++11μ΄νλ‘ λͺ
μμ μΌλ‘ return 0μ μμ±νμ§ μμλ μμμ mainμμ 0μ λ°ννκ² λμλ€λ€μ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ§μμ γ γ λ°±μ€μμ void mainμ΄ μ λλ μ΄μ λ κ°μ μ΄μ μ λλ€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νΈλ¦¬λ λ€μ΄λ³΄κΈ°λ§ νκ³ μ²μ 곡λΆν΄λ΄€λ€μ..! μ°Έμ‘°ν΄μ£Όμ λ§ν¬κ° ν° λμμ΄ λμ΄μ
μ²μμ ν΄λμ€λ‘ ꡬνν΄λ³΄λ € νλλ° ν΄λμ€μ νΈλ¦¬κ° λͺ¨λ μ΄λ©΄μ΄λΌ.. 2μ°¨μ λ°°μ΄λ‘ μ μμνλ§μ΄λΌλ ꡬνν΄λ΄€μ΅λλ€..! μκ°λ³΄λ€ ν₯λ―Έλ‘μμ λ 곡λΆν΄λ³΄κ³ μΆλ€μ
νμ΄λ€
import sys
N = int(input())
tree = [[None, None] for _ in range(N)]
for i in range(N):
a, b, c = sys.stdin.readline().split()
tree[ord(a) - 65][0] = b
tree[ord(a) - 65][1] = c
def preorder(node):
if node == ".":
return
print(node, end='')
idx = ord(node) - ord('A')
preorder(tree[idx][0])
preorder(tree[idx][1])
preorder('A')
|
@hadongun |
caucsejunseo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νΈλ¦¬λ μ²μ 보λ μΉκ΅¬λ€μ κ·Έλ¦Όκ³Ό 첨λΆνμΌμ μ¬λ €μ£Όμ μ νΈνκ² κ³΅λΆνμ΅λλ€!
μμ€ν€μ½λμ μ¬κ·ν¨μ κ°μ΄ νμ μμ°λ κ²λ€μ λ°λ‘λ°λ‘ μκ°μ΄ μλλ€μ. νμ€ν λ λ€μνκ³ λ§μ λ¬Έμ μ ν΄λ΄μΌ ν κ±° κ°μ΅λλ€!
μ¬κ·ν¨μλ μ¬λ°λ λ¬Έμ λ€μ΄ λ§μΌλ μ‘°κΈμ© λ¨Ήμ΄λ³΄λ©΄μ μ΅μν΄μ§μ λ μ’μ΅λλ€! |
π λ¬Έμ λ§ν¬
νΈλ¦¬ μν
μ€λμ νΈλ¦¬λ₯Ό 곡λΆνκΈ° λλ¬Έμ, μ΄λ₯Ό ꡬνν΄λ³Ό μ μλ λ¬Έμ λ₯Ό κ°μ Έμμ΅λλ€.βοΈ μμλ μκ°
15λΆ
β¨ μλ μ½λ
κ°λ¨νκ² νΈλ¦¬ μνλ₯Ό ꡬννλ λ¬Έμ μ λλ€.
μ μ μν, μ€μ μν, νμ μν μΈ κ°μ§λ₯Ό ꡬνν΄μΌ νλλ°, νΈλ¦¬κ° μ΄λ ΄νμ΄ λμ§λ§ μκ³ μμ΄λ κ·Έλ κ² μ΄λ ΅μ§ μμ΅λλ€.
λ¬Έμ μμλ μμ μ λ ₯1μ λν μΈκ°μ§ νΈλ¦¬ μνμ κ²°κ³Όλ₯Ό μλ €μ£Όκ³ μ죡..
κ°κ°μ μνλ₯Ό κ·Έλ¦ΌμΌλ‘ ννν΄ λ΄ μλ€.
λΆλͺ¨μ λ°©λ¬Έ μμ μ κΈ°μ€μΌλ‘ μ©μ΄λ₯Ό λ°λΌλ³΄λ©΄ λ μ½κ² μ΄ν΄ν μ μμ΅λλ€.
κ°λ μ μ΄μ λλ‘ μΆ©λΆν κ² κ°μΌλ,
μ νμ΄ μ€λͺ μΌλ‘ λμ΄κ°λ³΄κ² μ΅λλ€.
μ΄μ PRκ³Ό λΉμ·νκ², κ° λ Έλμ μμ λ Έλλ₯Ό μΈμ 리μ€νΈλ‘ ꡬννμμ΅λλ€.

μλλ μμ μ λ ₯ 1μ μΈμ 리μ€νΈλ‘ ꡬνν λ°©μμ ννν κ·Έλ¦Όμ λλ€.
char νμ μ C++ λ΄λΆμμλ μ μ(ASCII κ°)μΌλ‘ μ²λ¦¬λκΈ° λλ¬Έμ 'A'-'A' = 0μ΄ μ±λ¦½ν©λλ€.
Aλ ASCII κ° 65μ λλ€. B = 66, C = 67, D = 68 ... μ΄λ° μμΌλ‘ κ°μ κ°μ§μ£±.
κ·Έλμ νμ¬ λ Έλ μνλ²³ - 'A' λ‘ κ° λ Έλμ μΈλ±μ€λ₯Ό μ€μ νμμ΅λλ€.
μΈλ±μ€ μ€μ λ λμκ³ μΈμ 리μ€νΈλ ꡬννλ€λ©΄ λ§μ§λ§μΌλ‘ κ° μνλ₯Ό ꡬννλ λΆλΆμ λλ€.
μ λ κ° μνλ₯Ό DFSλ₯Ό μ΄μ©νμ¬ κ΅¬ννμμ΅λλ€.
μ½λλ₯Ό 보면 μ΄μ PRλ€μμ 보μλ DFSλ λΉμ·ν ννλΌλκ² κ°μ΄ μ€μ€ κ² κ°μ΅λλ€.
μ½λμ νμμ κ°κ³ μΆλ ₯μ μ΄λμ νλμ λ°λΌ μΈκ°μ§ μνλ₯Ό μΆλ ₯ν μ μμ΅λλ©
μ¬κ·λ₯Ό μ¬μ©νμ¬ λ Έλλ₯Ό λ°©λ¬Ένλ―λ‘ μ΄λ¬ν ννλ₯Ό κ°μ§κ² λ©λλ€.
μΌμͺ½ μμλΆν° μ¬κ·λ₯Ό μ¬μ©νλ κ²μ μΈ μν λͺ¨λ μΌμͺ½ μμμ΄ μ€λ₯Έμͺ½ μμλ³΄λ€ λ¨Όμ λ°©λ¬ΈλκΈ° λλ¬Έμ΄μ£±.
κ° μνλ§λ€ μ΄λμ μΆλ ₯μ ν΄μΌ ν μ§ μ΄ν΄λ΄ μλ€.
μ μ μν
(루νΈ) β (μΌμͺ½ μμ) β (μ€λ₯Έμͺ½ μμ) μμλ‘ λ°©λ¬Ένλ―λ‘,
λ°©λ¬Ένλ μμλλ‘ μΆλ ₯νλ©΄ λ©λλ€.
μμν λ Aλ‘ μμνλ€λ©΄,
A(μΆλ ₯) β A μΌμͺ½μμ B(μΆλ ₯) β B μΌμͺ½μμ D(μΆλ ₯) β D μμμκ³ B μ€λ₯Έμͺ½ μμ μμΌλ A μ€λ₯Έμͺ½ μμ C(μΆλ ₯)
β C μΌμͺ½ μμ E(μΆλ ₯) β E μμ μμΌλ C μ€λ₯Έμͺ½ μμ F(μΆλ ₯) β F μΌμͺ½ μμ μμΌλ F μ€λ₯Έμͺ½ μμ G(μΆλ ₯)
μ΄λ¬ν μμλλ‘ μΆλ ₯λκ² μ£ .
μ€μ μν
(μΌμͺ½ μμ) β (루νΈ) β (μ€λ₯Έμͺ½ μμ) μμλ‘ λ°©λ¬Ένλ―λ‘,
μΌμͺ½ μμ λ°©λ¬Έμ΄ λλ μμλλ‘ μΆλ ₯νλ©΄ λ©λλ€.
νμ μν
(μΌμͺ½ μμ) β (μ€λ₯Έμͺ½ μμ) β (루νΈ) μμλ‘ λ°©λ¬Ένλ―λ‘,
μμμ΄ μκ±°λ μμ λͺ¨λ λ°©λ¬Έμ΄ λλ μμλλ‘ μΆλ ₯νλ©΄ λ©λλ€.
μΆλ ₯ μμΉκ° μ ν΄μ‘λ€λ©΄, mainμμ ν¨μλ₯Ό μ€νμμΌ κ° μν κ²°κ³Όλ₯Ό μΆλ ₯νμ¬ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ΄μ§ νΈλ¦¬ μν μκ³ λ¦¬μ¦(Binary Tree Traversal Algorithm)
μ¬μ΄ν΄μ΄ μλ μ°κ²° κ·Έλνλ₯Ό νΈλ¦¬λΌκ³ ν©λλ€.
μ¬κΈ°μ κ° λ Έλκ° μ΅λ λ κ°μ μμ λ Έλλ₯Ό κ°μ§λ©΄ μ΄λ₯Ό μ΄μ§ νΈλ¦¬λΌκ³ ν©λλ€.
(λ μμ λ Έλλ μΌμͺ½ μμ λ Έλ, μ€λ₯Έμͺ½ μμ λ ΈλλΌκ³ ν©λλ€.)
μ΄μ§ νΈλ¦¬λ μ£Όλ‘ 4κ°μ§ μνλ°©λ²μ μ¬μ©ν©λλ€.
κ·Έμ€ 3κ°λ μμμ μ€λͺ νλ μ μ, μ€μ, νμ μν μ΄κ³ ,
λλ¨Έμ§ νλκ° λ 벨 μμ μνμ λλ€.
κ°λ¨ν λ§ν΄μ λ Έλμ κΉμ΄μ λ°λΌ μμλλ‘ λ°©λ¬Ένλ λ°©μμ λλ€.
μ΄λμ λΉμ·ν κ±Έ λ³Έ κ² κ°μλ°....νλ μμλ BFS νμ λ°©λ²μ μ¬μ©νλλΌκ΅¬μ.
μ λ¬Έμ μμμ μμ μ λ ₯ 1μ λ 벨 μμ μνλ‘ λ°©λ¬Ένλ©΄ ABCDEFGκ° μΆλ ₯λ©λλ€.
νΈλ¦¬κ° μκ°λ³΄λ€ 곡λΆν λ΄μ©μ΄ λ§μμ μ¬κΈ°μ λ€ λ΄μ§λ λͺ»νμ΅λλ€..!
μ¬λ¬ μλ£λ₯Ό μ°Έκ³ νμ§λ§ κ°μ₯ λμμ΄ λλ μλ£λ₯Ό λ¨κΉλλ€.
[μλ£κ΅¬μ‘°] Tree - Binary Tree (μ΄μ§ νΈλ¦¬) - κ°λ κ³Ό ꡬν