- 论坛徽章:
- 0
|
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct tree
- {
- char* p_word;
- int count;
- struct tree* left;
- struct tree* right;
- };
- int main()
- {
- void output_tree(struct tree*);
- void input_tree(struct tree*,char*);
- struct tree* root=NULL;
- //input
- char* p=malloc(sizeof(char)*20);
- setbuf(stdin,NULL);
- scanf("%s",p);
- while((*p)!='0')
- {
- input_tree(root,p);
- setbuf(stdin,NULL);
- p=malloc(20*sizeof(char));
- scanf("%s",p);
- }
- //output
- output_tree(root);
- }
- void input_tree(struct tree* root,char* p)
- {
- if(root==NULL)
- {
- root=malloc(sizeof(struct tree));
- root->p_word=p;
- root->count=1;
- root->left=root->right=NULL;
- }
- else if(strcmp(p,root->p_word)==0)
- root->count++;
- else if(strcmp(p,root->p_word)<0)
- input_tree(root->left,p);
- else if(strcmp(p,root->p_word)>0)
- input_tree(root->right,p);
- }
- void output_tree(struct tree* root)
- {
- if(root->left!=NULL)
- output_tree(root->left);
- if(root!=NULL)
- printf("%s:%d\n",root->p_word,root->count);
- if(root->right!=NULL)
- output_tree(root->right);
- }
复制代码
|
|