poj 1577

题不难,数据结构之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;
			}
			else
				insert(p->rchild,c);
		}
		else
		{
			if(p->lchild==NULL) 
			{
				p->lchild=new Tnode;
				p->lchild->data=c;
				p->lchild->lchild=NULL;
				p->lchild->rchild=NULL;
			}
			else
				insert(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;
}


 

原文地址:https://www.cnblogs.com/lj030/p/3002313.html