二叉树的实现

来源:互联网 发布:淘宝上的色情杂志 编辑:程序博客网 时间:2024/04/28 07:22

#include<stdio.h>
#include<malloc.h>

typedef struct bintree
{
 int data;
 struct bintree *lchild,*rchild;
}BTN,*BTP;
//按先序创建一个二叉树
BTP creatbt()
{
 BTP bt;int n;
 scanf("%d",&n);
 if(n)
 {
  bt=(BTP)malloc(sizeof(BTN));
  bt->data=n;
  bt->lchild=creatbt();
  bt->rchild=creatbt();
 }
 else
  bt=NULL;
 return bt;
}
//先序遍历
void preorder(BTP bt)
{
 if(bt)
 {
  printf("%4d",bt->data);
  preorder(bt->lchild);
  preorder(bt->rchild);
 }
}

void main()
{
 BTP bt;
 bt=creatbt();
 preorder(bt);
}