二叉树的创建、先序、中序、后序遍历

#include <stdio.h>
#include <stdlib.h>


#define ERROR 0
#define OK 1


typedef struct bitree
{
    int data;
    struct bitree *lchild;
    struct bitree *rchild;
}bitree_s,*pbitree;


//以先序方式,创建一颗二叉树;
int creat_tree(pbitree *T,int j,int* i)
{
    (*T) = (bitree_s *)malloc(sizeof(bitree_s));
    if(!(*T)) return ERROR;
    (*T)->data = (*i);
    printf("%d\n",((*T)->data));
    (*T)->lchild = NULL;
    (*T)->rchild = NULL;
    (*i) += 1;
    j -= 1;
    if(j==0)
    {
        return OK;
    }
    creat_tree(&((*T)->lchild),j,i);
    creat_tree(&((*T)->rchild),j,i);
}
//以先序进行遍历
int preorder(pbitree T)
{
    if(!T) return ERROR;
    printf("%d\n",T->data);
    preorder(T->lchild);
    preorder(T->rchild);
}
//以中序进行遍历
int minorder(pbitree T)
{
    if(!T) return ERROR;
    minorder(T->lchild);
    printf("%d\n",T->data);
    minorder(T->rchild);


}
//以后序进行遍历
int lastorder(pbitree T)
{
    if(!T) return ERROR;
    lastorder(T->rchild);
    printf("%d\n",T->data);
    lastorder(T->lchild);


}


int main()
{
    int count = 1;
    int deep = 3;
    pbitree tree;
    printf("hello world\n");
    creat_tree(&tree,deep,&count);
    printf("pre order transvers\n");
    preorder(tree);
    printf("min order transvers\n");
    minorder(tree);
    printf("last order transvers\n");
    lastorder(tree);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值