1 问题
编写程序实现二叉树的如下操作:
1) 建立二叉链表
2) 二叉树的先序、中序、后序遍历
3) 求二叉树的叶子结点个数
4) 将二叉树中所有结点的左、右子树相互交换
输入:
扩展二叉树先序序列:ab#d##ce###。其中#代表空指针。
输出:
二叉树的凹入表示
二叉树的先序序列、中序序列、后序序列
二叉树叶子结点个数
左、右子树相互交换后的二叉树的凹入表示
左、右子树相互交换后的二叉树的先序序列、中序序列、后序序列。
说明:
在输出凹入表示的二叉树时,先输出根结点,然后依次输出左右子树,上下层结点之间相隔 3 个空格。
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
2 解题
- 属于基础操作,着重理解其中的递归过程和利用二级指针创建二叉树的操作
#include <cstdio>
#include <cstdlib>
#include <iomanip> //cout函数格式化输出头文件
#include <iostream>
using namespace std;
typedef struct BiNode {
char element;
struct BiNode *left;
struct BiNode *right;
} * BiTree;
int numleaf = 0; //叶子节点的数量
// 使用二级指针递归创建二叉树(不需要返回)
void linkcreat(BiTree *rt) {
char ch;
cin >> ch;
if (ch == '#')
*rt = NULL;
else {
*rt = (BiTree)malloc(sizeof(struct BiNode));
(*rt)->element = ch;
linkcreat(&(*rt)->left);
linkcreat(&(*rt)->right);
if (!(*rt)->left && !(*rt)->right) numleaf++;
}
}
// 使用返回值递归创建二叉树
BiTree linkcreat(BiTree rt) {
char ch;
cin >> ch;
if (ch == '#')
rt = NULL;
else {
rt = (BiTree)malloc(sizeof(struct BiNode));
rt->left = NULL;
rt->right = NULL;
rt->element = ch;
rt->left = linkcreat(rt->left);
rt->right = linkcreat(rt->right);
if (!rt->left && !rt->right) numleaf++;
}
return rt;
}
void formatprint(char ch, int num) {
for (int i = 0; i < 4 * num; i++) cout << " ";
cout << ch << endl;
}
void listprint(BiTree rt, int depth) { //depth是深度,用来控制凹入法的格式化输出
if (rt) { //非空
formatprint(rt->element, depth++); //这一行执行完了以后depth的值才回增加
listprint(rt->left, depth);
listprint(rt->right, depth);
}
}
void preorder(BiTree rt) {
if (rt) {
cout << rt->element;
preorder(rt->left);
preorder(rt->right);
}
}
void inorder(BiTree rt) {
if (rt) {
inorder(rt->left);
cout << rt->element;
inorder(rt->right);
}
}
void postorder(BiTree rt) {
if (rt) {
postorder(rt->left);
postorder(rt->right);
cout << rt->element;
}
}
void Swaptree(BiTree rt) {
if (rt) {
if (rt->left || rt->right) {
BiTree tmp; //直接交换地址
tmp = rt->left;
rt->left = rt->right;
rt->right = tmp;
}
Swaptree(rt->left);
Swaptree(rt->right);
}
}
int main() {
//freopen("file in.txt", "r", stdin);
BiTree root;
root = NULL; //初始化
// linkcreat(&root);
root = linkcreat(root);
cout << "BiTree" << endl;
listprint(root, 0); //初始深度为0
// 默认是右对齐,这里left控制左对齐,宽度14
cout << left << setw(14) << "pre_sequence";
cout << ": ";
preorder(root);
cout << endl;
cout << left << setw(14) << "in_sequence";
cout << ": ";
inorder(root);
cout << endl;
cout << left << setw(14) << "post_sequence";
cout << ": ";
postorder(root);
cout << endl;
cout << left << setw(14) << "Number of leaf";
cout << ": ";
cout << numleaf << endl;
Swaptree(root);
cout << left << setw(14) << "BiTree swapped";
cout << endl;
listprint(root, 0);
cout << left << setw(14) << "pre_sequence";
cout << ": ";
preorder(root);
cout << endl;
cout << left << setw(14) << "in_sequence";
cout << ": ";
inorder(root);
cout << endl;
cout << left << setw(14) << "post_sequence";
cout << ": ";
postorder(root);
cout << endl;
return 0;
}
小结
- 自定义函数可以名字相同,参数不同,调用的时候主函数根据参数类型自行调用相关的函数,代码中就包含了两个listcreat函数