본문 바로가기

프로그래밍/C, C++

이진트리

728x90
반응형
#include <stdio.h>
#include <stdlib.h>
typedef struct BinTrNode {
    char data;
    struct BinTrNode* left;
    struct BinTrNode* right;
} TNode;
typedef struct LinkedNode {
    TNode* data;
    struct LinkedNode* link;
} Node_q;
Node_q* front = NULL;
Node_q* rear = NULL;
int is_empty() { return front == NULL; }
void init_queue() { front = rear = NULL; }
int size() {
    Node_q* p;
    int count = 0;
    for (p = front; p != NULL; p = p->link) count++;
    return count;
}
void enqueue(TNode* e) {
    Node_q* node = (Node_q*)malloc(sizeof(Node_q));
    node->data = e;
    node->link = NULL;
    if (is_empty()) {
        front = rear = node;
    }
    else {
        rear->link = node;
        rear = node;
    }
}
TNode* dequeue() {
    Node_q* node = front;
    TNode* e;
    if (!is_empty()) {
        node = front;
        front = front->link;
        if (front == NULL) rear = NULL;
        e = node->data;
        free(node);
        return e;
    }
    else {
        exit(1);
    }
}
TNode* root;
void init_tree() { root = NULL; }
int is_empty_tree() { return root == NULL; }
TNode* get_root() { return root; }
TNode* create_tree(char val, TNode* l, TNode* r) {
    TNode* n = (TNode*)malloc(sizeof(TNode));
    n->data = val;
    n->left = l;
    n->right = r;
    return n;
}
void preorder(TNode* node) {
    if (node != NULL) {
        printf("[%c] ", node->data);
        preorder(node->left);
        preorder(node->right);
    }

}
void inorder(TNode* node) {
    if (node != NULL) {
        inorder(node->left);
        printf("[%c] ", node->data);
        inorder(node->right);
    }
}
void postorder(TNode* node) {
    if (node != NULL) {
        postorder(node->left);
        postorder(node->right);
        printf("[%c] ", node->data);
    }
}
void levelorder(TNode* node) {
    TNode* n;
    if (root == NULL) return;
    init_queue();
    enqueue(root);
    while (!is_empty()) {
        n = dequeue();
        if (n != NULL) {
            printf("[%c] ", n->data);
            enqueue(n->left);
            enqueue(n->right);
        }
    }
}
int count_node(TNode* node) {
    if (node == NULL) return 0;
    return 1 + count_node(node->left) + count_node(node->right);
}
int count_leaf(TNode* node) {
    if (node == NULL) return 0;
    if (node->left == NULL && node->right == NULL) return 1;
    else return count_leaf(node->left) + count_leaf(node->right);
}
int calc_height(TNode* node) {
    int hLef, hRight;
    if (node == NULL) return 0;
    hLef = calc_height(node->left);
    hRight = calc_height(node->right);
    return(hLef > hRight) ? hLef + 1 : hRight + 1;
}
void main()
{
    TNode* b, * c, * d, * e, * f;
    init_tree();
    d = create_tree('D', NULL, NULL);
    e = create_tree('E', NULL, NULL);
    b = create_tree('B', d, e);
    f = create_tree('F', NULL, NULL);
    c = create_tree('C', f, NULL);
    root = create_tree('A', b, c);
    printf("\n Pre-Order : "); preorder(root);
    printf("\n In-Order : "); inorder(root);
    printf("\n Post-Order : "); postorder(root);
    printf("\n Level-Order : "); levelorder(root);
    printf("\n");

    printf("노드의 개수 = %d\n", count_node(root));
    printf("단말의 개수 = %d\n", count_leaf(root));
    printf("트리의 높이 = %d\n", calc_height(root));
    
}
728x90
반응형

'프로그래밍 > C, C++' 카테고리의 다른 글

이진탐색트리 검색 추가 삭제  (0) 2022.12.07
두근두근 자료구조 8장 연습문제  (0) 2022.12.05
선형덱  (0) 2022.11.28
연결된큐  (0) 2022.11.28
연결리스트 스택  (0) 2022.11.04