#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct BiNode
{
int data;
struct BiNode *lchild, *rchild;
}BiNode,*BiTree;
BiNode *BiNode_Create()
{
BiTree tmp = NULL;
char c;
printf("请输入符号:");
scanf("%c", &c);
if (c == '#')
{
return NULL;
}
else
{
tmp = (BiNode*)malloc(sizeof(BiNode));
if (tmp == NULL)
{
return NULL;
}
tmp->data = (int)c;//创建根节点
tmp->lchild = BiNode_Create();//创建左子树
tmp->rchild = BiNode_Create();//创建右子树
return tmp;
}
}
void preOrder(BiNode *root)
{
if (root != NULL)
{
printf("%c ", root->data);
preOrder(root->lchild);
preOrder(root->rchild);
}
}
void BiNode_Free(BiNode *root)//释放树
{
if (root->rchild != NULL)
{
BiNode_Free(root->rchild);
}
if (root->lchild != NULL)
{
BiNode_Free(root->lchild);
}
if (root!= NULL)
{
free(root);
root == NULL;
}
}
void main()
{
BiTree t;
t = BiNode_Create();
preOrder(t);
BiNode_Free(t);
}
#号法创建树
最新推荐文章于 2022-05-16 09:48:31 发布