From 370a7d54a245f12fd09b230b2ab30342cc341182 Mon Sep 17 00:00:00 2001 From: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> Date: Sat, 13 Jan 2024 16:01:44 +0000 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=B8=80=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=AD=97=E7=AC=A6=E5=8F=8A?= =?UTF-8?q?=E5=85=B6=E5=87=BA=E7=8E=B0=E7=9A=84=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> --- ...0\347\232\204\346\254\241\346\225\260.cpp" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "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" 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 00000000..4aeeaf2f --- /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 -- Gitee