二叉树的遍历
来源:互联网 发布:淘宝怎么创建子帐号 编辑:程序博客网 时间:2024/04/19 14:26
二叉树有:先序遍历,后序遍历,中序遍历:
先序遍历:
中序遍历:
后序遍历:
先要做得是建立链表,链表用递归的方法:
void creat(BiTree *root,int i)
{
if(i>=l||s[i]=='.')
*root=NULL;//如果输入点就表示空;
else
{
*root=(BiTree)malloc(sizeof(BiNode));//开辟新空间;
(*root)->data=s[i];
creat(&((*root)->LChild),2*i+1); //递归。
creat(&((*root)->RChild),2*i+2);
}
}
遍历:(以后序为例)
void PreOrder(BiTree root)
{
if(root!=NULL)
{
PreOrder(root->LChild);//输出结点;
PreOrder(root->RChild);
printf("%c",root->data);
}
}
主函数:
void main()
{
BiTree T; //跟结点‘
while(scanf("%s",s)!=EOF)
{
l=strlen(s);//计算长度;
i=0;
creat(&T,i);
PreOrder(T);
printf("\n");
}
}
完整的函数:
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- Android截图以及加水印Demo
- 多线程控制方法
- Android将Widget添加到自己的应用程序
- 多线程知识
- Android Gallery 3D 特效精华
- 二叉树的遍历
- 逐步设置vim C/C++语法高亮显示和自动缩进
- poj 1066 Treasure Hunt(判断线段相交)
- cygwin编译的win32程序不需要cygwin1.dll库的方法
- rhel.5.0 下安装GCC
- 七种武器——.NET工程师求职面试必杀技
- c#.net常用函数列表
- 查看gcc编译器预定义了哪些宏
- SGU 537 Divisibility