【学习点滴 -数据结构-二叉树】 二叉树的遍历(全)
2012-05-31 16:23
513 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <stack> #include <iostream> #include <malloc.h> #define LEAF -1 using namespace std; typedef struct BTreeNode{ BTreeNode* lchild; BTreeNode* rchild; int value; }BTreeNode,*Btree; BTreeNode* createTree(){ BTreeNode* T; int t; scanf("%d",&t); if(t == LEAF){ T = NULL; }else{ T = (BTreeNode *) malloc(sizeof(BTreeNode)); T->value = t; T->lchild = createTree(); T->rchild = createTree(); } return T; } void visit(BTreeNode * root){ printf("%d ",root->value); } void preOrder(BTreeNode* root){ if(root != NULL){ visit(root); } if(root->lchild != NULL){ preOrder(root->lchild); } if(root->rchild !=NULL){ preOrder(root->rchild); } } void inOrder(BTreeNode* root){ if(root->lchild != NULL){ inOrder(root->lchild); } if(root != NULL){ visit(root); } if(root->rchild !=NULL){ inOrder(root->rchild); } } void postOrder(BTreeNode* root){ if(root->lchild != NULL){ postOrder(root->lchild); } if(root->rchild !=NULL){ postOrder(root->rchild); } if(root != NULL){ visit(root); } } void BFSVisit(BTreeNode* root){ if(root == NULL){ return ; } queue<BTreeNode*> que; que.push(root); while(!que.empty()){ BTreeNode *tmp = que.front(); que.pop(); visit(tmp); if(tmp->lchild != NULL){ que.push(tmp->lchild); } if(tmp->rchild != NULL){ que.push(tmp->rchild); } } } void non_recur_preOrder(BTreeNode * root){ stack<BTreeNode *> s; BTreeNode * tmp = root; while(tmp != NULL|| !(s.empty())){ while(tmp != NULL){ visit(tmp); s.push(tmp); tmp = tmp->lchild; } while(!(s.empty())){ tmp = s.top(); s.pop(); tmp = tmp->rchild; } } } void non_recur_inOrder(BTreeNode * root){ stack<BTreeNode *> s; BTreeNode * tmp = root; while(tmp != NULL|| !(s.empty())){ while(tmp != NULL){ s.push(tmp); tmp = tmp->lchild; } while(!(s.empty())){ tmp = s.top(); visit(tmp); s.pop(); tmp = tmp->rchild; } } } void non_recur_postOrder(BTreeNode *root){ stack<int> tag; stack<BTreeNode *> s; BTreeNode* tmp = root; do{ while(tmp != NULL){ s.push(tmp); tag.push(0); tmp = tmp->lchild; } while(!s.empty() && tag.top()==1){ tmp = s.top(); s.pop(); tag.pop(); visit(tmp); } if(!s.empty()){ tag.pop(); tag.push(1); tmp = (s.top())->rchild; } }while(!(s.empty())); } main(){ BTreeNode * root; root = createTree(); printf("preOrder :"); preOrder(root); printf("\n"); printf("preOrder :"); non_recur_preOrder(root); printf("\n"); printf("inOrder :"); inOrder(root); printf("\n"); printf("inOrder :"); non_recur_inOrder(root); printf("\n"); printf("postOrder :"); postOrder(root); printf("\n"); printf("postOrder :"); non_recur_postOrder(root); printf("\n"); system("pause"); return 0; }
相关文章推荐
- 【学习点滴-数据结构-二叉树】二叉树中找大于等于(min+max)/2的节点
- 【数据结构学习】-二叉树的概念与遍历
- 【学习点滴-数据结构-二叉树】二叉查找树转换成双链表
- 数据结构学习7——二叉树的遍历
- 二叉树的概念,二叉树的数据存储结构,二叉树的性质,二叉树的遍历方法数据结构-树的学习(3)
- 数据结构学习-二叉树-链式结构、特殊二叉树、性质、遍历、遍历应用
- 【学习点滴-数据结构-二叉树】求二叉树中某两个节点的最近公共祖先
- 数据结构学习笔记(8)---二叉树的层次遍历
- 【学习点滴-数据结构-二叉树】判断二叉树是否是平衡二叉树
- 数据结构学习心得——二叉树的三种遍历算法
- 数据结构学习——非递归方法遍历二叉树
- 数据结构学习笔记8——简单二叉树的实现与遍历
- 【学习点滴-数据结构-二叉树】二叉树转换为其镜像。
- 数据结构学习之_二叉树的遍历
- 数据结构学习——二叉树的层次遍历:上下左右&下上右左
- 数据结构学习笔录--二叉树的遍历
- 数据结构与算法学习之二叉树的遍历:已知先序、中序、后序序列三者中的两种求另外一种遍历的算法
- 基本数据结构学习总结: 二叉树的遍历
- 【学习点滴-数据结构-二叉树】和为某一值的二叉树路径~
- 【学习点滴-数据结构-二叉树】求二叉树两个节点之间的最大距离