-
Notifications
You must be signed in to change notification settings - Fork 0
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
14-pu2rile #45
Conversation
int preIndex = 0; | ||
if (size > 0) { | ||
TreeNode* root = construct_bst(pre, &preIndex, pre[0], INT_MIN, INT_MAX, size); | ||
post_order(root); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
free_tree(root);
๋ฅผ ์ถ๊ฐํด์ ๋์ ํ ๋น๋ ํธ๋ฆฌ ๋
ธ๋๋ฅผ ํด์ ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ฐฉ์งํ ์ ์์ต๋๋ค... ๊น๋จน์์ด์ ๐
There was a problem hiding this 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๊ตฌ๊ฐ์ ์ฐพ์๋ด ์ด๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐฉ๋ฌธํ๋ฉฐ ์ ์ ํ ๋ ธ๋๋ฅผ ์ถ๋ ฅํ๋ ๊ฑฐ์ฃ .
๋ฌธ์ ์์ ์ฃผ์ด์ง ํธ๋ฆฌ์ธ๋ฐ, ์ด๋ฅผ ๋ฐฐ์ด๋ก ํํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ฃ .
[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๋ ๊ถ์คํ ๊ต์๋ ์๊ณ ๋ฆฌ์ฆ ์ค๊ฐ๊ณ ์ฌ ์ํ๋ฌธ์ ์์ต๋๋ค :)
๐ ๋ฌธ์ ๋งํฌ
๋ฐฑ์ค - ์ด์ง ํ์ ํธ๋ฆฌ
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
โจ ์๋ ์ฝ๋
๋ฌธ์ ์์ฝ
์๋ ์ฌ์ง๊ณผ ๊ฐ์ด ์ฃผ์ด์ง ํธ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ ์ ์ํ(Preorder Traversal) ์์๋ก ๋ ธ๋๋ฅผ ์ ๋ ฅ๋ฐ์ ์ด์ง ๊ฒ์ ํธ๋ฆฌ(Binary Search Tree)๋ฅผ ๊ตฌ์ฑํ ํ ํ์ ์ํ(Postorder Traversal) ์์๋ก ๋ ธ๋๋ฅผ ์ถ๋ ฅ
50 30 24 5 28 45 98 52 60
5 28 24 45 30 60 52 98 50
๐ ์ด์ง ํธ๋ฆฌ์ ์ํ
๋ฐ๋ผ์, ๋ฌธ์ ์ ์ ๋ ฅ(์ ์ ์ํ)์
์ถ๋ ฅ(ํ์ ์ํ)์
์ด๋ ๊ฒ ๊ตฌํ ์ ์์ต๋๋ค.
์๊ณ ๋ฆฌ์ฆ
pre
์ ์ ์ฅconstruct_bst
ํจ์๋ก ์ ์ ์ํ ๋ฐฐ์ด์ ์ด์ฉํ์ฌ ์ฌ๊ท์ ์ผ๋ก BST๋ฅผ ๊ตฌ์ฑpost_order
ํจ์๊ฐ BST๋ฅผ ํ์ ์ํํ์ฌ ๋ ธ๋์ ๊ฐ์ ์ถ๋ ฅํจ์ ์ค๋ช
๊ตฌ์ฑ
preIndex
๋ ๋ฐฐ์ด์์ ํ์ฌ ์ฒ๋ฆฌ ์ค์ธ ์์๋ฅผ ๊ฐ๋ฆฌํดkey
๋ ํ์ฌ ๋ ธ๋์ ๊ฐ์ด๋ฉฐ, ๋ฐฐ์ด์pre[preIndex]
๊ฐmin
๊ณผmax
๋ ํ์ฌ ๋ ธ๋์ ๊ฐ์ด ๊ฐ์ง ์ ์๋ ๋ฒ์๋ฅผ ์ง์ <limits.h>
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์ธ์์INT_MIN
๊ณผINT_MAX
๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒ์ ์ ํ์ ์ค์ ํ์๋์
preIndex
๊ฐ ๋ฐฐ์ด์ ํฌ๊ธฐ(size
)๋ฅผ ์ด๊ณผํ๋์ง ํ์ธํ์ฌ ๋ฐฐ์ด์ ๋์ ๋๋ฌํ๋์ง ํ์ธkey
๊ฐmin
๊ณผmax
๋ฒ์ ๋ด์ ์๋์ง ํ์ธpreIndex
๋ฅผ ์ฆ๊ฐ๐ป ์ต์ข ์ฝ๋
ํ์ธ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ด์ง ๊ฒ์ ํธ๋ฆฌ(BST)๋ฅผ ๊ตฌ์ฑํ ๋ ๋ ธ๋๊ฐ ํน์ ๋ฒ์ ๋ด์ ์์ด์ผ ํธ๋ฆฌ๊ฐ ์ฌ๋ฐ๋ฅธ ํํ๋ฅผ ์ ์งํ ์ ์์ต๋๋ค. ์ด๋,
INT_MIN
๊ณผINT_MAX
๋ฅผ ์ด๊ธฐ ๋ฒ์๋ก ์ฌ์ฉํ๋ฉด ์ฒซ ๋ฒ์งธ ๋ ธ๋ ์ฝ์ ์ ์ฉ์ดํ๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค.โ ํธ๋ฆฌ์ ์ผ์ชฝ ์๋ธํธ๋ฆฌ๋ ํญ์ ํ์ฌ ๋ ธ๋๋ณด๋ค ์์์ผ ํ๊ณ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ ํญ์ ํ์ฌ ๋ ธ๋๋ณด๋ค ์ปค์ผ ํ๋๋ฐ, ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ์ฌ์ฉํ๋ฉด ์ด๋ฌํ ์กฐ๊ฑด์ ์ฝ๊ฒ ํ์ธํ ์ ์์ต๋๋ค.