數據結構實驗之查找一:二叉排序樹

#include <cstdio>
using namespace std;
struct tree {
    int data;
    tree *l, *r;
    tree() {
        l=r=NULL;
    }
};
int x;
bool f;
tree *insert(tree *root) {
    if(!root) {
        root=new tree;
        root->data=x;
    }
    else if(x<root->data)
        root->l=insert(root->l);
    else root->r=insert(root->r);
    return root;
}
void judge(tree *root1, tree *root2) {
    if(root1&&root2) {
        if(root1->data!=root2->data) {
            f=0;
            return ;
        }
        judge(root1->l,root2->l);
        judge(root1->r,root2->r);
    }
}
int main() {
    int n, m;
    while(~scanf("%d", &n)) {
        if(!n) break;
        scanf("%d", &m);
        tree *root1=new tree;
        scanf("%d", &root1->data);
        for(int i=1;i<n;i++) {
            scanf("%d", &x);
            root1=insert(root1);
        }
        while(m--) {
            f=1;
            tree *root2=new tree;
            scanf("%d", &root2->data);
            for(int i=1;i<n;i++) {
                scanf("%d", &x);
                root2=insert(root2);
            }
            judge(root1,root2);
            if(f) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章