码迷,mamicode.com
首页 > 其他好文 > 详细

7-20 Binary Search Tree (25分)

时间:2020-02-07 10:53:55      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:line   job   字典   sample   tin   process   sse   cat   can   

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

用数组建树,模仿字典树的建树过程这样结点都在数组中连续,然后对于测试序列,从根节点开始比较如果当前结点标记过了,则用测试结点与之比较确定往左还是往右比较,如果没标记过,直接比较是否相等。
代码:
#include <cstdio>
#include <cstring>
using namespace std;

int trie[11][2],v[11],vis[11],ind;
void insert(int d) {
    if(ind == 0) {
        v[ind ++] = d; 
    }
    else {
        int x = 0;
        while(v[x] != d) {
            if(d < v[x]) {
                if(!trie[x][0]) {
                    trie[x][0] = ind;
                    v[ind ++] = d;
                }
                x = trie[x][0];
            }
            else {
                if(!trie[x][1]) {
                    trie[x][1] = ind;
                    v[ind ++] = d;
                }
                x = trie[x][1];
            }
        }
    }
}
bool check(int x,int d) {
    if(!vis[x]) {
        if(v[x] == d) {
            vis[x] = 1;
            return true;
        }
        return false;
    }
    return d < v[x] ? check(trie[x][0],d) : check(trie[x][1],d);
}
int main() {
    int n,l,d,flag;
    while(scanf("%d",&n) && n) {
        scanf("%d",&l);
        ind = 0;
        memset(trie,0,sizeof(trie));
        for(int i = 0;i < n;i ++) {
            scanf("%d",&d);
            insert(d);
        }
        for(int i = 0;i < l;i ++) {
            flag = 1;
            memset(vis,0,sizeof(vis));
            for(int j = 0;j < n;j ++) {
                scanf("%d",&d);
                flag &= check(0,d);
            }
            puts(flag ? "Yes" : "No");
        }
    }
}

 

7-20 Binary Search Tree (25分)

标签:line   job   字典   sample   tin   process   sse   cat   can   

原文地址:https://www.cnblogs.com/8023spz/p/12271964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!