题目信息
编写程序实现二叉树的如下操作:
- 建立二叉链表
- 二叉树的先序、中序、后序遍历
- 求二叉树的叶子结点个数
- 将二叉树中所有结点的左、右子树相互交换
输入
按完全二叉树的层次关系给出二叉树的遍历序列(#表示虚结点,数据结点为单一字符)。
输出
二叉树的凹入表示
二叉树的先序序列、中序序列、后序序列
二叉树叶子结点个数
左、右子树相互交换后的二叉树的凹入表示
左、右子树相互交换后的二叉树的先序序列、中序序列、后序序列。
说明
在输出凹入表示的二叉树时,先输出根结点,然后依次输出左右子树,上下层结点之间相隔 3 个空格。
测试样例
测试样例1
abc#de
BiTree
a
b
d
c
e
pre_sequence : abdce
in_sequence : bdaec
post_sequence : dbeca
Number of leaf: 2
BiTree swapped
a
c
e
b
d
pre_sequence : acebd
in_sequence : ceadb
post_sequence : ecdba
测试样例2
abcdefg
BiTree
a
b
d
e
c
f
g
pre_sequence : abdecfg
in_sequence : dbeafcg
post_sequence : debfgca
Number of leaf: 4
BiTree swapped
a
c
g
f
b
e
d
pre_sequence : acgfbed
in_sequence : gcfaebd
post_sequence : gfcedba
解答
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct BTreeNode
{
char data;
BTreeNode *Left;
BTreeNode *Right;
};
class BTree
{
public:
//传参需要注意,二叉树是指针类型的,节点本身就是一个指针:*node。所以需要二级指针才能改变二叉树的内容
void Create(BTreeNode *Node)
{//按层创建二叉树
queue<BTreeNode *> que;
que.push(Node);
while (!que.empty())
{
char ch;
ch = getchar();
BTreeNode *node = que.front();
que.pop();
if (ch == '\n')
{
break;
}
node->data = ch;
node->Left = new BTreeNode;
node->Left->data = '\0';
que.push(node->Left);
node->Right = new BTreeNode;
node->Right->data = '\0';
que.push(node->Right);
}
}
void DisPlay(BTreeNode *Node, int depth)
{
if (Node->data != '#' && Node->data != '\0')
{//既不是空也不是虚节点
for (int i = 0; i < depth; i++)
{
cout << " ";
}
cout << Node->data << endl;
depth++;
DisPlay(Node->Left, depth);
DisPlay(Node->Right, depth);
}
}
void preorderTree(BTreeNode *Node)
{//前序遍历(根左右)
if (Node->data != '#' && Node->data != '\0')
{
cout << Node->data;
preorderTree(Node->Left);
preorderTree(Node->Right);
}
}
void inorderTree(BTreeNode *Node)
{//中序遍历(左中右)
if (Node->data != '#' && Node->data != '\0')
{
inorderTree(Node->Left);
cout << Node->data;
inorderTree(Node->Right);
}
}
void postorderTree(BTreeNode *Node)
{ //后序遍历(左右中)
if (Node->data != '#' && Node->data != '\0')
{
postorderTree(Node->Left);
postorderTree(Node->Right);
cout << Node->data;
}
}
void levelTree(BTreeNode *Node)
{//层序遍历
queue<BTreeNode *> que;
if (Node == NULL) return;
else
{
que.push(Node);
while (!que.empty())
{
BTreeNode *node = que.front();
cout << node->data << " ";
que.pop();
if (node->Left->data != '#' && node->Left->data != '\0')
{
que.push(node->Left);
}
if (node->Right->data != '#' && node->Right->data != '\0')
{
que.push(node->Right);
}
}
}
}
int depthOfTree(BTreeNode *Node)
{//二叉树深度
if (Node->data != '#' && Node->data != '\0')
{
return max(depthOfTree(Node->Left), depthOfTree(Node->Right)) + 1;
}
else
{
return 0;
}
}
int getNodeNum(BTreeNode *Node)
{//返回节点总数目
if (Node->data != '#' && Node->data != '\0')
{
return 1 + getNodeNum(Node->Left) + getNodeNum(Node->Right);
}
else
{
return 0;
}
}
int getLeafNum(BTreeNode *Node)
{//返回叶子节点
if (Node->data == '#' || Node->data == '\0')
{
return 0;
}
else if ((Node->Left->data == '#' || Node->Left->data == '\0') &&
(Node->Right->data == '#' || Node->Right->data == '\0'))
{
return 1;
}
else
{
return getLeafNum(Node->Left) + getLeafNum(Node->Right);
}
}
void SwapTree(BTreeNode *Node)
{
if (Node->data != '#' && Node->data != '\0')
{
swap(Node->Left, Node->Right);
SwapTree(Node->Left);
SwapTree(Node->Right);
}
return;
}
};
int main()
{
BTree tree;
BTreeNode *root = (BTreeNode *) malloc(sizeof(BTreeNode));
root->data = '\0';
tree.Create(root);
cout << "BiTree" << endl;
tree.DisPlay(root, 0);
cout << "pre_sequence : ";
tree.preorderTree(root);
cout << endl << "in_sequence : ";
tree.inorderTree(root);
cout << endl << "post_sequence : ";
tree.postorderTree(root);
cout << endl << "Number of leaf: " << tree.getLeafNum(root) << endl;
cout << "BiTree swapped" << endl;
tree.SwapTree(root);
tree.DisPlay(root, 0);
cout << "pre_sequence : ";
tree.preorderTree(root);
cout << endl << "in_sequence : ";
tree.inorderTree(root);
cout << endl << "post_sequence : ";
tree.postorderTree(root);
cout << endl;
return 0;
}