手记

基本的二叉树操作之:二叉树的创建

/**/
//abc空格空格de空格g空格空格f空格空格空格

/***/

include<stdio.h>
include<stdlib.h>

typedef
char
DataType;

typedef
struct
Node
{
DataType
Data;

struct 
Node 
*lchild,
*rchild;

}BinNode;

typedef
BinNode*
BinTree;

BinTree
CreateTree()
{
BinTree T;

char 
ch = getchar();

if(ch== ' ')
T =NULL;
else
{
    T=(BinTree)malloc(sizeof(BinTree));
    T->Data =ch;
    T->lchild =CreateTree();
    T->rchild =CreateTree();    
}

return T;
}

void
preOrder
(BinTree T)
{
if(T==NULL)
return ;

else
{
    printf("%c ",T->Data);
    preOrder(T->lchild);
    preOrder(T->rchild);
}

}

int main()
{
BinTree
T=CreateTree();
preOrder(T);
return 0;
}

0人推荐
随时随地看视频
慕课网APP