算法导论学习笔记——12.1二叉搜索树

在这里插入图片描述
在这里插入图片描述

map<int,int>mp[maxn];
void insert(int idx) {
    if(!num[idx]) {
        num[idx] = m;
        mp[m]=idx;
        return ;
    }
    if(m > num[idx]) insert(idx * 2);
    else insert(idx * 2 + 1);
}

在这里插入图片描述

void search(int rt,int x)
{
	if(rt==-1||x==mp[rt])
		return rt;
	if(x<mp[rt])
		return search(rt*2,x);
	else
		return search(rt*2+1,x);
}

总代码(pta天梯赛二叉搜索树例题)

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 220;
int BST[maxn];
int n,m;
typedef struct TNode
{
    int data;
    struct TNode *left,*right;
}TNode,*BiTree;
void buildtree(BiTree &T,int x)//建树
{
    if(T==NULL)
    {
        T=(BiTree)malloc(sizeof(BiTree));
        T->data=x;
        T->left=NULL;
        T->right=NULL;
    }
    else if(x<T->data)
    {
        buildtree(T->left,x) ;
    }
    else if(x>T->data)
    {
        buildtree(T->right,x);
    }
}
bool Search_BST(BiTree &T,int x)
{//查找关键字k并返回其指针
    if(T==NULL||T->data==x) return true;
    if(T->data>x) return Search_BST(T->left,x);
    else return Search_BST(T->right,x);
}
int Get_value(BiTree &T)
{
    if(T==NULL) return -1;
    return T->data;
}
bool Judge_Parent(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==x)
    {
        if(Get_value(T->left)==y||Get_value(T->right)==y)
            return true;
    }
    return (Judge_Parent(T->left,x,y)||Judge_Parent(T->right,x,y));
}
bool  Judge_LChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->left)==x)
            return true;
    }
    return (Judge_LChild(T->left,x,y)||Judge_LChild(T->right,x,y));
}
bool Judge_RChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->right)==x)
            return true;
    }
    return (Judge_RChild(T->left,x,y)||Judge_RChild(T->right,x,y));
}
void Height(BiTree& T,int e,int h,int &c)
{
    if(T==NULL) return;
    if(T->data==e) c=h;
    Height(T->left,e,h+1,c);
    Height(T->right,e,h+1,c);
}
bool Judge_Same(BiTree T,int x,int y)
{
    int f1 , f2;
    Height(T,x,0,f1);
    Height(T,y,0,f2);
    if(f1==f2) return true;
    return false;
}
bool Judge_Bother(BiTree T,int x,int y)
{
    if(T==NULL) return false;
    if(T->left!=NULL&&T->right!=NULL)
    {
        if(T->left->data==x&&T->right->data==y) return true;
        if(T->left->data==x&&T->right->data==y) return true;
    }
    Judge_Bother(T->left,x,y);
    Judge_Bother(T->right,x,y);
}
bool finds(int x)
{
    for(int i=0;i<n;i++)
        if(x==BST[i]) return true;
    return false;
}
int main()
{
    scanf("%d",&n);
    BiTree T=NULL;
    for(int i=0;i<n;i++) scanf("%d",&BST[i]);
    for(int i=0;i<n;i++) buildtree(T,BST[i]);
    scanf("%d",&m);
    int _a,_b,_c;
    string a,b,c;
    while(m--)
    {
        scanf("%d",&_a);
        cin>>a;
        if(a=="is")
        {
            cin>>a>>b;
            if(b=="parent")
            {
                cin>>c>>_b;
                if(Judge_Parent(T,_a,_b)&&finds(_a)&&finds(_b))
                    printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="left")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_LChild(T,_a,_b)&&finds(_a)&&finds(_b))
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="right")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_RChild(T,_a,_b)&&finds(_a)&&finds(_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="root")
            {
                if(T!=NULL&&T->data==_a&&finds(_a))
                    printf("Yes\n");
                else
                    printf("No\n");
                continue;
            }
        }
        else if(a=="and")
        {
            cin>>_b;
            cin>>b>>c;
            if(c=="siblings")
            {
                if(Judge_Bother(T,_a,_b)&&finds(_a)&&finds(_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else
            {
                getline(cin,b);
                if(Judge_Same(T,_a,_b)&&finds(_a)&&finds(_b)) // 层数相同
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值