7-20 Binary Search Tree(25 分)

https://pintia.cn/problem-sets/16/problems/682

A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tree may correspond to several different insertion sequences. Now given several insertion sequences, it is your job to determine if they can generate the same binary search tree.

Input Specification:

Input consists of several test cases. For each test case, the first line contains two positive integers N (10) and L, which are the total number of integers in an insertion sequence and the number of sequences to be tested, respectively. The second line contains N positive integers, separated by a space, which are the initially inserted numbers. Then L lines follow, each contains a sequence of N integers to be checked.

For convenience, it is guaranteed that each insertion sequence is a permutation of integers 1 to N - that is, all the N numbers are distinct and no greater than N. The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, output to the standard output. Print in L lines the checking results: if the sequence given in the i-th line corresponds to the same binary search tree as the initial sequence, output to the i-th line Yes; else output No.

Sample Input:

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample Output:

Yes
No
No
//注意!vector与普通数组的一个重要区别:作为函数参数时,普通数组按数组名传递即按地址传递, 但vector 按向量名传递相当于按值传递,
//所以如果希望函数体内对vector的修改在函数执行完后仍有效, 则vector需按 引用 或者 地址 传递, 形参类型分别为 vector<T> & 和 vector<T> *
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

typedef struct BinNode{
    int data;
    BinNode * lc;
    BinNode * rc;
    BinNode(int d, BinNode *l = NULL, BinNode *r = NULL):
        data(d), lc(l), rc(r){}
}BinNode, * BST;

void Init_Array(int *, int);
BST Init_BST(int *, int);
bool Compare_BST(BST, BST);
void preorder(BST, std::vector<int>&);//前序采集各结点的data依次存入vector中
void inorder(BST, std::vector<int>&);//中序采集各结点的data依次存入vector中
bool comp_vector(vector<int>, vector<int>);//比较两个vector的各元素是否顺次相等,全相等返回true,否则返回false

int main()
{
    int N, L;
    vector<bool> YorN;
    while(true){
        cin >> N;
        if(N == 0)
            break;
        cin >> L;

        int a[N];
        Init_Array(a, N);
        int b[L][N];
        for(int j = 0; j < L; ++j){
            Init_Array(b[j],N);
            //对于二维数组b[row][column], b[row]的作用相当于一维数组的数组名
        }

        BST bst_tree = Init_BST(a, N);//初始化第一棵BST
        BST bst_comp[L];//保存L棵用于比较的BST
        for(int j = 0; j < L; ++j){
            bst_comp[j] = Init_BST(b[j], N);
            if(Compare_BST(bst_tree, bst_comp[j]))//判断两棵树是否相等
                YorN.push_back(true);
            else
                YorN.push_back(false);
        }
    }
    vector<bool>::iterator it;
    for(it = YorN.begin(); it != YorN.end(); it++){
        if((*it) == true)
            cout << "Yes";
        else
            cout << "No";
        if(it+1 != YorN.end())
            cout << endl;
    }
    return 0;
}

void Init_Array(int *arr, int n)
{
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
    }
}

BST Init_BST(int *a, int n)
{
    BST T = new BinNode(a[0]);
    BinNode * p = T;//p用于定位待插入位置
    int e;//新的data
    for(int k = 1; k < n; ++k){
        e = a[k];

        while(true){
            if(p->data > e){
                if(p->lc)
                    p = p->lc;
                else{
                    p->lc = new BinNode(e);
                    break;
                }
            }
            else if(p->data < e){
                if(p->rc)
                    p = p->rc;
                else{
                    p->rc = new BinNode(e);
                    break;
                }
            }
            else{//每个data互异,按照题意这个else永远不会被执行
                cout << "p->data == e\n";
                getchar();
            }
        }
        p = T;
    }
    return T;
}

bool Compare_BST(BST T1, BST T2)
{
    vector<int> v1_pre;
    vector<int> v2_pre;
    vector<int> v1_in;
    vector<int> v2_in;
    preorder(T1, v1_pre);
    preorder(T2, v2_pre);
    inorder(T1, v1_in);
    inorder(T2, v2_in);
    if(comp_vector(v1_pre, v2_pre) && comp_vector(v1_in, v2_in))
    //前序和中序确定唯一一棵树
        return true;
    else
        return false;
}
void preorder(BST bst,  vector<int> &vec_pre)
{
    if(bst){
        vec_pre.push_back(bst->data);
        preorder(bst->lc, vec_pre);
        preorder(bst->rc, vec_pre);
    }
}
void inorder(BST bst,  vector<int> &vec_in)
{
    if(bst){
        inorder(bst->lc, vec_in);
        vec_in.push_back(bst->data);
        inorder(bst->rc, vec_in);
    }
}
bool comp_vector(vector<int> v1, vector<int> v2)
{
    if(v1.size() != v2.size())
        return false;
    for(unsigned int i = 0; i < v1.size(); ++i)
        if(v1[i] != v2[i])
            return false;
    return true;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值