diff --git "a/2224020152/\347\254\254\344\271\235\345\215\225\345\205\203/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/2224020152/\347\254\254\344\271\235\345\215\225\345\205\203/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4aeeaf2fa2f89b56ede0b104b7c9d8ec2ac65907 --- /dev/null +++ "b/2224020152/\347\254\254\344\271\235\345\215\225\345\205\203/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" @@ -0,0 +1,75 @@ +#include +#include +#include + + +#define MAX_WORD (100) + +typedef struct tnode +{ +    char ch;                                 +    int cnt;                                 +    struct tnode *lchild;                   +    struct tnode *rchild;                   +}BSTNode;                                   + + +static void create_bst(BSTNode *&bt, char ch)               +{ +    if(bt == NULL)                                           +    { +        bt = (BSTNode *)malloc(sizeof(BSTNode)); +        bt->ch = ch; +        bt->cnt = 1; +        bt->lchild = bt->rchild = NULL; +    } +    else if(ch == bt->ch) +        bt->cnt++; +    else if(ch < bt->ch) +        create_bst(bt->lchild, ch); +    else +        create_bst(bt->rchild, ch); +} + + +static void inorder(BSTNode *bt) +{ +    if(bt != NULL) +    { +        inorder(bt->lchild);                     +        printf("  %c(%d)\n", bt->ch, bt->cnt); +        inorder(bt->rchild);                     +    } +} + + +static void destroy_bst(BSTNode *bt) +{ +    if(bt != NULL) +    { +        destroy_bst(bt->lchild); +        destroy_bst(bt->rchild); +        free(bt); +    } +} + +int main(int argc, char *argv[]) +{ +    BSTNode *bt = NULL; +    int i = 0; +    char str[MAX_WORD]; + +    printf("输入字符串:"); +    gets(str); +    while(str[i] != '\0') +    { +        create_bst(bt, str[i]); +        i++; +    } +    printf("字符及出现次数:\n"); +    inorder(bt); +    printf("\n"); +    destroy_bst(bt); + +    return 0; +} \ No newline at end of file