From ddd4aec331e1931c59b5d58be109530002ec06ec Mon Sep 17 00:00:00 2001 From: pu2rile <3o920@naver.com> Date: Mon, 5 Aug 2024 16:55:52 +0900 Subject: [PATCH] 24-08-05 --- .DS_Store | Bin 6148 -> 6148 bytes pu2rile/.DS_Store | Bin 8196 -> 6148 bytes pu2rile/README.md | 6 +- "pu2rile/\355\212\270\353\246\254/.DS_Store" | Bin 0 -> 6148 bytes ...20\354\203\211 \355\212\270\353\246\254.c" | 68 ++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 "pu2rile/\355\212\270\353\246\254/.DS_Store" create mode 100644 "pu2rile/\355\212\270\353\246\254/\354\235\264\354\247\204 \355\203\220\354\203\211 \355\212\270\353\246\254.c" diff --git a/.DS_Store b/.DS_Store index 097f6a03febd3f58dc58c3727435e2fed0922d49..cbcf6180ff4e964a9fcb0fdd742034bb013da88a 100644 GIT binary patch delta 90 zcmZoMXfc=|#>B!ku~2NHo+2a1#(>?7iv?Ji7&$icFljKd8=D*HC>U8zwr2L{Oes#z sNy^X9VccB7T*9=Oor9kPsB<$T%Xj9<{34DVj0_A+3_!3sLSzjy0Nz{_ZU6uP delta 80 zcmZoMXfc=|#>B)qu~2NHo+2ar#(>?7jO?3vSTq>fjSNh56pYL#TeJFaUdo!pw6Q^& iX)`+qKL=3#WDfYO2j zg2jTYf~6_N$vH{+`8goP17LZ1MqNe#W1ZF9=VYY#{79 zd9{f7c6k diff --git a/pu2rile/README.md b/pu2rile/README.md index 7d06827..f516ba1 100644 --- a/pu2rile/README.md +++ b/pu2rile/README.md @@ -11,4 +11,8 @@ | 7차시 | 2024.05.10 | 완전 탐색 알고리즘 | [영화감독 숌](https://www.acmicpc.net/problem/1436) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909) | 8차시 | 2024.05.14 | 그리디 알고리즘 | [팔](https://www.acmicpc.net/problem/1105) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/28#issue-2295901384) | 9차시 | 2024.05.27 | 구현 | [오늘도 졌다](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288) -| 10차시 | 2024.07.11 | 스택 | [화학식량](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169) \ No newline at end of file +| 10차시 | 2024.07.11 | 스택 | [화학식량](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169) +| 11차시 | 2024.07.13 | 우선순위 큐 | [강의실](https://www.acmicpc.net/problem/1374) | [#37](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/37#issue-2406937336) +| 12차시 | 2024.07.23 | 동적 프로그래밍 | [1학년](https://www.acmicpc.net/problem/5557) | [#40](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/40) +| 13차시 | 2024.07.26 | 스택 | [후위 표기식](https://www.acmicpc.net/problem/1918) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/43) +| 14차시 | 2024.08.05 | 트리 | [이진 탐색 트리](https://www.acmicpc.net/problem/5639) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/45) \ No newline at end of file diff --git "a/pu2rile/\355\212\270\353\246\254/.DS_Store" "b/pu2rile/\355\212\270\353\246\254/.DS_Store" new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 +#include +#include + +typedef struct TreeNode { + int data; + struct TreeNode *left, *right; +} TreeNode; + +// 새로운 노드를 생성하는 함수 +TreeNode* new_node(int key) { + TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); + node->data = key; + node->left = node->right = NULL; + return node; +} + +// 전위 순회 배열을 이용해 BST를 생성하는 함수 +TreeNode* construct_bst(int pre[], int* preIndex, int key, int min, int max, int size) { + // 트리가 공백이면 NULL 반환 + if (*preIndex >= size) + return NULL; + + TreeNode* root = NULL; + + // 현재 key가 min과 max 사이에 있을 때만 노드를 생성 + if (key > min && key < max) { + root = new_node(key); + *preIndex = *preIndex + 1; + + if (*preIndex < size) { + // 왼쪽 서브트리를 구성 + root->left = construct_bst(pre, preIndex, pre[*preIndex], min, key, size); + } + if (*preIndex < size) { + // 오른쪽 서브트리를 구성 + root->right = construct_bst(pre, preIndex, pre[*preIndex], key, max, size); + } + } + return root; +} + +// 후위 순회 함수 +void post_order(TreeNode* root) { + if (root) { + post_order(root->left); // 왼쪽 서브 트리 순회 + post_order(root->right); // 오른쪽 서브 트리 순회 + printf("%d\n", root->data); // 노드 방문 + } +} + +int main(void) { + int pre[10000]; // 최대 10000개의 노드를 입력받을 수 있도록 배열 크기를 지정 + int key; + int size = 0; + + while (scanf("%d", &key) != EOF) { + pre[size++] = key; + } + + int preIndex = 0; + if (size > 0) { + TreeNode* root = construct_bst(pre, &preIndex, pre[0], INT_MIN, INT_MAX, size); + post_order(root); + } + + return 0; +}