二叉排序树
来源:互联网 发布:怎么找文献的数据 编辑:程序博客网 时间:2024/05/01 16:39
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;struct node{ char word[35]; int count; struct node *left,*right; node(char *n) { strcpy(word,n); count=1; left=NULL; right=NULL; } }*root;node *insert(node *p,char *word){ if(p==NULL) p= new node(word); else if(strcmp(word,p->word)<0) { p->left = insert(p->left,word); } else if(strcmp(word,p->word)==0) { p->count++; } else { p->right = insert(p->right,word); } return p;}int sum;void dfs(node *p){ if(p==NULL) return ; dfs(p->left); printf("%s %.4lf\n",p->word,1.0*(p->count)/sum*100); dfs(p->right); delete(p); }int main(){ sum=0; char str[35]; while(gets(str)) { root = insert(root,str); sum++; } dfs(root); // for(;;); return 0;}