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

来源:互联网 发布:药妆 知乎 编辑:程序博客网 时间:2024/06/05 02:54
#include <bits/stdc++.h>#define ElemType charusing namespace std;ElemType arr[1123];ElemType res[1123];typedef struct BinaryTree{    ElemType data;    struct BinaryTree *Left;    struct BinaryTree *Right;}Node;void BstCreat(Node *&root, ElemType x);bool CmpBst(Node *root, Node *p);int main(){    int l,n;    while(cin >> n >> l && n)    {        Node *root = new Node;        root = NULL;        for(int i = 0; i < n; i++)        {            cin >> arr[i];        }        for(int i = 0; i < n; i++)        {            BstCreat(root, arr[i]);        }        while(l--)        {            Node *p = new Node;            p = NULL;            for(int i = 0; i < n; i++)            {                cin >> res[i];            }            for(int i = 0; i < n; i++)            {                BstCreat(p, res[i]);            }            int f = CmpBst(root, p);            if(!f)            {                cout << "No" << endl;            }            else            {                cout << "Yes" << endl;            }        }    }    return 0;}void BstCreat(Node *&root, ElemType x){    if(!root)    {        root = new Node;        root->data = x;        root->Left = NULL;        root->Right = NULL;    }    else    {        if(x < root->data)        {            BstCreat(root->Left, x);        }        else        {            BstCreat(root->Right, x);        }    }}bool CmpBst(Node *root, Node *p){    if(root == NULL && p == NULL)    {        return true;    }    else if(root == NULL || p == NULL)    {        return false;    }    else    {        if(CmpBst(root->Left, p->Left) && CmpBst(root->Right, p->Right))        {            return true;        }    }    return false;}
阅读全文
0 0
原创粉丝点击