zoukankan      html  css  js  c++  java
  • 二叉排序树

    数据结构试验:

    /*
    已知,二叉树存储结构定义见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

  • 相关阅读:
    Uva 10935 Throwing cards away I
    Uva 3226 Symmetry
    eclipse @ 注释为何一写就报错
    2015省赛小感想
    Zoj 3842 Beauty of Array
    fedora 设置命令别名
    Uva220 Othello
    工作小技巧积累
    SSL介绍与Java实例
    一个完整的SSL连接建立过程
  • 原文地址:https://www.cnblogs.com/twodog/p/12141528.html
Copyright © 2011-2022 走看看