数据结构树(二叉树的使用)

#include<stdio.h>
#include<stdlib.h>
#define maxSize 127
typedef char TElemType;
typedef struct node{
    TElemType data[maxSize];       //结点数据 
    struct node *lchild,*rchild;   //左右子女指针 
    int length;
}BinTNode, *BinTree;    //二叉树定义 
//创建二叉树
void createBinTree_Pre(BinTNode *T){
    TElemType pre;
    int n;
    while(pre==getchar()!=';'){
        if(pre != '#'){
            T = (BinTNode*) malloc (sizeof(BinTNode));   //递归根节点
            T->data[n]=pre;
            createBinTree_Pre(T->lchild);     //递归建立左子树
            createBinTree_Pre(T->rchild);     //递归建立右子树 
            n++;
        }else 
            T = NULL;     //否则建立空子树 
    }
    T->length=n;

};
//以广义表的形式输出二叉树
void PrintBinTree (BinTNode *T){
    if(T!=NULL){
        printf("%c",T->data);
        if(T->lchild!=NULL||T->rchild!=NULL){
            printf("(");
            PrintBinTree(T->lchild);
            printf(",");
            PrintBinTree(T->rchild);
            printf(")");
        }
    }
} 
main(){
    BinTNode T;
    createBinTree_Pre(&T);
    PrintBinTree(&T);
}

有点小自闭,输出的时候出现错误

猜你喜欢

转载自www.cnblogs.com/lvzhiqi/p/10934690.html