第一次亲自写博文,也好久没有着手写些程序了。以此,一来激励自己,二者与大家共勉。
#include
#include
typedef struct BiNode
{
int data;
struct BiNode *left;
struct BiNode *right;
}BiNode;
typedef BiNode *BiTree;
void initBiTree(BiTree *T);
void createBiTree(BiTree *T);
void preOrderTraverseBiTree(BiTree T);
void inOrderTraverseBiTree(BiTree T);
void destroyBiTree(BiTree *T);
int main()
{
BiTree T;
printf("init your Binary Tree...\n");
initBiTree(&T);
printf("Preorder Create your Binary Tree...\n");
createBiTree(&T);
printf("preOrderTraverseBiTree...\n");
preOrderTraverseBiTree(T);
printf("inOrderTraverseBiTree...\n");
inOrderTraverseBiTree(T);
printf("destroyBiTree...\n");
destroyBiTree(&T);
return 0;
}
void initBiTree(BiTree *T)
{
BiNode *Root;
Root =(BiNode *)malloc(sizeof(BiNode));
if(!Root)
{
printf("init Root error!\n");
return;
}
*T = Root;
}
void createBiTree(BiTree *T)
{
int data;
BiNode *Root;
printf("input your number and -1 for NULL:");
scanf("%d",&data);
if(data == -1)
{
return;
}
else
{
Root =(BiNode *)malloc(sizeof(BiNode));
if(!Root)
{
printf("malloc BiNode error!\n");
return;
}
Root->data = data;
*T = Root;
printf("%d\n",Root->data);
createBiTree(&(Root->left));
createBiTree(&(Root->right));
}
}
void preOrderTraverseBiTree(BiTree T)
{
if(T)
{
printf("%d\n",T->data);
preOrderTraverseBiTree(T->left);
preOrderTraverseBiTree(T->right);
}
}
void inOrderTraverseBiTree(BiTree T)
{
if(T)
{
inOrderTraverseBiTree(T->left);
printf("%d\n",T->data);
inOrderTraverseBiTree(T->right);
}
}
void destroyBiTree(BiTree *T)
{
if(*T)
{
destroyBiTree(&((*T)->left));
destroyBiTree(&((*T)->right));
free(*T);
}
}
阅读(1419) | 评论(0) | 转发(0) |