• 二叉排序树


    数据结构试验:

    /*
    已知,二叉树存储结构定义见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("中序遍历:
    ");
        inorder(t);                 /*中序遍历二叉排序树*/
        return 0;
    }

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

  • 相关阅读:
    word上怎么打钩
    POI操作excel常用方法总结
    web.xml中Filter过滤器标签说明
    IDEA使用操作说明(自己总结)
    windows 64位上oracle 11g安装
    List<T>与List<?>的区别
    java分页之假分页
    CDN之Web Cache
    HTTP之缓存技术
    HTTP之Cookie和Session
  • 原文地址:https://www.cnblogs.com/twodog/p/12141527.html
Copyright © 2020-2023  润新知