码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构大实习——二叉排序树

时间:2018-12-27 18:26:26      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:文件中   中序   \n   def   queue   windows   char   cin   data   

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <queue>

using namespace std;

typedef long long ll;

typedef struct node
{
    char no[20];
    double key;
    node *lt,*rt;
} bstnode;

bool flag=true;

void bstinsert(bstnode *&t,double key,char no[])//成绩插入
{
    if(t==NULL)
    {
        t=(bstnode *)malloc(sizeof(bstnode));
        t->key=key;
        strcpy(t->no,no);
        t->lt=t->rt=NULL;
    }
    else if(key<t->key)
    {
        bstinsert(t->lt,key,no);
    }
    else if(key>t->key)
    {
        bstinsert(t->rt,key,no);
    }
    return ;
}

void dfs(bstnode *&t)//中序递归遍历
{
    if(t==NULL)
        return ;
    if(t->lt!=NULL)
        dfs(t->lt);
    flag?printf("%s %lf",t->no,t->key):printf(" %s %lf",t->no,t->key);
    flag=false;
    if(t->rt!=NULL)
        dfs(t->rt);
    return ;
}

bool findkey(bstnode *&t,double key)//成绩查找
{
    if(t==NULL)
        return false;
    if(t->key==key)
        return true;
    if(t->key>key)
        if(findkey(t->lt,key))
            return true;
        else
            return false;
    else if(findkey(t->rt,key))
        return true;
    else
        return false;
}

void delkey(bstnode *&t,double key)//按成绩删除
{
    bstnode *f=t,*fa=t;
    while(f->key!=key)
    {
        if(f->key>key)
            fa=f,f=f->lt;
        if(f->key<key)
            fa=f,f=f->rt;
    }
    if(f->lt==NULL&&f->rt==NULL)
    {
        if(fa->rt==f)
            fa->rt=NULL;
        else
            fa->lt=NULL;
        if(f==t)t=NULL;
        free(f);
    }
    else if(f->lt==NULL)
    {
        if(fa->lt==f)
            fa->lt=f->rt;
        else
            fa->rt=f->rt;
        if(f==t)t=t->rt;
        free(f);
    }
    else if(f->rt==NULL)
    {
        if(fa->lt==f)
            fa->lt=f->lt;
        else
            fa->rt=f->lt;
        if(f==t)t=t->lt;
        free(f);
    }
    else
    {
        bstnode *tmp=f->lt,*pre=f;
        while(tmp->rt!=NULL)
            pre=tmp,tmp=tmp->rt;
        f->key=tmp->key;
        if(pre!=f)
            pre->rt=tmp->lt;
        else
            pre->lt=tmp->lt;
    }
    return ;
}



int menu()
{
    int type;
    system("cls");
    printf("1.插入学生数据\n");
    printf("2.查找学生\n");
    printf("3.删除学生数据\n");
    printf("4.显示二叉树的深度\n");
    printf("5.显示二叉树节点数和叶子节点数\n");
    printf("6.显示中序遍历序列\n");
    printf("7.从文件中读取\n");
    printf("8.退出\n");
    printf("输入上述序号:");
    cin>>type;
    return type;
}
int depth=0;
void DfsDeepth(bstnode *&t,int lev)//求深度
{
    if(t==NULL)return ;
    depth=max(depth,lev);
    DfsDeepth(t->lt,lev+1);
    DfsDeepth(t->rt,lev+1);
    return ;
}

int leaf=0,nodenum=0;
void DfsCount(bstnode *&t)
{
    nodenum++;
    if(t->lt==NULL&&t->rt==NULL)
        leaf++;
    if(t->lt!=NULL)
        DfsCount(t->lt);
    if(t->rt!=NULL)
        DfsCount(t->rt);
    return ;
}

void BfsWrite(bstnode *&t)
{
    FILE *fp;
    fp=fopen("data.txt","w");
    if(t==NULL)return ;
    queue<bstnode *> q;
    q.push(t);
    while(!q.empty())
    {
        bstnode *tmp=q.front();
        q.pop();
        fprintf(fp,"%s %lf\n",tmp->no,tmp->key);
        if(tmp->lt!=NULL)
            q.push(t->lt);
        if(tmp->rt!=NULL)
            q.push(t->rt);
    }
    return ;
}

void FileRead(bstnode *&t)
{
    FILE *fp=fopen("data.txt","r");
    char no[50];
    double key;
    while(fscanf(fp,"%s %lf",no,&key)!=EOF)
    {
        bstinsert(t,key,no);
    }
    return ;
}

void stop()
{
    cout<<"任意键继续"<<endl;
    getchar();
    getchar();
}

int main()
{
    bstnode *student=NULL;
    int type;
    bool exit=true;
    while((type=menu())!=8)
    {
        if(type>8)
        {
            printf("输入错误!");
            continue;
        }
        switch(type)
        {
            case 1:
            {
                printf("输入学生学号和成绩:");
                char no[20];
                double key;
                scanf("%s %lf",no,&key);
                bstinsert(student,key,no);
                break;
            }
            case 2:
            {
                printf("查找学生的成绩:");
                double key;
                scanf("%lf",&key);
                if(findkey(student,key))
                    printf("查找成功!\n");
                else
                    printf("查找失败!\n");
                stop();
                break;
            }
            case 3:
            {
                printf("删除学生的成绩:");
                double key;
                scanf("%lf",&key);
                if(findkey(student,key))
                    delkey(student,key),printf("删除成功!");
                else
                    printf("无该学生!");
                stop();
                break;
            }
            case 4:
            {
               depth=0;
               DfsDeepth(student,1);
               printf("二叉树深度为:%d\n",depth);
               stop();
               break;
            }
            case 5:
            {
                leaf=nodenum=0;
                DfsCount(student);
                printf("二叉树的叶子节点数为:%d,节点数为:%d\n",leaf,nodenum);
                stop();
                break;
            }
            case 6:
            {
                flag=true;
                dfs(student);
                stop();
                break;
            }
            case 7:
            {
                FileRead(student);
                printf("读入成功!");
                stop();
                break;
            }
            default :
                break;
        }
    }
    BfsWrite(student);
    return 0;
}

  

数据结构大实习——二叉排序树

标签:文件中   中序   \n   def   queue   windows   char   cin   data   

原文地址:https://www.cnblogs.com/wyhbadly/p/10185435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!