二叉树遍历 c语言实现,二叉树的建立和遍历c语言实现

41528d3028836879cd698677c3999917.gif二叉树的建立和遍历c语言实现

#include #include typedef struct node{ int data; struct node *ltree; struct node *rtree; }TNode; //ADT of the Binary tree TNode * create(TNode *); int depth(TNode *); int findmax(int,int); void postorder(TNode *); void inorder(TNode *); void preorder(TNode *); int main() {TNode *tree;printf(“BinaryTree (By Recursion)\n\n“);tree=create(tree);printf(“\npreorder:\n“);preorder(tree); printf(“\ninorder:\n“);inorder(tree);printf(“\npostorder:\n“);postorder(tree);printf(“\nThe depth o the binary tree is: %d.\n“,depth(tree));return 0;}//main TNode * create(TNode * p) {//创建一棵二叉树,返回根节点地址 int num;scanf(“%d“, if(num==0){ return NULL; }//if p=(TNode *)malloc(sizeof(TNode)); p->data=num;//把数据赋值给数据域 p->ltree=create(p->ltree);//递归算法,创建左子树 p->rtree=create(p->rtree); return p; }//create void preorder(TNode * p){ TNode* tree=p; if(tree){ printf(“%5d“,p->data); preorder(p->ltree); preorder(p->rtree); } }//preorder void inorder(TNode * p) { TNode* tree=p; if(tree){ inorder(p->ltree); printf(“%5d“,p->data); inorder(p->rtree); } }//inorder void postorder(TNode * p) { TNode* tree=p; if(tree){ postorder(p->ltree);postorder(p->rtree); printf(“%5d“,p->data); } }//postorder int findmax(int x,int y) { return (x>y?x:y); }//findmax int depth(TNode * p) { TNode * tree=p; int dep=0; int depl,depr; if(!p){ return 0; } depl=depth(p->ltree); depr=depth(p->rtree); dep=1+findmax(depl,depr); return dep; }//depth

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值