이진트리

2022. 12. 5. 20:38·정리 전 게시글/공부 관련
#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));
    
}
저작자표시 (새창열림)

'정리 전 게시글 > 공부 관련' 카테고리의 다른 글

이진탐색트리 검색 추가 삭제  (0) 2022.12.07
두근두근 자료구조 8장 연습문제  (0) 2022.12.05
연결리스트를 이용한 다항식을 계산하고 사용자가 입력한 문자열을 파싱하는 Polynominal 코드 만들기  (0) 2022.11.29
선형덱  (0) 2022.11.28
연결된큐  (0) 2022.11.28
'정리 전 게시글/공부 관련' 카테고리의 다른 글
  • 이진탐색트리 검색 추가 삭제
  • 두근두근 자료구조 8장 연습문제
  • 연결리스트를 이용한 다항식을 계산하고 사용자가 입력한 문자열을 파싱하는 Polynominal 코드 만들기
  • 선형덱
aptenia
aptenia
공부하면서 배운 것들
  • aptenia
    새벽의 아이디어
    aptenia
  • 전체
    오늘
    어제
    • 분류 전체보기 (277)
      • f1tenth (2)
      • 개발 관련 아무거나 (1)
      • 정리 전 게시글 (268)
        • 개발 관련 (25)
        • 정보 관련 (19)
        • 공부 관련 (224)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 네이버 블로그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    콜라츠추측
    스크롤바CSS
    마크
    이것이자바다확인문제
    파이썬
    일본규슈공업대학교
    빅데이터공모전
    백준
    이것이자바다연습문제
    안드로이드
    이것이자바다
    컨텍스트스위칭
    프로그래머스PCCE
    C언어강좌
    프로그래머스
    c언어초보
    티스토리HTML
    마인크래프트스크립트
    C++강좌
    자바
    마인크래프트강화스크립트
    반복하지않는수
    티스토리반응형2스킨편집
    캡스톤디자인
    마크스크립트
    공개SW개발자대회
    C언어
    파이어베이스
    티스토리스킨편집
    마인크래프트
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
aptenia
이진트리
상단으로

티스토리툴바