二叉树的生成和遍历
来源:互联网 发布:淘宝mac上的软件能用么 编辑:程序博客网 时间:2024/05/29 07:32
源文件
#include"define.h"int main(){bitree *root=create();printf("二叉树的先序遍历:\n");preorder(root);printf("\n");printf("中序遍历:\n");inorder(root);printf("\n");printf("后序遍历\n");postorder(root);printf("\n");return 0;}
头文件
#include<stdio.h>#include<malloc.h>#define MAXSIZE 1024typedef char datatype;typedef struct lnode{datatype data;struct lnode *lchild,*rchild;} bitree;//生成二叉树bitree *create(){bitree *root,*s,*Q[MAXSIZE];datatype x;unsigned int front=1,rear=0;scanf("%c",&x);while(x!='#'){if(x!='@'){s=(bitree *)malloc(sizeof(bitree));s->data=x;s->lchild=NULL;s->rchild=NULL;}rear++;Q[rear]=s;if(rear==1)root=s;else{if(rear%2)Q[front]->rchild=s;elseQ[front]->lchild=s;if(rear%2)front++;}scanf("%c",&x);}return root;}void preorder(bitree *root){bitree *t=root;if(t){printf("%c,",t->data);preorder(t->lchild);preorder(t->rchild);}}//中序遍历void inorder(bitree *root){bitree *t=root;if(t){inorder(t->lchild);printf("%c,",t->data);inorder(t->rchild);}}//后序遍历void postorder(bitree *root){bitree *t=root;if(t){postorder(t->lchild);postorder(t->rchild);printf("%c,",t->data);}}
- 二叉树的生成和遍历
- 线索二叉树的生成和遍历
- 二叉树的生成和遍历
- 二叉树的生成和遍历
- 二叉树的生成和递归遍历
- 二叉树的遍历和生成——Pascal实现
- 已知二叉树先序和中序遍历,生成二叉树
- 由二叉树前序和中序遍历生成二叉树
- 二叉树的生成与遍历
- 二叉树的生成与遍历
- 线索二叉树的生成及遍历
- 线索二叉树的生成及遍历
- 二叉树的生成与遍历
- 由二叉树先序遍历/后序遍历和中序遍历生成二叉树
- 二叉树的遍历(层遍历和深度遍历)
- 在二叉树的生成和遍历中讨论C++的参数传递
- java写的二叉搜索树(生成和前序遍历)
- 由二叉树的先序和中序遍历生成二叉树的层序和后序遍历结构
- Qt 5模块简介
- C# 在datagridview中的单元格中加入button按钮
- 【算法】n个小孩围成一圈,数到5出列,求最后剩下的人
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc
- 关于RTP协议写的很好的一篇文章
- 二叉树的生成和遍历
- 学习嵌入式,该学习什么基本的知识呢?
- Linux下system()函数返错,errno是ECHILD
- 当领导并不一定说明你的能力强
- html_day2---表格+列表+框架
- gradle - 使用介绍(二)
- 润乾——函数(分组选取)
- 学习嵌入式的 目标和定位
- mysql根据多个字段查找和置顶功能的实现