二叉树的遍历
来源:互联网 发布:维棠下载器 mac版 编辑:程序博客网 时间:2024/06/12 20:20
本题要求给定二叉树的4种遍历。
函数接口定义:
void InorderTraversal( BinTree BT );void PreorderTraversal( BinTree BT );void PostorderTraversal( BinTree BT );void LevelorderTraversal( BinTree BT );
其中BinTree
结构定义如下:
typedef struct TNode *Position;typedef Position BinTree;struct TNode{ ElementType Data; BinTree Left; BinTree Right;};
要求4个函数分别按照访问顺序打印出结点的内容,格式为一个空格跟着一个字符。
实现:
void InorderTraversal( BinTree BT ){ if (BT == NULL) return ; InorderTraversal(BT->Left); printf(" %c", BT->Data); InorderTraversal(BT->Right);}void PreorderTraversal( BinTree BT ){ if (BT == NULL) return ; printf(" %c", BT->Data); PreorderTraversal(BT->Left); PreorderTraversal(BT->Right);}void PostorderTraversal( BinTree BT ){ if (BT == NULL) return ; PostorderTraversal(BT->Left); PostorderTraversal(BT->Right); printf(" %c", BT->Data);}void LevelorderTraversal( BinTree BT ){ BinTree *que; int head = 0, tail = 0; if (BT == NULL) return ; que = (BinTree *)malloc(sizeof(struct TNode) * 100); que[tail++] = BT; while (head != tail) { BinTree now = que[head++]; printf(" %c", now->Data); if (now->Left != NULL) que[tail++] = now->Left; if (now->Right != NULL) que[tail++] = now->Right; } free(que);}
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- Linux下的fdisk用法
- mybatis generator的使用
- 51nod 1460 连接小岛 (贪心)
- 面向对象高级( 随手记-7)
- dbutils源码的基本结构
- 二叉树的遍历
- 使用python获取系统所有进程PID以及进程名称
- 如何获得exe当前路径
- VMware 12 许可密钥
- 根据datetimepicker选择的日期计算年龄
- SpringMVC 返回json
- C++ 读取文件内容到data 结构体 structure
- Python中hashlib模块
- 杭电 1720 A+B Coming 十六进制转化为十进制