二叉排序树学习入门

本文介绍了二叉排序树的基本概念,并通过解决HDU的三道题目——The order of a Tree HDU3791、The order of a Tree HDU3999、Elven Postman HDU5444,探讨了如何构建二叉排序树、判断其是否相同、寻找节点位置以及检查是否为完全二叉树的方法。在完全二叉树的判断中,利用了BFS遍历策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


PS:今天刚刚上了一堂关于二叉排序树的数据结构,把以前不会的二叉排序树的题补一下;

The order of a Tree   hdu3791

题意:根据插入的顺序进行构建二叉排序树,判断二叉排序树是否相同

思路:建树,比较,都是入门;

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
struct Node
{
    Node *lchild,*rchild;
    int data;
};
void inserts(Node *(&t),int x)//因为没有返回值,所以要对他进行改变就得用&,刚开始没用这个,调了好久才发现了时是这个建树的过程;
{//寻找并进行操作的过程
    if(t == NULL)
    {
        t = new Node ;
        t -> data = x;
        t -> lchild = t -> rchild = NULL;
    }
    else
    {
        if(x < t -> data)
             inserts(t -> lchild ,x);
        else inserts(t -> rchild,x);
    }
}
void Creat(Node *tree, char s[])
{
    tree -> data= s[0] - '0';
    tree -> lchild = tree -> rchild = NULL;
    int len = strlen(s);
    for(int i = 1; i < len ; i ++)
    {
        inserts(tree,s[i] - '0');
    }
}
bool ans ;
void cmp(Node *t1,Node *t2)//两棵树的比较操作,注意当指针为空的时候就不能对它的值进行操作,不然就会内存泄露,RE
{
    if(t1 == NULL && t2 == NULL)
        return ;
    if(t1 == NULL || t2 == NULL)
    {
        ans = false;
        return ;
    }
    if(t1 -> data != t2 -> data)
    {
//        cout << t1 -> data << endl;
        ans = false;
        return ;
    }
    cmp(t1 -> lchild, t2 -> lchild);
    cmp(t1 -> rchild, t2 -> rchild);
}
//void pre_print(Node *t)
//{
//    if(t == NULL)
//        return;
//    cout << t -> data;
//    pre_print(t -> lchild);
//
//    pre_print(t -> rchild);
//}
int main()
{
    int n;
    while( ~ scanf("%d",&n) && n)
    {
        char s[maxn];
        scanf("%s",s);
        Node *tree;
        tree = new Node;
        Creat(tree,s);
//        pre_print(tree);
//        cout <<endl;
        for(int i = 1;i <= n ; i ++)
        {
            ans = true;
            char ss[maxn];
            scanf("%s",ss);
            Node *trees;
            trees = new Node;
            Creat(trees,ss);
            cmp(tree,trees);
            if(ans)
                cout << "YES"<< endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}


The order of a Tree hdu3999

题意思路:建一棵二叉排序树,输出前序排序;

为了实现空格和换行,就用了一下队列;


#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Node
{
    Node *lchild,*rchild;
    int data;
};
int n;
int a[maxn];
void inserts(Node *(&t),int x)
{
    if(t == NULL)
    {
        t = new Node ;
        t -> data = x;
        t -> lchild = t -> rchild = NULL;
    }
    else
    {
        if(x < t -> data)
             inserts(t -> lchild ,x);
        else inserts(t -> rchild,x);
    }
}
void Creat(Node *tree, int a[])
{
    tree -> data= a[1];
    tree -> lchild = tree -> rchild = NULL;
    for(int i = 2; i <= n ; i ++)
    {
        inserts(tree,a[i]);
    }
}
queue<int>q;
void pre_print(Node *t)
{
    if(t == NULL)
        return;
    q.push(t -> data);
    pre_print(t -> lchild);
    pre_print(t -> rchild);
}
void Print()
{
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        if(q.empty())
            cout << t << endl;
        else cout << t << " ";
    }
}
int main()
{
    while( ~ scanf("%d",&n))
    {
        for(int i = 1; i <= n ; i ++)
            scanf("%d",&a[i]);
        Node *tree;
        tree = new Node;
        Creat(tree,a);
        pre_print(tree);
        Print();
    }
    return 0;
}


Elven Postman   hdu5444


题意:给出一个二叉排序树的插入顺序,建树,找出它的位置;

思路:建树,每个节点记录它的位置,之后查找输出;


include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int a[maxn];
int n;
struct Node
{
    int data;
    string s;
    Node *lchild,*rchild;
};
void inserts(Node *pre_t,Node *(&t),int x)
{
    if(t == NULL)
    {
        t = new Node;
        if(x < pre_t -> data)
        {
            t -> s = pre_t -> s + 'E';
        }
        else t -> s = pre_t -> s + 'W';
        t -> data = x;
        t -> lchild = t -> rchild = NULL;
    }
    if(x == t -> data)
        return ;//ÎTDè2åèë
    if(x < t -> data)
    {
        inserts(t,t -> lchild,x);
    }
    else inserts(t,t -> rchild,x);
}
void Creat(Node *tree,int a[])
{
    tree -> data = a[1];
    tree -> lchild = tree -> rchild = NULL;
    for(int i = 2; i <= n ; i ++)
    {
        inserts(tree,tree,a[i]);
    }
}
void Print(Node *t,int x)
{
    if(t == NULL)
        return ;
    if(t -> data == x)
    {
        cout << t -> s << endl;
    }
    if(x < t -> data)
    Print(t -> lchild,x);
    else Print(t -> rchild,x);
}
int main()
{
    int Tcase;
    scanf("%d",&Tcase);
    for(int ii = 1; ii <= Tcase ; ii ++)
    {
        scanf("%d",&n);
        for(int i = 1; i <= n ; i ++)
            scanf("%d",&a[i]);
        Node *tree;
        tree = new Node;
        Creat(tree,a);
        int m;
        scanf("%d",&m);
        for(int i = 1; i <= m ; i ++)
        {
            int x;
            scanf("%d",&x);
            Print(tree,x);
        }
    }
    return 0;
}

L3-010. 是否完全二叉搜索树


题意:给出一段插入顺序,建立一颗二叉搜索树,要求左 > 根 > 右,之后判断他是否是完全二叉树,输出它的层次遍历顺序;

思路:建树的话就按照二叉排序树那样建树就行了,完全二叉树的判断方法是用BFS把所有的节点包括根节点一起压到队列中哦那个,进行遍历,当找到一个空的元素的时候就索说明到了最后一层,判断这个空节点之后还有非空的节点吗,如果没有,就是完全二叉树,否者 ,不是;


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int a[maxn];
int n;
struct Node
{
    int data;
    Node *lchild,*rchild;
};
void inserts(Node *(&t),int x)
{
   if(t == NULL)
   {
       t = new Node;
       t -> data = x;
       t -> lchild = t -> rchild = NULL;
       return ;
   }
   if(x == t -> data)
    return ;
   if(x > t -> data)
   {
       inserts(t -> lchild,x);
   }
   else inserts(t -> rchild,x);
}
void Creat(Node *tree,int a[])
{
    tree -> data = a[1];
    tree -> lchild = tree -> rchild = NULL;
    for(int i = 2; i <= n ; i ++)
    {
        inserts(tree,a[i]);
    }
}
bool flag ;
void Print(Node *tree)
{
    queue<Node*>q,ans;
    q.push(tree);
    while(!q.empty())
    {
        Node *temp = q.front();
        ans.push(temp);
        q.pop();
        if(temp)
        q.push(temp -> lchild);
        if(temp)
            q.push(temp -> rchild);
    }

    queue<Node*>Q;
    while(!ans.empty())
    {
        Node *temp = ans.front();
        if(temp == NULL)
            break;
        ans.pop();
        Q.push(temp);
    }
    while(!ans.empty())
    {
        Node *temp = ans.front();
        ans.pop();
        if(temp != NULL)
        {
            flag = false;
            Q.push(temp);
        }
    }
    while(!Q.empty())
    {
        Node *temp = Q.front();
        Q.pop();
        if(Q.empty())
            cout << temp -> data << endl;
        else cout << temp -> data << " ";
    }
}
int main()
{
    while( ~ scanf("%d",&n) )
    {

        for(int i = 1; i <= n ; i ++)
            scanf("%d",&a[i]);
        Node *tree;
        tree = new Node;
        Creat(tree,a);
        flag = true;
        Print(tree);
        if(flag)
            cout << "YES"<<endl;
        else cout << "NO" << endl;
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值