SDUT 3373 数据结构实验之查找一:二叉排序树

来源:互联网 发布:淘宝拉帮结派流量协议 编辑:程序博客网 时间:2024/05/21 17:43

点击打开题目链接

#include <bits/stdc++.h>using namespace std;struct node{    char data;    node *left, *right;};int flag;int _array[15];//void Inorder(node *root);node *Insert(node *root, int x);void cmpare(node *root1, node *root2);node *creat(int lenth, int _array[]);int main(){    node *root1, *root2;    int n, m;    while(cin >> n, n)    {        cin  >> m;        for(int i = 0; i < n; i++)        {            cin >> _array[i];        }        root1 = creat(n, _array);        while(m--)        {            flag = 1;            for(int i = 0; i < n; i++)            {                cin >> _array[i];            }            root2 = creat(n, _array);            cmpare(root1, root2);            if(flag)            {                cout << "Yes" << endl;            }            else            {                cout << "No" << endl;            }        }    }    return 0;}node *creat(int lenth, int  _array[]){    node *root = NULL;    int i = 0;    while(i < lenth)    {        root = Insert(root, _array[i]);        i++;    }    return root;}node *Insert(node *root,int x){    if(!root)    {        root = new node;        root -> data = x;        root -> left = NULL;        root -> right = NULL;    }    else if(x < root -> data)    {        root -> left = Insert(root -> left, x);    }    else    {        root -> right = Insert(root -> right, x);    }}/*void Inorder(node *root){    if(root)    {        Inorder(root -> left);        cout << root -> data;        Inorder(root -> right);    }}*/void cmpare(node *root1, node *root2){    if(root1==NULL &&root2==NULL)    {        return;    }    else if(root1==NULL || root2==NULL)    {        flag=0;        return;    }    else if(root1->data!=root2->data)    {        flag=0;        return;    }    else    {        cmpare(root1->left, root2->left);        cmpare(root1->right, root2->right);    }}


0 0
原创粉丝点击