二叉树 前序,中序,后序遍历 --非递归遍历

来源:互联网 发布:电玩巴士淘宝 编辑:程序博客网 时间:2024/06/05 04:15

前序遍历:

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


0 0
原创粉丝点击