DataStructure_第六章 樹 ( 樹的先根後根遍歷 / 二叉樹的遞歸與非遞歸實現先序中序後序層次遍歷 / 線索二叉樹 / 哈夫曼樹實現 / 哈夫曼編碼實現 )





樹的 先根遍歷 後根遍歷






二叉樹的先序/中序/後序/層次遍歷 遞歸+棧 實現

/*** 
 * @Author      : acmaker
 * @Date        : 2020-05-08 17:54:45
 * @LastEditTime: 2020-05-08 22:40:18
 * @FilePath    : \myCPlusPlusCode\DataStructure\Tree\BinaryTree.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h>
using namespace std;

typedef char ElementType;
typedef struct BinaryTreeNode {
    ElementType data;
    BinaryTreeNode *l, *r;
} BinaryTreeNode, *BinaryTree;

/***
 * @description: 先序建樹
 * @param :
 * @return:
 */
BinaryTree CreateBinaryTree() {
    char ch;
    cin >> ch;
    if (ch == '#')
        return NULL;
    BinaryTreeNode* nd = new BinaryTreeNode;
    nd->data = ch;
    nd->l = CreateBinaryTree();
    nd->r = CreateBinaryTree();
    return nd;
}

/***
 * @description: 先序遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByPre_recursion(BinaryTree bt) {
    if (bt == NULL)
        return;
    cout << bt->data;
    TraverseByPre_recursion(bt->l);
    TraverseByPre_recursion(bt->r);
}

/***
 * @description: 中序遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByIn_recursion(BinaryTree bt) {
    if (bt == NULL)
        return;
    TraverseByIn_recursion(bt->l);
    cout << bt->data;
    TraverseByIn_recursion(bt->r);
}

/***
 * @description: 後序遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByPost_recursion(BinaryTree bt) {
    if (bt == NULL)
        return;
    TraverseByPost_recursion(bt->l);
    TraverseByPost_recursion(bt->r);
    cout << bt->data;
}

/***
 * @description: 先序非遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByPre_stack(BinaryTree bt) {
    BinaryTreeNode* nd = bt;
    stack<BinaryTreeNode*> S;
    while (!S.empty())
        S.pop();
    while (nd || !S.empty()) {
        while (nd) {
            cout << nd->data;
            S.push(nd);
            nd = nd->l;
        }
        if (!S.empty()) {
            nd = S.top();
            S.pop();
            nd = nd->r;
        }
    }
}

/***
 * @description: 中序非遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByIn_stack(BinaryTree bt) {
    BinaryTreeNode* nd = bt;
    stack<BinaryTreeNode*> S;
    while (!S.empty())
        S.pop();
    while (nd || !S.empty()) {
        while (nd) {
            S.push(nd);
            nd = nd->l;
        }
        if (!S.empty()) {
            nd = S.top();
            S.pop();
            cout << nd->data;
            nd = nd->r;
        }
    }
}

/***
 * @description: 後序非遞歸遍歷
 * @param :
 * @return:
 */
void TraverseByPost_stack(BinaryTree bt) {
    BinaryTreeNode* nd = bt;
    stack<BinaryTreeNode*> S1, S2;
    while (!S1.empty())
        S1.pop();
    while (!S2.empty())
        S2.pop();
    while (nd || !S1.empty()) {
        while (nd) {
            S1.push(nd);
            S2.push(nd);
            nd = nd->r;
        }
        if (!S1.empty()) {
            nd = S1.top();
            S1.pop();
            nd = nd->l;
        }
    }
    while (!S2.empty()) {
        nd = S2.top();
        S2.pop();
        cout << nd->data;
    }
}

int main() {
    // 輸入樣例如下
    // ABC##DE#G##F###
    BinaryTree bt = CreateBinaryTree();

    cout << endl;

    cout << "Recursion Traverse by Pre  : ";
    TraverseByPre_recursion(bt);
    cout << endl;

    cout << "Recursion Traverse by In   : ";
    TraverseByIn_recursion(bt);
    cout << endl;

    cout << "Recursion Traverse by Post : ";
    TraverseByPost_recursion(bt);
    cout << endl;

    cout << endl;

    cout << "Stack Traverse by Pre  : ";
    TraverseByPre_stack(bt);
    cout << endl;

    cout << "Stack Traverse by In   : ";
    TraverseByIn_stack(bt);
    cout << endl;

    cout << "Stack Traverse by Post : ";
    TraverseByPost_stack(bt);
    cout << endl;

    return 0;
}





線索二叉樹






哈夫曼樹實現






哈夫曼編碼實現


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章