#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct node { // 二叉鏈表存儲結(jié)構(gòu)
ElemType data;
struct node *Lchild, *Rchild;
} Bnode, *Bintree;
/**
* 先序建立二叉樹 TLR
* @param T 引用 main 函數(shù)中定義的樹根節(jié)點的地址
*/
void createBintree(Bintree &T) {
ElemType ch;
cin >> ch;
getchar(); // 接收 space 或 enter
if (ch == '#') { // # 結(jié)束符
T = nullptr;
} else {
T = (Bintree) malloc(sizeof(Bnode)); // 新建節(jié)點
T->data = ch;
cout << ch << " 左節(jié)點(#結(jié)束): ";
createBintree(T->Lchild);
cout << ch << " 右節(jié)點(#結(jié)束): ";
createBintree(T->Rchild);
}
}
/**
* 先序遍歷
*/
void preOrder(Bintree T) {
if (T) {
cout << T->data << " ";
preOrder(T->Lchild);
preOrder(T->Rchild);
}
}
/**
* 中序遍歷
*/
void inOrder(Bintree T) {
if (T) {
inOrder(T->Lchild);
cout << T->data << " ";
inOrder(T->Rchild);
}
}
/**
* 后序遍歷
*/
void postOrder(Bintree T) {
if (T) {
postOrder(T->Lchild);
postOrder(T->Rchild);
cout << T->data << " ";
}
}
/**
* 層序遍歷
*/
void levelOrder(Bintree T) {
// 定義 Bintree 類型循環(huán)隊列
// 循環(huán)隊列不能只保存data,因為還要通過出隊列的元素獲得其左右孩子
Bintree queue[10];
int front = 0, rear = 0; // 頭尾
// 根節(jié)點進隊列
Bintree p = T;
if (p) {
rear = (rear + 1) % 10;
queue[rear] = p;
}
// 循環(huán)隊列遍歷
while (front != rear) { // 判斷循環(huán)隊列不為空
// 隊頭元素出隊列
front = (front + 1) % 10;
p = queue[front];
cout << p->data << " ";
// 并將對頭元素的孩子節(jié)點進隊列
if (p->Lchild) { // 若左孩子不空,進隊
rear = (rear + 1) % 10;
queue[rear] = p->Lchild;
}
if (p->Rchild) { // 若右孩子不空,進隊
rear = (rear + 1) % 10;
queue[rear] = p->Rchild;
}
}
}
/**
* 樹的高度
* @param T
* @return
*/
int depthBintree(Bintree T) {
int depL, depR, dep;
if (T == nullptr) {
dep = 0;//空樹的深度為0
} else {
depL = depthBintree(T->Lchild);
depR = depthBintree(T->Rchild);
dep = depL > depR ? depL + 1 : depR + 1; // 樹的深度為其左、右子樹中最深的子樹加1
}
return dep;
}
void swapTree(Bintree T) {
if (T) {
Bintree p = T->Lchild;
T->Lchild = T->Rchild;
T->Rchild = p; // 1輪交換
swapTree(T->Lchild);
swapTree(T->Rchild);
}
}
int main() {
// 創(chuàng)建樹
Bintree bintree;
cout << "輸入樹的根節(jié)點: ";
createBintree(bintree);
cout << endl;
// 三序遍歷 + 層序
cout << "先序遍歷: ";
preOrder(bintree);
cout << endl;
cout << "中序遍歷: ";
inOrder(bintree);
cout << endl;
cout << "后序遍歷: ";
postOrder(bintree);
cout << endl;
cout << "層序遍歷: ";
levelOrder(bintree);
cout << endl;
// 樹高
cout << "樹的高度: " << depthBintree(bintree) << endl << endl;
// 交換樹
cout << "交換樹" << endl;
swapTree(bintree);
// 交換后 三序遍歷 + 層序
cout << "先序遍歷: ";
preOrder(bintree);
cout << endl;
cout << "中序遍歷: ";
inOrder(bintree);
cout << endl;
cout << "后序遍歷: ";
postOrder(bintree);
cout << endl;
cout << "層序遍歷: ";
levelOrder(bintree);
cout << endl;
return 0;
}
測試樹
輸入樹的根節(jié)點: 1
1 左節(jié)點(#結(jié)束): 2
2 左節(jié)點(#結(jié)束): #
2 右節(jié)點(#結(jié)束): #
1 右節(jié)點(#結(jié)束): 3
3 左節(jié)點(#結(jié)束): 4
4 左節(jié)點(#結(jié)束): #
4 右節(jié)點(#結(jié)束): #
3 右節(jié)點(#結(jié)束): 5
5 左節(jié)點(#結(jié)束): #
5 右節(jié)點(#結(jié)束): #
先序遍歷: 1 2 3 4 5
中序遍歷: 2 1 4 3 5
后序遍歷: 2 4 5 3 1
層序遍歷: 1 2 3 4 5
樹的高度: 3
交換樹
先序遍歷: 1 3 5 4 2
中序遍歷: 5 3 4 1 2
后序遍歷: 5 4 3 2 1
層序遍歷: 1 3 2 5 4