티스토리 뷰

프로그래밍/Data Structure

Binary Tree

DEV LION 2010. 1. 9. 14:15


#include <stdio.h>
#include <stdlib.h>

typedef struct _NODE{
        int key;
        struct _NODE *left;
        struct _NODE *right;
} NODE;

NODE* insertNode(NODE*,int);
void preorder(NODE*);
void inorder(NODE*);
void postorder(NODE*);

void main(){

        int num, i, input;
        NODE *tree = NULL;
       
        printf("입력받을 노드의 수 갯수:");
        scanf("%d",&num);
        printf("%d개의 노드의 수 입력 \n", num);
        for(i=0;i<num;i++){
                scanf("%d",&input);
                tree = insertNode(tree, input);
        }
        printf("\n\n**결과**\n");
       
        printf("\npreorder:");
        preorder(tree);
        printf("\ninorder:");
        inorder(tree);
        printf("\npostorder:");
        postorder(tree);
        printf("\n");
       
        //fflush(stdin);
        //getchar();
}

NODE* insertNode(NODE *t, int data){

        NODE *root;
        if(t==NULL){
                root = (NODE*)malloc(sizeof(NODE));
                root->left = NULL;
                root->right = NULL;
                root->key = data;
                return root;
        }

        if(data < t->key){
                t->left = insertNode(t->left,data);
                return t;
        }else{
                t->right = insertNode(t->right,data);
                return t;
        }
}

void preorder(NODE *t){

        if(t!=NULL){
                printf("%d ",t->key);
                preorder(t->left);
                preorder(t->right);
        }
}

void inorder(NODE *t){
       
        if(t!=NULL){
                inorder(t->left);
                printf("%d ",t->key);
                inorder(t->right);
        }
}

void postorder(NODE *t){
       
        if(t!=NULL){
                postorder(t->left);
                postorder(t->right);
                printf("%d ",t->key);

        }
}

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함