1. 程式人生 > 其它 >二叉樹遍歷的遞迴演算法

二叉樹遍歷的遞迴演算法

二叉樹遍歷

遞迴演算法

  • BinaryTree.h
//
// Created by Administrator on 2021/10/25.
//

#ifndef TEST_BINARYTREE_H
#define TEST_BINARYTREE_H

#include <iostream>

using namespace std;

typedef char ElemType;

typedef struct BiNode {
    ElemType data;
    BiNode *left;
    BiNode *right;

    BiNode(ElemType val) {
        data = val;
        left = nullptr;
        right = nullptr;
    }
} BiNode, *BiTree;

class BinaryTree {
public:
    void Create();

    int getSize();

    int getHeight();

    void preOrder();

    void inOrder();

    void postOrder();

    void destroy();

private:
    BiTree create();

    void preOrder(BiTree root);

    void inOrder(BiTree root);

    void postOrder(BiTree root);

    void destroy(BiTree root);

    int getHeight(BiTree root);

    void AddNode(ElemType key, int direction, BiTree root);

    BiTree m_root;
    int size;
};

#endif //TEST_BINARYTREE_H
  • BinaryTree.cpp
//
// Created by Administrator on 2021/10/25.
//

#include "BinaryTree.h"

void BinaryTree::Create() {
    size = 0;
    m_root = create();
}

int BinaryTree::getSize() {
    return size;
}

int BinaryTree::getHeight() {
    return getHeight(m_root);
}

void BinaryTree::preOrder() {
    cout << "先序遍歷:" << endl;
    preOrder(m_root);
    cout << endl;
}

void BinaryTree::inOrder() {
    cout << "中序遍歷:" << endl;
    inOrder(m_root);
    cout << endl;
}

void BinaryTree::postOrder() {
    cout << "後序遍歷:" << endl;
    postOrder(m_root);
    cout << endl;
}

void BinaryTree::destroy() {
    destroy(m_root);
}


BiTree BinaryTree::create() {
    BiTree current = nullptr;
    ElemType val;
    // 輸入值
    cin >> val;
    if (val == '#') {
        // 當前子樹為空
        return nullptr;
    } else {
        // 遞迴建立左右子樹
        size++;
        current = new BiNode(val);
        current->left = create();
        current->right = create();
        return current;
    }
}

void BinaryTree::preOrder(BiTree root) {
    if (root == nullptr)
        return;
    else {
        cout << root->data << " --> ";  // 先列印根節點
        preOrder(root->left);           // 遍歷左子樹
        preOrder(root->right);          // 遍歷右子樹
    }
}

void BinaryTree::inOrder(BiTree root) {
    if (root == nullptr)
        return;
    else {
        inOrder(root->left);
        cout << root->data << " --> ";
        inOrder(root->right);
    }
}

void BinaryTree::postOrder(BiTree root) {
    if (root == nullptr)
        return;
    else {
        postOrder(root->left);
        postOrder(root->right);
        cout << root->data << " --> ";
    }
}

void BinaryTree::destroy(BiTree root) {
    if (root) {
        destroy(root->left);
        destroy(root->right);
        // 釋放節點
        delete root;
        root = nullptr;
        size = 0;
    }
}

/**
 *
 * @param root
 * @return 遞迴得到樹高
 */
int BinaryTree::getHeight(BiTree root) {
    if (root == nullptr)
        return 0;
    int left_height = getHeight(root->left);
    int right_height = getHeight(root->right);
    return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
}

/**
 * 前序遍歷的方式建立
 * @param key 值
 * @param direction 0是從左子樹插入,1是從右子樹插入
 * @param root 被插入的根節點
 */
void BinaryTree::AddNode(const ElemType key, int direction, BiTree root) {
    if (direction == 0) {
        // 插入到左子樹
        if (root->left == nullptr)
            root->left = new BiNode(key);
        else
            AddNode(key, direction, root->left);
    } else if (direction == 1) {
        // 插入到右子樹
        if (root->right == nullptr)
            root->right = new BiNode(key);
        else
            AddNode(key, direction, root->right);
    }
}
  • main.cpp
#include <iostream>
#include "BinaryTree.h"

using namespace std;

int main() {

    BinaryTree tree{};
    cout << "按先序遍歷方式建立樹:" << endl;
    // ABCD##E##FG###HI#J##K##
    tree.Create();
    cout << "樹的高度為:" << tree.getHeight() << endl;
    cout << "樹的節點為:" << tree.getSize() << endl;
    tree.preOrder();
    tree.inOrder();
    tree.postOrder();
    tree.destroy();

    return 0;
}
  • 截圖