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)
abd##e##cf##g##
输出(2)
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 <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
// Binary tree storage structure
typedef struct BiTNode {
char data;
struct BiTNode* lchild;
struct BiTNode* rchild;
} BiTNode, *BiTree;
queue<BiTree> q; // Auxiliary queue
int counts = 0; // Count of leaf nodes
void createBiTree(); // Build the binary tree
void visit(BiTree R); // Visit a node
void print(BiTree R, int n); // Output the binary tree
void pre_sequence(BiTree R); // Pre-order traversal
void in_sequence(BiTree R); // In-order traversal
void post_sequence(BiTree R); // Post-order traversal
/* Representations after swapping left and right subtrees */
void swap_print(BiTree R, int n);
void swap_pre_sequence(BiTree R);
void swap_in_sequence(BiTree R);
void swap_post_sequence(BiTree R);
int main() {
BiTree bit;
bit = (BiTree)malloc(sizeof(BiTNode));
q.push(bit);
// Build the tree
createBiTree();
// Print the binary tree
cout << "BiTree" << endl;
print(bit, 0);
// Pre-order
cout << "pre_sequence : ";
pre_sequence(bit);
cout << endl;
// In-order
cout << "in_sequence : ";
in_sequence(bit);
cout << endl;
// Post-order
cout << "post_sequence : ";
post_sequence(bit);
cout << endl;
// Number of leaf nodes
cout << "Number of leaf: " << counts << endl;
/* Swap the subtrees */
cout << "BiTree swapped" << endl;
swap_print(bit, 0);
cout << "pre_sequence : ";
swap_pre_sequence(bit);
cout << endl;
cout << "in_sequence : ";
swap_in_sequence(bit);
cout << endl;
cout << "post_sequence : ";
swap_post_sequence(bit);
cout << endl;
return 0;
}
void createBiTree() {
char c;
BiTree T, N;
while (!q.empty()) {
cin >> c;
if (c == '\n') // End reading at a newline character
break;
T = q.front();
q.pop();
T->data = c; // Assign node value
if (c != '#') {
// Build the left subtree
N = (BiTree)malloc(sizeof(BiTNode));
T->lchild = N;
q.push(N);
createBiTree();
// Build the right subtree
N = (BiTree)malloc(sizeof(BiTNode));
T->rchild = N;
q.push(N);
createBiTree();
}
}
}
void visit(BiTree R) {
// Output the node if it's not empty
if (R->data != '#' && R->data != '\0')
cout << R->data;
}
void print(BiTree R, int n) {
if (R->data != '#' && R->data != '\0') {
int i = 1;
while (i <= n) {
cout << " ";
i++;
}
visit(R);
cout << endl;
// Traverse one level down
n++;
print(R->lchild, n);
print(R->rchild, n);
}
}
void pre_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
visit(R);
pre_sequence(R->lchild);
pre_sequence(R->rchild);
}
}
void in_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
in_sequence(R->lchild);
/* Count leaf nodes in combination with in-order traversal */
if ((R->lchild->data == '#' || R->lchild->data == '\0') && (R->rchild->data == '#' || R->rchild->data == '\0'))
counts++;
visit(R);
in_sequence(R->rchild);
}
}
void post_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
post_sequence(R->lchild);
post_sequence(R->rchild);
visit(R);
}
}
void swap_print(BiTree R, int n) {
// The representation after swapping is the same as the original, just in a different order
if (R->data != '#' && R->data != '\0') {
int i = 1;
while (i <= n) {
cout << " ";
i++;
}
visit(R);
cout << endl;
n++;
swap_print(R->rchild, n);
swap_print(R->lchild, n);
}
}
void swap_pre_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
visit(R);
swap_pre_sequence(R->rchild);
swap_pre_sequence(R->lchild);
}
}
void swap_in_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
swap_in_sequence(R->rchild);
visit(R);
swap_in_sequence(R->lchild);
}
}
void swap_post_sequence(BiTree R) {
if (R->data != '#' && R->data != '\0') {
swap_post_sequence(R->rchild);
swap_post_sequence(R->lchild);
visit(R);
}
}