前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stack实现二叉树的前序、中序和后序遍历

stack实现二叉树的前序、中序和后序遍历

作者头像
Cyril-KI
发布2022-07-29 19:35:15
3230
发布2022-07-29 19:35:15
举报
文章被收录于专栏:KI的算法杂记

前序遍历

思想

前序遍历的顺序为DLR,因此先将根结点进栈,当栈不为空:当前结点出栈,访问该结点->如果当前结点的右子树不为空则进栈->如果当前结点的左子树不为空则进栈。

代码

代码语言:javascript
复制
void preorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  while(!stk.empty()) {
    BitTree* t = stk.top();
    stk.pop();
    cout << t->val << " ";
    if(t->right) {
      stk.push(t->right);
    }
    if(t->left) {
      stk.push(t->left);
    }
  }
  cout << endl;
}

中序遍历

思想

中序遍历的顺序为LDR,因此我们需要先找到最左下方的结点,步骤:当前结点的左孩子不为空则一直进栈->当栈不为空时,栈顶的元素肯定是没有左孩子的,依据中序遍历的特点,直接输出。如果该结点存在右孩子,说明已经进行完LD,下一步应该进行R,所以应该将右孩子入栈,然后结束循环。

代码

代码语言:javascript
复制
void inorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  while(!stk.empty()) {
    while(stk.top()->left != NULL) {
      stk.push(stk.top()->left);
    }
    while(!stk.empty()) {
      BitTree* t = stk.top();
      stk.pop();
      cout << t->val << " "; //不存在左孩子,直接输出
      if(t->right != NULL) {
        stk.push(t->right);  //如果有右孩子,下一步就输出右孩子
        break;
      }
    }
  }
  cout << endl;
}

后序遍历

思想

后序遍历的顺序为LRD,同样需要先找到最左下方的结点,这和中序遍历一致,所以步骤为:如果当前结点的左孩子不为空则一直进栈->当栈不为空时,当前结点肯定为最左下方的结点,如果上一次出栈并被访问的结点为当前结点的右孩子或者当前结点的右孩子为空,都说明已经进行完了LR的步骤,应该进行输出,否则如果右孩子不为空,则说明只进行完了L步骤,下一步应该进行R步骤,即将当前结点的右孩子入栈,然后结束循环。

代码

代码语言:javascript
复制
void postorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  BitTree* temp = NULL;
  while(!stk.empty()) {
    while(stk.top()->left != NULL) {
      stk.push(stk.top()->left);
    }
    while(!stk.empty()) {
      BitTree* t = stk.top();
      if(temp == t->right || t->right == NULL) {
        cout << t->val << " ";
        temp = t;
        stk.pop();
      }else if(t->right != NULL) {
        stk.push(t->right);
        break;
      }
    }
  }
  cout << endl;
} 

完整代码

代码语言:javascript
复制
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
using namespace std;

map<int, int> mp;

struct BitTree {
  int val;
  BitTree* left;
  BitTree* right;
  BitTree(int x) : val(x), left(NULL), right(NULL) {}
};

BitTree* create1(vector<int>& pre, int x1, int x2, int x3, int x4) {
  if(x1 > x2 || x3 > x4) {
    return NULL;
  }
  //创建根结点 
  int root_val = pre[x1];
  BitTree* root = new BitTree(root_val);
  int x = mp[root_val];  //中序遍历中根的位置
  int y = x - x3 + x1;
  //创建左子树
  root->left = create1(pre, x1 + 1, y, x3, x - 1);
  //创建右子树 
  root->right = create1(pre, y + 1, x2, x + 1, x4);
  return root; 
}

//中序遍历和后续遍历构建二叉树
BitTree* create2(vector<int>& post, int x1, int x2, int x3, int x4) {
  if(x1 > x2 || x3 > x4) {
    return NULL;
  }
  //创建根结点
  int root_val = post[x2];
  BitTree* root = new BitTree(root_val);
  int x = mp[root_val]; //中序遍历中的位置
  int y = x1 + x - 1 - x3;
  //创建左子树
  root->left = create2(post, x1, y, x3, x - 1);
  //创建右子树
  root->right = create2(post, y + 1, x2 - 1, x + 1, x4);
  return root; 
}

//层次遍历
void level(BitTree* T) {
  queue<BitTree*> que;
  que.push(T);
  while(!que.empty()) {
    BitTree* t = que.front();
    que.pop();
    cout << t->val << " ";
    if(t->left) {
      que.push(t->left);
    }
    if(t->right) {
      que.push(t->right);
    }
  }
}

//栈实现前序遍历
void preorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  while(!stk.empty()) {
    BitTree* t = stk.top();
    stk.pop();
    cout << t->val << " ";
    if(t->right) {
      stk.push(t->right);
    }
    if(t->left) {
      stk.push(t->left);
    }
  }
  cout << endl;
}

//栈实现中序遍历
void inorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  while(!stk.empty()) {
    //寻找左节点
    while(stk.top()->left != NULL) {
      stk.push(stk.top()->left);
    }
    while(!stk.empty()) {
      BitTree* t = stk.top();
      stk.pop();
      cout << t->val << " ";
      if(t->right != NULL) {
        stk.push(t->right);
        break;
      }
    }
  }
  cout << endl;
}

//栈实现后序遍历
void postorder(BitTree* T) {
  if(T == NULL) {
    return;
  }
  stack<BitTree*> stk;
  stk.push(T);
  BitTree* temp = NULL;
  while(!stk.empty()) {
    while(stk.top()->left != NULL) {
      stk.push(stk.top()->left);
    }
    while(!stk.empty()) {
      BitTree* t = stk.top();
      if(temp == t->right || t->right == NULL) {
        cout << t->val << " ";
        temp = t;
        stk.pop();
      }else if(t->right != NULL) {
        stk.push(t->right);
        break;
      }
    }
  }
  cout << endl;
} 

int main() {
  vector<int> pre = {3, 9, 20, 15, 7};
  vector<int> in = {9, 3, 15, 20, 7};
  vector<int> post = {9, 15, 7, 20, 3};
  for(int i = 0; i < in.size(); i++) {
    mp[in[i]] = i;  //记录中序遍历中元素的位置
  }
  int size = in.size();
  BitTree *T1 = create1(pre, 0, size - 1, 0, size - 1);
  //BitTree *T2 = create2(post, 0, size - 1, 0, size - 1);
  postorder(T1);
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 KI的算法杂记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前序遍历
    • 思想
      • 代码
      • 中序遍历
        • 思想
          • 代码
          • 后序遍历
            • 思想
              • 代码
              • 完整代码
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档