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 
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;
}





线索二叉树






哈夫曼树实现






哈夫曼编码实现


你可能感兴趣的:(#,数据结构,二叉树,算法,数据结构,线索化,哈夫曼)