二叉树的建立和先序遍历
#include<stdio.h>#include<malloc.h>
typedef struct bitrees
{
int data; //数据域
struct bitrees *leftnode,*parent;
struct bitrees *rightnode;
}bitnode,*bitree;
bitree bitree_preordercreat(bitree root ) //构造二叉树的函数
{
int a;
scanf_s("%d",&a);
if(!a)
{
root=NULL;
}
else
{
root=(bitree)malloc(sizeof(bitnode));
root->data=a;
bitree_preordercreat(root->leftnode);
bitree_preordercreat(root->rightnode);
}
return root;
}
void bitree_preordervisit(bitree root) //先序遍历二叉树的函数
{
if(root!=NULL)
{
printf("%d ",root->data);
bitree_preordervisit(root->leftnode);
bitree_preordervisit(root->rightnode);
}
}
void main()
{
bitnode q;
bitree root,r;
root=&q;
printf("请输入你想输入的数字构成树:");
root=bitree_preordercreat(root); //构造二叉树.
bitree_preordervisit(root); //先序遍历二叉树;
}