poj 1577

来源:互联网 发布:top是什么域名 编辑:程序博客网 时间:2024/05/16 08:41

题不难,数据结构之BST,就是输入纠结了一会

#include <iostream>#include <cstring>using namespace std;const int maxn=30;struct Tnode{char data;    Tnode * lchild;Tnode * rchild;};Tnode * root;void insert(Tnode * p,char c){if(root==NULL){root=new Tnode;root->data=c;root->lchild=NULL;root->rchild=NULL;}else{if(c>p->data){if(p->rchild==NULL) {p->rchild=new Tnode;p->rchild->data=c;p->rchild->lchild=NULL;p->rchild->rchild=NULL;}elseinsert(p->rchild,c);}else{if(p->lchild==NULL) {p->lchild=new Tnode;p->lchild->data=c;p->lchild->lchild=NULL;p->lchild->rchild=NULL;}elseinsert(p->lchild,c);}}}void print(Tnode * p){if(p==NULL) return;cout<<p->data;print(p->lchild);print(p->rchild);}int main(){char tem[maxn][maxn];int flag=1;while(flag){int count=0;while(cin>>tem[count]){if(tem[count][0]=='$'){flag=0;break;}if(tem[count][0]=='*') break;count++;}root=NULL;int i,j;int len=strlen(tem[2]);for(i=count-1;i>=0;i--){for(j=0;j<strlen(tem[i]);j++)insert(root,tem[i][j]);}print(root);cout<<endl;}return 0;}


 

原创粉丝点击