hdu 3791 二叉查找树,BST

796093 asia2562219 F Accepted 524 KB 15 ms G++ 1525 B 2012-11-04 12:37:34

关键字树~ 简单题。。

前序 中序 判同。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 11111
struct Node
{
char data;
Node* left;
Node* right;
};
class BTree
{
public:
Node *root;
void init(char *ch)
{
Node* temp[100];
int front=0;
Node *ptr,*now,*temp1;

while(ch[front]!='\0')
{
    ptr=new Node;
    ptr->data=ch[front];
    ptr->left=NULL;
    ptr->right=NULL;
    temp[++front]=ptr;
    if(1==front)
    {
        root=ptr;
    }
    else
    {
        now=root;
        while(now!=NULL)
        {
            temp1=now;
            if(ptr->data>now->data)
            now=now->right;
            else now=now->left;
        }
        if(ptr->data>temp1->data)
            temp1->right=ptr;
        else temp1->left=ptr;
    }

}
;
}
void first(Node *now,string &s)
{
if(now==NULL)return;
s+=now->data;
first(now->left,s);
first(now->right,s);
}
void mid(Node *now,string &s)
{
if(now==NULL)return;
mid(now->left,s);
s+=now->data;
mid(now->right,s);
}
bool operator == (BTree o)
    {
         string s1,s2;
         first(root,s1);
         first(o.root,s2);
         if(s1!=s2)return 0;
         mid(root,s1);
         mid(o.root,s2);
         if(s1!=s2)return 0;
         return 1;
    }
}temp,cmp;

int main()
{
int n;
char ch[12];
while(scanf("%d",&n),n)
{
    scanf("%s",ch);
    temp.init(ch);
    for(int i=0;i<n;i++)
    {
        scanf("%s",ch);
        cmp.init(ch);
        if(cmp==temp)
        printf("YES\n");
        else printf("NO\n");
    }
}


return 0;
}

时间压力好大

posted @ 2012-11-04 12:45  TO_Asia  阅读(158)  评论(0编辑  收藏  举报