数据结构——树1

来源:互联网 发布:免费一级域名注册 编辑:程序博客网 时间:2024/06/16 13:34
#include <stdio.h>#include <stdlib.h>#include <th01.h>struct SNode{    int Element;    STree Left;    STree Right;};int main(){    STree T=NULL;    int i;    for(i=0;i<2;i++)    {<span style="color:#ff0000;">        //Insert(i+1,T);错误,此时T并未改变        T=Insert(i+1,T);</span>        if(T==NULL)            printf("Error");    }    PrintTree(T);    return 0;}//好像没用,Insert()就包括了NULL的情形//STree CreateTree()//{//    STree T;//    T=malloc(sizeof(struct SNode));//    T->Element=0;//?//    T->Left=T->Right=NULL;////    return T;//}STree Insert(int X,STree T){    if(T==NULL)    {        T=malloc(sizeof(struct SNode));        T->Element=X;        T->Left=T->Right=NULL;    }    else if(X<T->Element)    {        T->Left=Insert(X,T->Left);//不是构造AVL树,所以不用旋转    }    else if(X>T->Element)        T->Right=Insert(X,T->Right);    return T;}void PrintTree(STree T){    if(T!=NULL)    {        printf("%-4d",T->Element);        PrintTree(T->Left);        PrintTree(T->Right);    }}


0 0
原创粉丝点击