c语言后序方式建树,数据结构建树程序,C语言C++皆可?

c8cd64f6466d544a0f763fec6ea16e6c.png

慕虎7371278

#include #include #include#define maxnode 100#define NULL 0typedef char DataType; /*设数据域类型为字符型*/typedef struct node{DataType data;struct node *lchild,*rchild; /*左右链域为指向结点结构的指针类型*/}bintnode; /*结点类型*/typedef bintnode *bintree; /*指向二叉树结点的指针类型*/void createbintree(bintree *T); /*构造二叉链表*/void Preorder(bintree T); /*先序*/void inorderout(bintree T); /*中序*/void Postorder(bintree T); /*后序*/void LevelOrder(bintree T); /*按层次遍历二叉树T*//*以加入结点的先序序列输入,构造二叉链表*/void createbintree(bintree *T){char ch;scanf("\n%c",&ch);if(ch=='0')*T=NULL;else{*T=(bintnode*)malloc(sizeof(bintnode));(*T)->data=ch;createbintree(&(*T)->lchild);createbintree(&(*T)->rchild);}}/*先序遍历二叉树T*/void Preorder(bintree T){if (T){cout<data; /*访问结点数据*/Preorder(T->lchild); /*先序遍历二叉树T的左子树*/Preorder(T->rchild); /*先序遍历二叉树T的右子树*/}}/*中序遍历二叉树T*/void inorderout(bintree T){if(T){inorderout(T->lchild); /*中序遍历二叉树T的左子树*/cout<data; /*访问结点数据*/inorderout(T->rchild); /*中序遍历二叉树T的右子树*/}}/*后序遍历二叉树T*/void Postorder(bintree T){if (T){Postorder(T->lchild); /*后序遍历二叉树T的左子树*/Postorder(T->rchild); /*后序遍历二叉树T的右子树*/cout<data; /*访问结点数据*/}}void LevelOrder(bintree bt){bintree queue[maxnode];int front,rear;if (bt==0)return;front=-1; /*队列初始化*/rear=0;queue[rear]=bt; /*根结点入队*/while(front!=rear){front++;cout<data; /*访问队首结点的数据域*/if(queue[front]->lchild!=NULL) /*将队首结点的左孩子结点入队列*/{rear++;queue[rear]=queue[front]->lchild;}//ifif(queue[front]->rchild!=NULL) /*将队首结点的右孩子结点入队列*/{rear++;queue[rear]=queue[front]->rchild;}//if}//while}//LevelOrdervoid main(){bintree T;char a,b;cout<>b;switch(b){case '1': cout<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值