二叉树的先序、中序、后序的递归与非递归实现
来源:互联网 发布:js canvas api 编辑:程序博客网 时间:2024/05/22 13:09
#include <iostream>#include <cstdlib>#include <stack>using namespace std;struct BinTree { int data; struct BinTree *left; struct BinTree *right;};struct BinPost { BinTree *pTree; bool isFirst;};void CreateBinTree(BinTree* &root){ int data; cin>>data; if(data == 0) root = NULL; else { root = (BinTree*)malloc(sizeof(BinTree)); root->data = data; CreateBinTree(root->left); CreateBinTree(root->right); }}void preOrderFirst(BinTree *root){ if(root) { cout<<root->data<<' '; preOrderFirst(root->left); preOrderFirst(root->right); }}void inOrderFirst(BinTree *root){ if(root) { inOrderFirst(root->left); cout<<root->data<<' '; inOrderFirst(root->right); }}void postOrderFirst(BinTree *root){ if(root) { postOrderFirst(root->left); postOrderFirst(root->right); cout<<root->data<<' '; }}void preOrderSecond(BinTree *root){ BinTree *p; stack<BinTree*> stk; stk.push(root); while(!stk.empty()) { p = stk.top(); while(p != NULL) { cout<<p->data<<' '; p = p->left; stk.push(p); } stk.pop(); if(!stk.empty()) { p = stk.top(); stk.pop(); if(p) { p = p->right; stk.push(p); } } }}void inOrderSecond(BinTree *root){ BinTree *p; stack<BinTree*> stk; stk.push(root); while(!stk.empty()) { p = stk.top(); while(p != NULL) { p = p->left; stk.push(p); } stk.pop(); if(!stk.empty()) { p = stk.top(); stk.pop(); if(p) { cout<<p->data<<' '; p = p->right; stk.push(p); } } }}void postOrderSecond(BinTree *root){ BinTree *p; BinPost *q; BinPost *post = (BinPost*)malloc(sizeof(BinPost)); stack<BinPost*> stk; post->isFirst = true; post->pTree = root; stk.push(post); while(!stk.empty()) { q = stk.top(); p = q->pTree; while(p != NULL) { p = p->left; post = (BinPost*)malloc(sizeof(BinPost)); post->isFirst = true; post->pTree = p; stk.push(post); } stk.pop(); if(!stk.empty()) { q = stk.top(); p = q->pTree; if(q->isFirst) { q->isFirst = false; p = p->right; post = (BinPost*)malloc(sizeof(BinPost)); post->isFirst = true; post->pTree = p; stk.push(post); } else { cout<<p->data<<' '; q->pTree = NULL; } } }}int main(){ BinTree *root; CreateBinTree(root); preOrderFirst(root); cout<<endl; preOrderSecond(root); cout<<endl; inOrderFirst(root); cout<<endl; inOrderSecond(root); cout<<endl; postOrderFirst(root); cout<<endl; postOrderSecond(root); cout<<endl; return 0;}
0 0
- 二叉树的先序、中序、后序的递归与非递归实现
- C++ 二叉树的构建,先序/中序/后序的递归/非递归实现
- 二叉树的先序、中序、后序遍历的递归和非递归实现
- 二叉树的先序,中序,后序递归和非递归实现
- C++实现二叉树的递归遍历与非递归遍历(先序、中序、后序、层序)
- 二叉树的先序、中序和后序遍历,递归与非递归方式实现。
- 二叉树的先序,中序,层次遍历,递归与非递归实现
- 二叉树的先序、中序、后序的递归及非递归实现,以及层次遍历的实现:
- 二叉树的先序、中序、后序遍历的非递归实现
- [二叉树专题]:先序遍历二叉树的递归实现与非递归实现
- 二叉树遍历的非递归算法(先序、中序、后序)代码实现
- php 实现(非递归,栈模拟)二叉树的先序、中序、后序
- c语言原生实现二叉树的 非递归 中序 先序 后序 遍历
- 二叉树的先序、中序、后序的递归和非递归算法
- 二叉树的先序、中序、后序、层序递归及非递归遍历
- 二叉树的先序/中序/后序(递归、非递归)+层序遍历
- java 实现二叉树的构建,先序,中序,后序,层次,递归,非递归的遍历
- 递归和非递归的方式实现二叉树的先序、中序和后序遍历
- Android中的style和theme的用法
- UINavigationController 导航控制器
- system函数的总结
- 黑马程序员_问题总结(持续更新)
- MyEcplise开发Servlet笔记
- 二叉树的先序、中序、后序的递归与非递归实现
- 神经网络学习算法matlab应用分析
- Android View.post(Runnable )
- 使用微博API(nearby timeline接口)搜集含GPS新浪微博数据
- linux上mysql数据库迁移
- 递归式之主方法
- UIApplication
- Android中的"Unable to start activity ComponentInfo"或者"Unable to instantiate activity ComponentInfo"的错误
- 人生第一次