백준/백준-C

5639번: 이진 검색 트리

Beabletoet 2017. 5. 12. 18:57

#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);

}

}