VioletaBabel
5639번: 이진 검색 트리 본문
#include <stdio.h>
#define mal (node *)malloc(sizeof(node));
typedef struct _node
{
struct _node *left;
struct _node *right;
int value;
}node;
node *head, *end, *temp, *cursor;
void post(node *now);
int main()
{
int num;
scanf("%d", &num);
head = mal;
end = mal;
temp = mal;
head->left = temp;
head->right = temp;
temp->left = end;
temp->right = end;
end->left = end;
end->right = end;
temp->value = num;
while (scanf("%d", &num) != EOF)
{
cursor = head->left;
while (1)
{
if (cursor->value > num)
if (cursor->left != end)
cursor = cursor->left;
else
{
temp = mal;
temp->left = end;
temp->right = end;
cursor->left = temp;
temp->value = num;
break;
}
else
if (cursor->right != end)
cursor = cursor->right;
else
{
temp = mal;
temp->left = end;
temp->right = end;
cursor->right = temp;
temp->value = num;
break;
}
}
}
post(head->left);
}
void post(node *now)
{
if (now != end)
{
post(now->left);
post(now->right);
printf("%d\n", now->value);
}
}
'백준 > 백준-C' 카테고리의 다른 글
8320번: 직사각형을 만드는 방법 (0) | 2017.05.12 |
---|---|
1620번: 나는야 포켓몬 마스터 이다솜 (0) | 2017.05.12 |
1427번: 소트인사이드 (0) | 2017.05.05 |
2108번: 통계학 (0) | 2017.05.05 |
10989번: 수 정렬하기 3 (0) | 2017.05.05 |