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

14-pu2rile #45

merged 5 commits into from
Sep 4, 2024

Conversation

pu2rile
Copy link
Collaborator

@pu2rile pu2rile commented Aug 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ - ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ ์š”์•ฝ

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ „์œ„ ์ˆœํšŒ(Preorder Traversal) ์ˆœ์„œ๋กœ ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ(Binary Search Tree)๋ฅผ ๊ตฌ์„ฑํ•œ ํ›„ ํ›„์œ„ ์ˆœํšŒ(Postorder Traversal) ์ˆœ์„œ๋กœ ๋…ธ๋“œ๋ฅผ ์ถœ๋ ฅ

image
  • ์ž…๋ ฅ(์ „์œ„ ์ˆœํšŒ) : 50 30 24 5 28 45 98 52 60
  • ์ถœ๋ ฅ(ํ›„์œ„ ์ˆœํšŒ) : 5 28 24 45 30 60 52 98 50

๐Ÿ” ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ˆœํšŒ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-08-06 แ„‹แ…ฉแ„’แ…ฎ 11 29 31
  1. ์ „์œ„ ์ˆœํšŒ : ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธ ํ›„ ์™ผ์ชฝ ์ž์‹, ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ์‹
  2. ์ค‘์œ„ ์ˆœํšŒ : ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธ ํ›„์— ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ค‘๊ฐ„์— ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ์‹
  3. ํ›„์œ„ ์ˆœํšŒ : ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹, ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๋ฐฉ๋ฌธ ํ›„ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ์‹

๋”ฐ๋ผ์„œ, ๋ฌธ์ œ์˜ ์ž…๋ ฅ(์ „์œ„ ์ˆœํšŒ)์€

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-08-06 แ„‹แ…ฉแ„’แ…ฎ 11 32 50

์ถœ๋ ฅ(ํ›„์œ„ ์ˆœํšŒ)์€

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-08-06 แ„‹แ…ฉแ„’แ…ฎ 11 34 08

์ด๋ ‡๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ์ „์œ„ ์ˆœํšŒ ๋ฐฐ์—ด ์ž…๋ ฅ
    • ์ „์œ„ ์ˆœํšŒ๋œ ๋…ธ๋“œ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด pre ์— ์ €์žฅ
  2. BST ์ƒ์„ฑ
    • construct_bst ํ•จ์ˆ˜๋กœ ์ „์œ„ ์ˆœํšŒ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ BST๋ฅผ ๊ตฌ์„ฑ
  3. ํ›„์œ„ ์ˆœํšŒ
    • post_order ํ•จ์ˆ˜๊ฐ€ BST๋ฅผ ํ›„์œ„ ์ˆœํšŒํ•˜์—ฌ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ถœ๋ ฅ

ํ•จ์ˆ˜ ์„ค๋ช…

TreeNode* construct_bst(int pre[], int* preIndex, int key, int min, int max, int size)

๊ตฌ์„ฑ

  • ์ด ํ•จ์ˆ˜๋Š” ์ „์œ„ ์ˆœํšŒ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ BST๋ฅผ ๊ตฌ์„ฑ
  • preIndex ๋Š” ๋ฐฐ์—ด์—์„œ ํ˜„์žฌ ์ฒ˜๋ฆฌ ์ค‘์ธ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • key ๋Š” ํ˜„์žฌ ๋…ธ๋“œ์˜ ๊ฐ’์ด๋ฉฐ, ๋ฐฐ์—ด์˜ pre[preIndex] ๊ฐ’
  • min ๊ณผ max ๋Š” ํ˜„์žฌ ๋…ธ๋“œ์˜ ๊ฐ’์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ์ง€์ •
    • <limits.h> ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์ธ์—์„œ INT_MIN ๊ณผ INT_MAX ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒ”์œ„ ์ œํ•œ์„ ์„ค์ •ํ–ˆ์Œ

