结构体采用链式存储:
typedef struct BiTreeNode{
int data;
struct BiTreeNode *lchild,*rchild;
}BiTreeNode,*BiTree;
二叉树的递归创建C语言:
void CreateBiTree(BiTree bt)
{ char ch;
ch=getchar();//头文件stdio.h
if(ch=='*')
bt=NULL;
else {
bt=(BiTreeNode*)malloc(sizeof(BiTreeNode));
bt->data=ch;
CreateBiTree(bt->lchild);
CreateBiTree( bt->rchild);
}
}
二叉树的递归创建C++语言:
void CreateBiTree(BiTree &T)//先序建立一棵二叉树
{
lElemType ch;
cin >> ch;
if (ch == '*') T = NULL;//输入*表示二叉树的该节点为空
else {
if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
exit(-1);