nyoj 1278G: Prototypes analyze 与 二叉排序树(BST)模板

参考博客:https://blog.csdn.net/stpeace/article/details/9067029
参考博客:https://blog.csdn.net/baidu_35643793/article/details/70792326

先放上二叉搜索树的板子

#include <iostream>
using namespace std;
 
// BST的结点
typedef struct node
{
    int key;
    struct node *lChild, *rChild;
}Node, *BST;
 
// 在给定的BST中插入结点,其数据域为element, 使之称为新的BST
bool BSTInsert(Node * &p, int element)
{
    if(NULL == p) // 空树
    {
        p = new Node;
        p->key = element;
        p->lChild = p->rChild = NULL;
        return true;
    }
 
    if(element == p->key) // BST中不能有相等的值
        return false;
 
    if(element < p->key)  // 递归
        return BSTInsert(p->lChild, element);
 
    return BSTInsert(p->rChild, element); // 递归
}
 
// 建立BST
void createBST(Node * &T, int a[], int n)
{
    T = NULL; 
    int i;
    for(i = 0; i < n; i++)
    {
        BSTInsert(T, a[i]);
    }
}
 
// 先序遍历
void preOrderTraverse(BST T)
{
    if(T)
    {
        cout << T->key << " ";
        preOrderTraverse(T->lChild);
        preOrderTraverse(T->rChild);
    }
}
 
// 中序遍历
void inOrderTraverse(BST T)
{
    if(T)
    {
        inOrderTraverse(T->lChild);
        cout << T->key << " ";
        inOrderTraverse(T->rChild);
    }
}
 
int main()
{
    int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
    int n = 10;
    BST T;
 
    // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
    createBST(T, a, n);
 
    preOrderTraverse(T);
    cout << endl;
 
    inOrderTraverse(T);
    cout << endl;
 
    return 0;
}

nyoj 1278

这道题 题意就是 判断二叉排序树的形状有多少个不一样。
首先 建树 就用上面的板子。
然后就是 判断形状:只需要各个位置是否对应一致有值就行了(如果一颗树在这个地方是空的,那么另一颗树要想与它形状相同,这个地方也必须是空的)

//BST二叉搜索树;
#include<cstdio>
#include<iostream>
using namespace std;
 
int flag;
 
typedef struct Node
{
    int key;
    struct Node *lChild,*rChild;
} Node,*BST;
 
bool BSTInsert(Node * &p,int element)
{
    if(p==NULL)
    {
        p=new Node;
        p->key=element;
        p->lChild=p->rChild=NULL;
        return true;
    }
    if(element==p->key)
        return false;
    if(element<p->key)
        return BSTInsert(p->lChild,element);
    return BSTInsert(p->rChild,element);
}
 
void judge(BST T1,BST T2)  //判断形状;
{
    if(T1==NULL&&T2==NULL)
        return;
    else if(T1&&T2)
    {
        judge(T1->lChild,T2->lChild);
        judge(T1->rChild,T2->rChild);
    }
    else
        flag=0;
}
 
int main()
{
    int t,n,k,x;
    BST tree[55];
 
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i=0; i<n; i++)  
        {
            BST T=NULL; 
            for(int j=0; j<k; j++)  //建树;
            {
                scanf("%d",&x);
                BSTInsert(T,x);
            }
            tree[i]=T;
        }
 
        //找形状种类数;
        int ans=0;
        for(int i=0; i<n; i++)
        {
            int flog=1;
            for(int j=i+1; j<n; j++)
            {
                flag=1;
                judge(tree[i],tree[j]);
                if(flag)
                {
                    flog=0;
                    break;
                }
            }
            if(flog)
                ++ans;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fisherss/p/10801215.html