二叉树遍历,递归和非递归方法
2016-02-25 11:37
260 查看
// BinTree.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> #include <tchar.h> #include <stdio.h> #include <string> #include <stack> using namespace std; typedef struct BinTree { BinTree* lChild; BinTree* rChild; string strValue; }BinTree; void preOrder(BinTree* root); void preOrderNoneRecursion(BinTree* root); void inOrder(BinTree* root); void inOrderrNoneRecursion(BinTree* root); void postOrder(BinTree* root); void postOrderNoneRecursion(BinTree* root); BinTree* formTestBinTree(); int _tmain(int argc, _TCHAR* argv[]) { BinTree* root = formTestBinTree(); printf("preOrder recursion:"); preOrder(root); printf("\n"); printf("preOrder none recursion:"); preOrderNoneRecursion(root); printf("\n"); printf("inOrder recursion:"); inOrder(root); printf("\n"); printf("inOrder none recursion:"); inOrderrNoneRecursion(root); printf("\n"); printf("postOrder recursion:"); postOrder(root); printf("\n"); printf("postOrder none recursion:"); postOrderNoneRecursion(root); printf("\n"); return 0; } BinTree* formTestBinTree() { //layer3 BinTree* layer3_1 = new BinTree(); layer3_1->strValue = 'D'; layer3_1->lChild = NULL; layer3_1->rChild = NULL; BinTree* layer3_2 = new BinTree(); layer3_2->strValue = 'E'; layer3_2->lChild = NULL; layer3_2->rChild = NULL; BinTree* layer3_3 = new BinTree(); layer3_3->strValue = 'F'; layer3_3->lChild = NULL; layer3_3->rChild = NULL; //layer2 BinTree* layer2_1 = new BinTree(); layer2_1->strValue = 'B'; layer2_1->lChild = layer3_1; layer2_1->rChild = layer3_2; BinTree* layer2_2 = new BinTree(); layer2_2->strValue = 'C'; layer2_2->lChild = layer3_3; layer2_2->rChild = NULL; BinTree* root = new BinTree(); root->strValue = 'A'; root->lChild = layer2_1; root->rChild = layer2_2; return root; } void preOrder(BinTree* root) { if(!root) { return; } //_tprintf(TEXT("%s"), root->strValue); printf("%s", root->strValue.c_str()); preOrder(root->lChild); preOrder(root->rChild); } void preOrderNoneRecursion(BinTree* root) { stack<BinTree*> s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { printf("%s", p->strValue.c_str()); s.push(p); p=p->lChild; } if(!s.empty()) { p=s.top(); s.pop(); p=p->rChild; } } } void inOrder(BinTree* root) { if(!root) { return; } inOrder(root->lChild); printf("%s", root->strValue.c_str()); inOrder(root->rChild); } void inOrderrNoneRecursion(BinTree* root) { stack<BinTree*> s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->lChild; } if(!s.empty()) { p=s.top(); printf("%s", p->strValue.c_str()); s.pop(); p=p->rChild; } } } void postOrder(BinTree* root) { if(!root) { return; } postOrder(root->lChild); postOrder(root->rChild); printf("%s", root->strValue.c_str()); } void postOrderNoneRecursion(BinTree* root) { stack<BinTree*> s; BinTree *cur; //当前结点 BinTree *pre=NULL; //前一次访问的结点 s.push(root); while(!s.empty()) { cur=s.top(); if((cur->lChild==NULL && cur->rChild==NULL) || (pre!=NULL && (pre==cur->lChild||pre==cur->rChild))) { printf("%s", cur->strValue.c_str()); //如果当前结点没有孩子结点或者孩子节点都已被访问过 s.pop(); pre=cur; } else { if(cur->rChild != NULL) s.push(cur->rChild); if(cur->lChild != NULL) s.push(cur->lChild); } } }
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- C++非递归队列实现二叉树的广度优先遍历
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- 全排列算法的非递归实现与递归实现的方法(C++)
- java数学归纳法非递归求斐波那契数列的方法
- 二叉树遍历 非递归 C++实现代码
- C#非递归先序遍历二叉树实例
- C++将二叉树转为双向链表及判断两个链表是否相交
- C++非递归建立二叉树实例
- C语言实现找出二叉树中某个值的所有路径的方法
- C++实现二叉树遍历序列的求解方法
- C语言实现二叉树遍历的迭代算法
- C++实现查找二叉树中和为某一值的所有路径的示例
- 用C语言判断一个二叉树是否为另一个的子结构
- C++实现二叉树非递归遍历方法实例总结
- C++二叉树结构的建立与基本操作
- 平衡二叉树