Data_Structure/資料結構光碟檔/CH13/binaryTreeSort.c.txt

82 lines
1.9 KiB
Plaintext
Raw Normal View History

2025-01-20 21:25:33 +08:00
/* file name: binaryTreeSort.c */
/* <20>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD>Ƨ<EFBFBD> */
#include <stdio.h>
#include <stdlib.h>
struct data {
int num;
struct data *lbaby, *rbaby;
} *root, *tree, *leaves;
void find(int, struct data *);
void output(struct data *);
void printDashLine(void);
int main()
{
int data[10] = {75, 23, 98, 44, 57, 12, 29, 64, 38, 82};
int i;
printf("\n<< Binary tree sort >>\n");
printf("\n<><6E><EFBFBD>ƧǪ<C6A7><C7AA><EFBFBD><EFBFBD><EFBFBD>: ");
for (i = 0; i < 10; i++)
printf("%d ", data[i]);
printf("\n");
printDashLine();
root = (struct data *) malloc(sizeof(struct data));
root->num = data[0]; /* <20>ؾ<EFBFBD><D8BE><EFBFBD> */
root->lbaby = NULL;
root->rbaby = NULL;
printf("\n#%2d Step : ", 1);
output(root);
leaves = (struct data *) malloc(sizeof(struct data));
for (i = 1; i < 10; i++) { /* <20>ؾ<EFBFBD><D8BE>K */
leaves->num = data[i];
leaves->lbaby = NULL;
leaves->rbaby = NULL;
find(leaves->num, root);
if (leaves->num > tree->num) /* <20>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD>`<60>I<EFBFBD>j<EFBFBD>A<EFBFBD>h<EFBFBD><68><EFBFBD>k<EFBFBD>l<EFBFBD><6C> */
tree->rbaby = leaves;
else /* <20>_<EFBFBD>h<EFBFBD><68><EFBFBD>b<EFBFBD><62><EFBFBD>l<EFBFBD><6C> */
tree->lbaby = leaves;
printf("\n#%3d Step : ", i+1);
output(root);
leaves = (struct data *) malloc(sizeof(struct data));
}
puts("");
printDashLine();
printf("\n<>Ѥp<D1A4>ܤj<DCA4>Ƨǫ᪺<C7AB><E1AABA><EFBFBD><EFBFBD> : ");
output(root);
printf("\n");
return 0;
}
/* <20>M<EFBFBD><4D><EFBFBD>s<EFBFBD>`<60>I<EFBFBD>s<EFBFBD>񪺦<EFBFBD><F1AABAA6>m */
void find(int input, struct data *papa)
{
if ((input > papa->num) && (papa->rbaby != NULL))
find(input, papa->rbaby);
else if ((input < papa->num) && (papa->lbaby != NULL))
find(input, papa->lbaby);
else
tree = papa;
}
/* <20>Τ<EFBFBD><CEA4>ǰl<C7B0>ܱN<DCB1><4E><EFBFBD>ƦL<C6A6>X */
void output(struct data *node)
{
if (node != NULL) {
output(node->lbaby);
printf("%d ", node->num);
output(node->rbaby);
}
}
void printDashLine()
{
for(int i = 0; i < 58; i++)
printf("-");
}