二叉樹三序遍歷 + 層序遍歷(循環隊列)

#include <iostream>

using namespace std;

typedef char ElemType;
typedef struct node { // 二叉鏈表存儲結構
    ElemType data;
    struct node *Lchild, *Rchild;
} Bnode, *Bintree;

/**
 * 先序建立二叉樹 TLR
 * @param T 引用 main 函數中定義的樹根節點的地址
 */
void createBintree(Bintree &T) {
    ElemType ch;
    cin >> ch;
    getchar(); // 接收 space 或 enter

    if (ch == '#') { // # 結束符
        T = nullptr;
    } else {
        T = (Bintree) malloc(sizeof(Bnode)); // 新建節點
        T->data = ch;
        cout << ch << " 左節點(#結束): ";
        createBintree(T->Lchild);
        cout << ch << " 右節點(#結束): ";
        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 類型循環隊列
    // 循環隊列不能只保存data,因為還要通過出隊列的元素獲得其左右孩子
    Bintree queue[10];
    int front = 0, rear = 0; // 頭尾

    // 根節點進隊列
    Bintree p = T;
    if (p) {
        rear = (rear + 1) % 10;
        queue[rear] = p;
    }

    // 循環隊列遍歷
    while (front != rear) { // 判斷循環隊列不為空

        // 隊頭元素出隊列
        front = (front + 1) % 10;
        p = queue[front];
        cout << p->data << " ";

        // 并將對頭元素的孩子節點進隊列
        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() {

    // 創建樹
    Bintree bintree;
    cout << "輸入樹的根節點: ";
    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;
}
測試樹
輸入樹的根節點: 1
1 左節點(#結束): 2
2 左節點(#結束): #
2 右節點(#結束): #
1 右節點(#結束): 3
3 左節點(#結束): 4
4 左節點(#結束): #
4 右節點(#結束): #
3 右節點(#結束): 5
5 左節點(#結束): #
5 右節點(#結束): #

先序遍歷: 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 
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容