二叉樹 前序,中序,後序遍歷 --非遞歸遍歷

前序遍歷:

#include <iostream>
#include <stack>
using namespace std;

struct TreeNode {
    int value;
    TreeNode* left;
    TreeNode* right;
};

TreeNode* createNode(int value) {
    TreeNode* node = new TreeNode;
    node -> value = value;
    node -> left = NULL;
    node -> right = NULL;
    return node;
}

void BinaryTree_PreOrder_1(TreeNode* root) {       //用棧記憶
    if(root == NULL) return;
    stack<TreeNode*> st;
    st.push(root);
    while(!st.empty()) {
        TreeNode* node = st.top();
        st.pop();
        cout<<node -> value<<ends;
        if(node -> right) st.push(node -> right);
        if(node -> left) st.push(node -> left);
    }
}

void BinaryTree_PreOrder_2(TreeNode* root) {        //結點增加指向父節點的指針:通過指向父節點的指針來回溯
    if(root == NULL) return;
    stack<TreeNode*> st;
    while((root != NULL) || !st.empty()) {
        if(root != NULL) {
            cout<<root -> value<<ends;
            st.push(root);
            root = root -> left;
        } else {
            root = st.top();
            st.pop();
            root = root -> right;
        }
    }
}

int main() {
    TreeNode* root = new TreeNode;
    root -> value = 10;
    root -> left = createNode(5);
    root -> right = createNode(12);
    root -> left -> left = createNode(4);
    root -> left -> right = createNode(7);
    BinaryTree_PreOrder_1(root);
    cout<<endl;
    BinaryTree_PreOrder_2(root);
}

中序遍歷:

#include <iostream>
#include <stack>
using namespace std;

struct TreeNode {
    int value;
    TreeNode* left;
    TreeNode* right;
};

TreeNode* createNode(int value) {
    TreeNode* node = new TreeNode;
    node -> value = value;
    node -> left = NULL;
    node -> right = NULL;
    return node;
}

void BinaryTree_InOrder(TreeNode* root) {
    if(root == NULL) return;
    stack<TreeNode*> st;
    while((root != NULL) || !st.empty()) {
        if(root != NULL) {                          //在前序遍歷第二種方法中修改了下
            st.push(root);
            root = root -> left;
        } else {
            root = st.top();
            st.pop();
            cout<<root -> value<<ends;
            root = root -> right;
        }
    }
}

int main() {
    TreeNode* root = new TreeNode;
    root -> value = 10;
    root -> left = createNode(5);
    root -> right = createNode(12);
    root -> left -> left = createNode(4);
    root -> left -> right = createNode(7);
    BinaryTree_PreOrder_1(root);
    cout<<endl;
    BinaryTree_InOrder(root);
}

後序遍歷:

#include <iostream>
#include <stack>
using namespace std;

struct TreeNode {
    int value;
    TreeNode* left;
    TreeNode* right;
};

TreeNode* createNode(int value) {
    TreeNode* node = new TreeNode;
    node -> value = value;
    node -> left = NULL;
    node -> right = NULL;
    return node;
}

void BinaryTree_PostOrder_1(TreeNode* root) {    // 後序遍歷的非遞歸
    if(root == NULL) return;
    stack<TreeNode*> st;
    TreeNode* curr = root;                     // 指向當前要檢查的節點
    TreeNode* PreVis = NULL;                   // 指向前一個被訪問的節點
    while(curr != NULL || !st.empty()) {       // 棧空時結束
        while(curr != NULL) {                  // 一直向左走直到爲空
            st.push(curr);
            curr = curr -> left;
        }
        curr = st.top();
        // 當前節點的右孩子如果爲空或者已經被訪問,則訪問當前節點
        if(curr -> right == NULL || curr -> right == PreVis) {
            cout<<curr -> value<<ends;
            PreVis = curr;
            st.pop();
            curr = NULL;
        } else {                       // 否則訪問右孩子
            curr = curr -> right;
        }
    }
}

void BinaryTree_PostOrder_2(TreeNode* root) {     // 後序遍歷的非遞歸     雙棧法
    stack<TreeNode*> s1, s2;
    TreeNode* curr;                     // 指向當前要檢查的節點
    s1.push(root);
    while(!s1.empty()) {               // 棧空時結束 
        curr = s1.top();
        s1.pop();
        s2.push(curr);
        if(curr -> left) {
            s1.push(curr -> left);
        }
        if(curr -> right) {
            s1.push(curr -> right);
        }
    }
    while(!s2.empty()) {
        cout<<s2.top() -> value<<ends;
        s2.pop();
    }
}

int main() {
    TreeNode* root = new TreeNode;
    root -> value = 1;
    root -> left = createNode(2);
    root -> right = createNode(3);
    root -> left -> left = createNode(4);
    root -> left -> right = createNode(5);
    root -> right -> left = createNode(6);
    root -> right -> right = createNode(7);
    BinaryTree_PostOrder_1(root);
    cout<<endl;
    BinaryTree_PostOrder_2(root);
}


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