๋™์ž‘

  • ํ•จ์ˆ˜๋Š” ๋จผ์ € preIndex ๊ฐ€ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ(size )๋ฅผ ์ดˆ๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ–ˆ๋Š”์ง€ ํ™•์ธ
  • then, ํ˜„์žฌ key ๊ฐ€ min ๊ณผ max ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š”์ง€ ํ™•์ธ
  • if TRUE, ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  preIndex ๋ฅผ ์ฆ๊ฐ€
  • then, ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌ์„ฑ

๐Ÿ’ป ์ตœ์ข… ์ฝ”๋“œ

ํ™•์ธ
#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);
    }

    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ(BST)๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ๋…ธ๋“œ๊ฐ€ ํŠน์ • ๋ฒ”์œ„ ๋‚ด์— ์žˆ์–ด์•ผ ํŠธ๋ฆฌ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ํ˜•ํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, INT_MIN ๊ณผ INT_MAX ๋ฅผ ์ดˆ๊ธฐ ๋ฒ”์œ„๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ ์‚ฝ์ž…์— ์šฉ์ดํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
โ†’ ํŠธ๋ฆฌ์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ํ•ญ์ƒ ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•˜๊ณ  ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ํ•ญ์ƒ ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ์ปค์•ผ ํ•˜๋Š”๋ฐ, ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์กฐ๊ฑด์„ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ์š” ๋ฌธ์ œ์˜ ์› ์˜๋„๋Š”, preorder ์ˆœ์„œ๋กœ ์ฃผ์–ด์ง„ ๋…ธ๋“œ ๋ฒˆํ˜ธ์—์„œ left subtree, right subtree๊ตฌ๊ฐ„์„ ์ฐพ์•„๋‚ด ์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ์ ์ ˆํ•œ ๋…ธ๋“œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฑฐ์ฃ .

image
๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ์ธ๋ฐ, ์ด๋ฅผ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ฃ .

[50, 30, 24, 5, 28, 45, 98, 52, 60]

์—ฌ๊ธฐ์„œ left subtree๋Š” 30, 24, 5, 28, 45, right subtree๋Š” 98, 52, 60์ด์ฃ .
์ž˜ ๋ณด๋ฉด left๋Š” root์ธ 50๋ณด๋‹ค ์ž‘์€(์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€) ์š”์†Œ๋“ค๋งŒ ์žˆ๊ณ , right๋Š” 50๋ณด๋‹ค ํฐ(ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€) ๊ฒƒ๋“ค๋งŒ ์žˆ์ฃ .

postorder๋Š” left subtree -> right subtree -> root ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฑฐ์ฃ ? ์ฆ‰
left subtree(30 ~ 45) ์žฌ๊ท€ ํ˜ธ์ถœ -> right subtree(98 ~ 60) ์žฌ๊ท€ ํ˜ธ์ถœ -> root(50) ์ถœ๋ ฅ
์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

#include <stdio.h>

int n = 0;
int preorder[10000];

void postorder(int begin, int end)
{
    if(begin >= end) return;
    
    int next = begin + 1;  // right subtree ์‹œ์ž‘ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ
    while(next < end && preorder[begin] >= preorder[next]) next++;

    postorder(begin + 1, next);  // left subtree ์ˆœํšŒ
    postorder(next, end);  // right subtree ์ˆœํšŒ
    printf("%d\n", preorder[begin]);  // root ์ถœ๋ ฅ
}

int main()
{
    while(scanf("%d", &preorder[n]) != EOF) n++;
    
    postorder(0, n);  // ์‹œ์ž‘ ์ธ๋ฑ์Šค๋Š” ๋‹ซํžŒ ๊ตฌ๊ฐ„, ๋ ์ธ๋ฑ์Šค๋Š” ์—ด๋ฆฐ ๊ตฌ๊ฐ„์ž…๋‹ˆ๋‹ค. ์ฆ‰ [0, n)
    
    return 0;
}

TMI๋กœ, ์ด ๋ฌธ์ œ๋Š” 22๋…„/23๋…„ ๊ถŒ์˜คํ  ๊ต์ˆ˜๋‹˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘๊ฐ„๊ณ ์‚ฌ ์‹œํ—˜๋ฌธ์ œ ์˜€์Šต๋‹ˆ๋‹ค :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants