二叉排序树

数据结构试验:

/*
已知,二叉树存储结构定义见bstree.h,请编写一个算法函数bstree creatBstree(int a[],int n),
以数组a中的数据作为输入建立一棵二叉排序树,并将建立的二叉排序树进行中序遍历。
(提示,a中的原始数据可从data1.txt中读入,实验代码详见lab9_05.c)

*/

#include "Arrayio.h"
#include "bstree.h"
#define N 100


bstree insertBST(bstree tree,int key)
{
    if(tree==NULL)
    {
        tree = (bstree)malloc(sizeof(bsnode));
        tree ->key = key;
        tree ->lchild = tree ->rchild =  NULL;
    }

    else
    {
        if(key>tree->key)
        {
            tree ->rchild = insertBST(tree->rchild,key);
        }
        else
        {
            tree ->lchild = insertBST(tree->lchild,key);
        }
    }
    return tree;
}

bstree  creatBstree(int a[],int n)
{
    /*根据输入的结点序列,建立一棵二叉排序树,并返回根结点的地址*/
    bstree tree = NULL;
    for(int i=0; i<n; i++)
        tree = insertBST(tree,a[i]);

    return tree;
}


int  main()
{
    int n,a[N];
    bstree p,t;
    n=readData(a,N,"data1.txt");
    output(a,n);
    t=creatBstree(a,n);         /*创建二叉排序树*/
    printf("中序遍历:\n");
    inorder(t);                 /*中序遍历二叉排序树*/
    return 0;
}

 

转载于:https://www.cnblogs.com/TreeDream/p/6185643.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值