第十周项目2--二叉树遍历的递归算法
来源:互联网 发布:ubuntu mount ext4 编辑:程序博客网 时间:2024/06/05 11:06
#include "btree.h"
void PreOrder(BTNode *b) //先序遍历的递归算法
{
if (b!=NULL)
{
printf("%c ",b->data); //访问根节点
PreOrder(b->lchild); //递归访问左子树
PreOrder(b->rchild); //递归访问右子树
}
}
void InOrder(BTNode *b) //中序遍历的递归算法
{
if (b!=NULL)
{
InOrder(b->lchild); //递归访问左子树
printf("%c ",b->data); //访问根节点
InOrder(b->rchild); //递归访问右子树
}
}
void PostOrder(BTNode *b) //后序遍历的递归算法
{
if (b!=NULL)
{
PostOrder(b->lchild); //递归访问左子树
PostOrder(b->rchild); //递归访问右子树
printf("%c ",b->data); //访问根节点
}
}
int main()
{
BTNode *b;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉树b:");
DispBTNode(b);
printf("\n");
printf("先序遍历序列:\n");
PreOrder(b);
printf("\n");
printf("中序遍历序列:\n");
InOrder(b);
printf("\n");
printf("后序遍历序列:\n");
PostOrder(b);
printf("\n");
DestroyBTNode(b);
return 0;
void PreOrder(BTNode *b) //先序遍历的递归算法
{
if (b!=NULL)
{
printf("%c ",b->data); //访问根节点
PreOrder(b->lchild); //递归访问左子树
PreOrder(b->rchild); //递归访问右子树
}
}
void InOrder(BTNode *b) //中序遍历的递归算法
{
if (b!=NULL)
{
InOrder(b->lchild); //递归访问左子树
printf("%c ",b->data); //访问根节点
InOrder(b->rchild); //递归访问右子树
}
}
void PostOrder(BTNode *b) //后序遍历的递归算法
{
if (b!=NULL)
{
PostOrder(b->lchild); //递归访问左子树
PostOrder(b->rchild); //递归访问右子树
printf("%c ",b->data); //访问根节点
}
}
int main()
{
BTNode *b;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉树b:");
DispBTNode(b);
printf("\n");
printf("先序遍历序列:\n");
PreOrder(b);
printf("\n");
printf("中序遍历序列:\n");
InOrder(b);
printf("\n");
printf("后序遍历序列:\n");
PostOrder(b);
printf("\n");
DestroyBTNode(b);
return 0;
}
0 0
- 第十周 项目2 二叉树的遍历递归算法
- 第十周-项目2 - 二叉树遍历的递归算法
- 第十周项目2-二叉树遍历的递归算法
- 第十周-项目2 - 二叉树遍历的递归算法
- 第十周 项目2-二叉树遍历的递归算法
- 第十周项目2--二叉树遍历的递归算法
- 第十周项目2【二叉树遍历的递归算法】
- 第十周 项目2 二叉树遍历的递归算法
- 第十周 项目2-二叉树遍历的递归算法
- 第十周项目2-二叉树遍历的递归算法
- 第十周项目2-二叉树遍历的递归算法
- 第十周项目2--二叉树遍历的递归算法
- 第十周项目2 - 二叉树遍历的递归算法
- 第十周 项目2--二叉树遍历的递归算法
- 第十周项目2二叉树遍历的递归算法
- 第十周 项目2-二叉树遍历的递归算法
- 第十周项目2-二叉树遍历的递归算法
- 第十周项目2- 二叉树遍历的递归算法
- odroid x2安装ubuntu系统,HDMI显示,说说折腾事儿。
- JSP中的JavaBean和动作元素(四)
- 百度地图 公交查询 redis入门
- NodeJS+express+ajax的简单post登录应用
- socket 套接字编程
- 第十周项目2--二叉树遍历的递归算法
- How To Use Git Effectively
- 【C语言】建立静态链 并 输出链表
- 第十周项目3--利用二叉树遍历思想解决问题
- maven 自定义archetype
- jquery、js调用iframe父窗口与子窗口元素的方法
- 自动装箱与自动拆箱
- Linux命令-命令别名(查看 设置 删除)
- 增量更新简介