기본적으로 구현된게 있는지 궁금합니다.
그리고 잘 모르겠어서 찾아보면서 했는데
혹시 이 코드에서 필요없는 부분이 뭘까요
제가 봐도 난잡하기는 한데 원래 C++에서 포인터로 구현하던걸 비슷하게 따라하려니 너무 헷갈리네요
global root
root = None
class Tree:
def __init__(self, data, left_child=None, right_child=None, left_data = None, right_data = None):
self.data = data
self.left_child = left_child
self.right_child = right_child
self.left_data = left_data
self.right_data = right_data
def search_tree(tree, list_of_tree):
for i in range(len(list_of_tree)):
if tree.data == list_of_tree[i].left_data:
list_of_tree[i].left_child = tree
break
elif tree.data == list_of_tree[i].right_data:
list_of_tree[i].right_child = tree
break
else:
continue
def preorder_traverse(tree):
if tree == None:
return
print(tree.data,end='')
preorder_traverse(tree.left_child)
preorder_traverse(tree.right_child)
def inorder_traverse(tree):
if tree == None:
return
inorder_traverse(tree.left_child)
print(tree.data,end='')
inorder_traverse(tree.right_child)
def postorder_traverse(tree):
if tree == None:
return
postorder_traverse(tree.left_child)
postorder_traverse(tree.right_child)
print(tree.data,end='')
#a = [int(x) for x in input().split()]
number_of_tree = int(input())
treelist = [Tree(0) for i in range(number_of_tree)]
input_data = [str(x) for x in input().split()]
treelist[0].data = input_data[0]
treelist[0].left_data = input_data[1]
treelist[0].right_data = input_data[2]
root = treelist[0]
for i in range(1,number_of_tree):
a = [str(x) for x in input().split()]
for j in range(3):
if a[j] == '.':
a[j] = None
treelist[i].data = a[0]
treelist[i].left_data = a[1]
treelist[i].right_data = a[2]
search_tree(treelist[i],treelist)
preorder_traverse(root);print()
inorder_traverse(root);print()
postorder_traverse(root);print()
# example of input and output
# 7
# A B C
# B D .
# C E F
# E . .
# F . G
# D . .
# G . .
# ABDCEFG
# DBAECFG
# DBEGFCA