L3-016 二叉搜索树的结构

Posted zhanglichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了L3-016 二叉搜索树的结构相关的知识,希望对你有一定的参考价值。

按要求判断树的信息,考查对字符串的处理~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1014;
struct node {
    int data;
    node * left;
    node * right;    
};
void insert (node * &root,int v) {
    if (root==NULL) {
        root=new node;
        root->data=v;
        root->left=NULL;
        root->right=NULL;
        return; 
    }
    if (v<root->data) insert (root->left,v);
    else insert (root->right,v);
}
int isFull=1,maxdepth=-1;
unordered_map<int,int> bro,l,r,father,depth,pos;
void bfs (node * root) {
    queue<node *> q;
    q.push(root);
    depth[root->data]=0;
    //maxdepth=max (depth[root->data],maxdepth);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        //if (!now->left||!now->right) isFull=0;
        if ((!now->left&&now->right)||(now->left&&!now->right)) isFull=0;
        if (now->left&&now->right) {
            bro[now->left->data]=now->right->data;
            bro[now->right->data]=now->left->data;
        }
        if (now->left) {
            q.push(now->left);
            depth[now->left->data]=depth[now->data]+1;
            //maxdepth=max (depth[now->left->data],maxdepth);
            l[now->data]=now->left->data;
            father[now->left->data]=now->data;
        }
        if (now->right) {
            q.push(now->right);
            depth[now->right->data]=depth[now->data]+1;
            r[now->data]=now->right->data;
            father[now->right->data]=now->data;
            //maxdepth=max (depth[now->right->data],maxdepth);
        }
    }
}
int main () {
    int N,x;
    scanf ("%d",&N);
    node * root=NULL;
    for (int i=0;i<N;i++) {
        scanf ("%d",&x);
        insert (root,x);
        pos[x]=1;
    }
    bfs (root);
    //if (N<pow(2,maxdepth)-1) isFull=0;
    int q;
    scanf ("%d",&q);
    string s;
    getchar ();
    for (int i=0;i<q;i++) {
        getline (cin,s);
        //cout<<s.substr(s.length()-1-4,4)<<endl;
        if (s.substr(s.length()-4,4)=="root") {
            int rootnum=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) rootnum=rootnum*10+s[j]-0;
                else break;
            }
            if (root->data==rootnum) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.substr(s.length()-8,8)=="siblings") {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) num[cnt]=num[cnt]*10+s[j]-0;
                else if (s[j-1]>=0&&s[j-1]<=9) cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No
"); 
            else if (bro[num[0]]==num[1]) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.substr(s.length()-4,4)=="tree") {
            if (isFull==1) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.substr(s.length()-5,5)=="level") {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) num[cnt]=num[cnt]*10+s[j]-0;
                else if (s[j-1]>=0&&s[j-1]<=9) cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No
"); 
            else if (depth[num[0]]==depth[num[1]]) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.find("left")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) num[cnt]=num[cnt]*10+s[j]-0;
                else if (s[j-1]>=0&&s[j-1]<=9) cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No
"); 
            else if (l[num[1]]==num[0]) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.find("right")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) num[cnt]=num[cnt]*10+s[j]-0;
                else if (s[j-1]>=0&&s[j-1]<=9) cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No
");
            else if (r[num[1]]==num[0]) printf ("Yes
");
            else printf ("No
");
        }
        else if (s.find("parent")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>=0&&s[j]<=9) num[cnt]=num[cnt]*10+s[j]-0;
                else if (s[j-1]>=0&&s[j-1]<=9) cnt++;
            }
            /*if (pos[num[0]]==0||pos[num[1]]==0) printf ("No
");
            else*/ if (father[num[1]]==num[0]) printf ("Yes
");
            else printf ("No
");
        }
    }
    return 0;
}

 

以上是关于L3-016 二叉搜索树的结构的主要内容,如果未能解决你的问题,请参考以下文章

L3-016

数据结构树相关代码(数据结构笔试复测Leecode牛客)

高阶数据结构 | 二叉搜索树(Binary Search Tree)

数据结构之二叉搜索树详解

❤️数据结构入门❤️(2 - 1)- 二叉搜索树

数据结构进阶:二叉搜索树