zoukankan      html  css  js  c++  java
  • 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;
    }
    


     

  • 相关阅读:
    CF1480
    网络编程中常见地址结构与转换(IPv4/IPv6)
    inet_pton, inet_ntop
    mktime 夏令时
    C/C++中volatile关键字详解
    STL之vector容器详解
    Linux学习--gdb调试
    Linux编程基础——GDB(设置断点)
    FTP模式简式:PORT/PASV/EPRT/EPSV
    strchr和strstr 函数
  • 原文地址:https://www.cnblogs.com/lj030/p/3002313.html
Copyright © 2011-2022 走看看