Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-pu2rile #45

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file modified pu2rile/.DS_Store
Binary file not shown.
7 changes: 6 additions & 1 deletion pu2rile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
| 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)
| 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)
| 15์ฐจ์‹œ | 2024.08.15 | ํŠธ๋ฆฌ | [๋‚˜๋ฌด ํƒˆ์ถœ](https://www.acmicpc.net/problem/15900) | [#48](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/48)
Binary file added pu2rile/ํŠธ๋ฆฌ/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free_tree(root); ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๋™์  ํ• ๋‹น๋œ ํŠธ๋ฆฌ ๋…ธ๋“œ๋ฅผ ํ•ด์ œํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค... ๊นŒ๋จน์—ˆ์–ด์š” ๐Ÿ˜…

}

return 0;
}