기술 블로그

1991번 트리 순회 본문

알고리즘 문제/BOJ

1991번 트리 순회

parkit 2018. 9. 30. 01:22
728x90
반응형

뭔가 살짝 노가다(?)로 구현한 것 같다.


다른 분들은 행렬로 구현 하셨는데, 나중에 행렬로 구현 해봐야겠다.


순회할 때, '중앙'이 기준이다. 그리고, 항상 왼쪽이 오른쪽보다 먼저 나온다.


전위 순회 : 중왼오

중위 순회 : 왼중오

후위 순회 : 왼오중


https://www.acmicpc.net/problem/1991



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
 
using namespace std;
 
typedef struct Node
{
    char alpha;
    struct Node * left;
    struct Node * right;
}Node;
 
int N = 0// 노드의 개수
 
Node * root = NULL// 루트 노드
 
bool stop = false;
 
void searchNode(Node * Root, Node * lc, Node * rc, char s)
{
    if (Root == NULL || stop) return;
 
    if (Root->alpha == s)
    {
        Root->left = lc;
        Root->right = rc;
 
        stop = true;
 
        return;
    }
 
    searchNode(Root->left, lc, rc, s);
    searchNode(Root->right, lc, rc, s);
}
 
void makeTree(char Self, char Left, char Right)
{
    Node * temp = (Node*)malloc(sizeof(Node));
    Node * left_child = (Node*)malloc(sizeof(Node));
    Node * right_child = (Node*)malloc(sizeof(Node));
 
    if (root == NULL// 루트 노드가 없을 때 즉, 최초 노드 생성
    {
        // 자신
        temp->alpha = Self;
 
        if (Left == '.') temp->left = NULL;
        else
        {
            left_child->alpha = Left;
            left_child->left = NULL;
            left_child->right = NULL;
 
            temp->left = left_child;
 
        }
 
        if (Right == '.') temp->right = NULL;
        else
        {
            right_child->alpha = Right;
            right_child->left = NULL;
            right_child->right = NULL;
 
            temp->right = right_child;
        }
 
        root = temp;
 
        return;
    }
 
    // 루트 노드가 아니라면, 들어갈 위치를 찾는다.
    if (Left == '.') left_child = NULL;
    else
    {
        left_child->alpha = Left;
        left_child->left = NULL;
        left_child->right = NULL;
    }
 
    if (Right == '.') right_child = NULL;
    else
    {
        right_child->alpha = Right;
        right_child->left = NULL;
        right_child->right = NULL;
    }
 
    searchNode(root, left_child, right_child, Self);
 
    stop = false;
}
 
// 전위 순회 : 중앙 → 왼쪽 → 오른쪽
void preOrder(Node * Root)
{
    if (Root == NULLreturn;
 
    printf("%c", Root->alpha); // 중
    preOrder(Root->left); // 왼
    preOrder(Root->right); // 오
}
 
// 중위 순회 : 왼쪽 → 중앙 → 오른쪽 
void inOrder(Node * Root)
{
    if (Root == NULLreturn;
 
    inOrder(Root->left); // 왼
    printf("%c", Root->alpha); // 중
    inOrder(Root->right); // 오
}
 
// 후위 순회 : 왼쪽 → 오른쪽 → 중앙 
void postOrder(Node * Root)
{
    if (Root == NULLreturn;
 
    postOrder(Root->left); // 왼
    postOrder(Root->right); // 오
    printf("%c", Root->alpha); // 중
}
 
int main(void)
{
    char S, L, R;
 
    scanf("%d"&N);
 
    for (int i = 0; i < N; i++)
    {
        cin >> S >> L >> R;
 
        makeTree(S, L, R);
    }
 
    preOrder(root);
    printf("\n");
    inOrder(root);
    printf("\n");
    postOrder(root);
    printf("\n");
 
    return 0;
}
cs




728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

1167번 트리의 지름  (0) 2018.09.30
1967번 트리의 지름  (0) 2018.09.30
11375번 열혈강호  (0) 2018.09.29
2188번 축사 배정  (0) 2018.09.29
15650번 N과 M(2)  (0) 2018.09.28