数据结构课设——BST二叉排序树

题目

编程实现二叉排序树的创建、插入、删除和查询
对于给定的这组数在二叉排序树上进行查找,给出两种情况下的查找成功和不成功时的ASL

代码

//BST
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;

typedef struct node
{
    int data;
    struct node *lchild, *rchild;
} BSnode, *BSTree;

int Search_BST(BSTree T, int key, BSTree f, BSTree *p)
{
    if (!T)
    {
        *p = f;
        return false;
    }
    else if (key == T->data)
    {
        *p = T;
        return true;
    }
    else if (key < T->data)
    {
        return Search_BST(T->lchild, key, T, p);
    }
    else
    {
        return Search_BST(T->rchild, key, T, p);
    }
}

int Insert_BST(BSTree *T, int key)
{
    BSTree p = NULL, s = NULL;
    if (!Search_BST(*T, key, NULL, &p))
    {
        s = new BSnode;
        s->data = key;
        s->lchild = s->rchild = NULL;
        if (!p)
            *T = s;
        else if (key < p->data)
            p->lchild = s;
        else
            p->rchild = s;
        return true;
    }
    return false;
}

int Delete(BSTree *p)
{
    BSTree q, s;
    if ((*p)->rchild == NULL)
    {
        q = *p;
        *p = (*p)->lchild;
        free(q);
    }
    else if ((*p)->lchild == NULL)
    {
        q = *p;
        *p = (*p)->rchild;
        free(q);
    }
    else
    {
        q = *p;
        s = (*p)->lchild;
        while (s->rchild)
        {
            q = s;
            s = s->rchild;
        }
        (*p)->data = s->data;
        if (q != *p)
            q->rchild = s->lchild;
        else
            q->lchild = s->lchild;
        free(s);
    }
    return true;
}

int Delete_BST(BSTree *T, int key)
{
    if (!*T)
        return false;
    else
    {
        if (key == (*T)->data)
        {
            return Delete(T);
        }
        else if (key < (*T)->data)
        {
            return Delete_BST(&(*T)->lchild, key);
        }
        else
        {
            return Delete_BST(&(*T)->rchild, key);
        }
    }
}

void Create_BST(BSTree *T, int a[], int n)
{
    for (int i = 0; i < n; i++)
        Insert_BST(T, a[i]);
}

void output_BST(BSTree T)
{
    if (T == NULL)
    {
        return;
    }
    output_BST(T->lchild);
    cout << T->data << " ";
    output_BST(T->rchild);
}

bool bfs(BSTree &T,int key)
{
    BSTree vis[MAXN];
    int fron = 1, rear = 1;
    vis[rear] = T;
    while (fron <= rear)
    {
        BSTree p = vis[fron++];
        if(p->data==key) return 1;
        if (p->lchild != NULL) vis[++rear] = p->lchild;
        if (p->rchild != NULL) vis[++rear] = p->rchild;
    }
    return 0;
}
int num=0;
int number(BSTree &T)
{
    if(T)
    {
        num++;
        number(T->lchild);
        number(T->rchild);
    }
    return num;
}
double ASL(BSTree &T,int key)
{
    if(bfs(T,key)==1) //能成功查找
    {
        int num=number(T); //得到树中节点的数量
        int d[MAXN];
        memset(d,-1,sizeof(d));
        BSTree vis[MAXN];
        int fron = 1, rear = 1;
        vis[rear] = T;
        d[T->data]=1;
        int res=1;
        while (fron <= rear)
        {
            BSTree p = vis[fron++];
            if (p->lchild != NULL)
            {
                vis[++rear] = p->lchild;
                d[p->lchild->data]=d[p->data]+1;
                res=res+d[p->lchild->data];
            }
            if (p->rchild != NULL)
            {
                vis[++rear] = p->rchild;
                d[p->rchild->data]=d[p->data]+1;
                res=res+d[p->rchild->data];
            }
        }
        return (res*1.0)/(num*1.0);
    }
    else //不能成功查找
    {
        int num=0;
        int d[MAXN];
        memset(d,-1,sizeof(d));
        BSTree vis[MAXN];
        int fron = 1, rear = 1;
        vis[rear] = T;
        d[T->data]=1;
        int res=0;
        while (fron <= rear)
        {
            BSTree p = vis[fron++];
            if (p->lchild != NULL)
            {
                vis[++rear] = p->lchild;
                d[p->lchild->data]=d[p->data]+1;
            }
            if (p->rchild != NULL)
            {
                vis[++rear] = p->rchild;
                d[p->rchild->data]=d[p->data]+1;
            }


            if( (p->lchild == NULL && p->rchild != NULL) || (p->lchild != NULL && p->rchild == NULL) )
            {
                num++;
                res=res+d[p->data];
            }
            if(p->lchild == NULL && p->rchild == NULL)
            {
                num=num+2;
                res=res+d[p->data]+d[p->data];
            }
        }
        return (res*1.0)/(num*1.0);
    }
}
int main()
{
    /*
    n=10
    62,88,58,47,35,73,51,99,37,93
    */

    cout << "Number of inputs: ";
    int n, x;
    cin >> n;
    int a[MAXN];
    cout << "Please enter:" << endl;
    for (int i = 0; i < n; i++)
        cin >> a[i];

    BSTree T = NULL;

    //创建
    Create_BST(&T, a, n);

    //插入 x=95
    cout << "Which number do you want to insert: ";
    cin >> x;
    Insert_BST(&T, x);

    //查找 x=95
    cout << "Which number do you want to find: ";
    cin >> x;
    BSTree p = NULL;
    if (!Search_BST(T, x, NULL, &p))
        cout << "Can't find!" << endl;
    else
    {
        cout << "The pointer to the search result is:\n"
             << p << endl;
    }

    //删除 x=88
    cout << "Which number to delete: ";
    cin >> x;
    Delete_BST(&T, x);
    cout << endl;

    //输出
    cout << "The output sequence is:" << endl;
    output_BST(T);
    cout << endl;

    //输出查找的ASL
    int key;
    cout<<"Please enter a band to find the element : ";
    cin>>key;
    cout<<ASL(T,key)<<endl;
    system("pause");
    return 0;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值