-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5639.cpp
More file actions
73 lines (67 loc) · 1.57 KB
/
5639.cpp
File metadata and controls
73 lines (67 loc) · 1.57 KB
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
/*********************
insert함수를 통해 트리를 만들고 이 트리를 후위 순회 시킨다.
트리에서의 기본 노드 구조를 짤 때 참고하면 좋을 듯.
***********************/
#include <iostream>
using namespace std;
int cnt;
typedef struct Node
{
int value;
Node *leftChild, *rightChild;
Node(int _value) : value(_value), leftChild(NULL), rightChild(NULL) {}
} Node;
Node *firstNode;
void insert(int num)
{
Node *newNode = new Node(num);
if (cnt == 0)
{
firstNode = newNode;
}
else
{
Node *temp = firstNode;
while (1)
{
if (temp->value < num)
{
if (!temp->rightChild)
{
temp->rightChild = newNode;
break;
}
temp = temp->rightChild;
}
else if (temp->value > num)
{
if (!temp->leftChild)
{
temp->leftChild = newNode;
break;
}
temp = temp->leftChild;
}
}
}
cnt++;
return;
}
void postOrder(Node *ptr)
{
if (ptr)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
cout << ptr->value << "\n";
}
}
int main()
{
int num;
while (cin >> num) //이런 식으로 입력을 받으면 입력이 그만 되는 시점에 맞춰서 cin이 멈추게 된다. 이게 가장 간단하고 보기 쉬운 코드인듯.
{
insert(num);
}
postOrder(firstNode);
}