【学习笔记】非递归实现先后根遍历二叉树
2017-10-28 14:09
531 查看
C语言代码:
运行结果:
#include <stdio.h> #include <string.h> #include <stdlib.h> /*----方便看代码的定义----*/ typedef int Status; #define OK 0 #define NotOK 1 #define STACK_INIT_SIZE 100 //栈空间初始分配量 #define STACKINCREMENT 10 //栈空间分配增量 /*------------------------*/ /*-----定义树结构-----*/ typedef struct BiTree{ char data; struct BiTree *Lchild; struct BiTree *Rchild; }BiTree,*TreeNode; /*--------------------*/ /*-----输入字符串形式建立树(基于先根)-----*/ /*例:输入 ABD#E##F##CG##HI### 将会生成: A / \ B C / \ / \ D F G H \ / E I */ TreeNode CreateBiTree(){ char ch; TreeNode T; scanf("%c",&ch); if( ch == '#' ){ T=NULL; } else{ T = (TreeNode)malloc(sizeof(BiTree)); T->data = ch; T->Lchild = CreateBiTree(); T->Rchild = CreateBiTree(); } return T; } /*-----------------------------------------*/ /*------------------------建立栈------------------------*/ typedef struct{ TreeNode *base; //在栈构造之前和销毁之后,base的值为NULL TreeNode *top; //栈顶指针 int stacksize; //当前已分配的存储空间,以元素为单位 }Stack; Status InitStack(Stack &S){ //构造一个空栈S S.base = (TreeNode *)malloc(STACK_INIT_SIZE * sizeof(TreeNode)); if(!S.base){ exit(NotOK); //存储分配失败 } S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } Status GetTop(Stack S, TreeNode &e){ //若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回NotOK if(S.top == S.base){ return NotOK; } e = *(S.top-1); return OK; } Status Push(Stack &S, TreeNode e){ //插入元素e为新的栈顶元素 if(S.top - S.base >= S.stacksize){ //栈满,追加存储空间 S.base = (TreeNode *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(TreeNode)); if(!S.base){ //存储分配失败 exit(NotOK); } S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK; } Status Pop(Stack &S, TreeNode &e){ //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回NotOK if(S.top == S.base){ return NotOK; } e = * --S.top; return OK; } bool StackIsEmpty(Stack &S){ //若栈为空,返回true;若栈非空,返回false if(S.top == S.base){ return true; } else{ return false; } } /*------------------------------------------------------*/ /*------非递归先根遍历------*/ Status PreOrderTraverse(TreeNode T){ if( T == NULL ){ return NotOK; } else{ printf("PreOrderTraverse:"); Stack TreeStack; InitStack(TreeStack); while( T!=NULL || !StackIsEmpty(TreeStack)){ while(T != NULL){ printf("%c",T->data); Push(TreeStack,T); T = T->Lchild; } if(!StackIsEmpty(TreeStack)){ Pop(TreeStack,T); T = T->Rchild; } } printf("\n"); return OK; } } /*--------------------------*/ /*------非递归后根遍历------*/ Status PostOrderTraverse(TreeNode T){ if( T == NULL ){ return NotOK; } else{ printf("PostOrderTraverse:"); Stack TreeStack; TreeNode last; TreeNode current; Push(TreeStack,T); while(!StackIsEmpty(TreeStack)){ GetTop(TreeStack,current); if((current->Lchild==NULL && current->Rchild==NULL) || (last != NULL && (last == current->Lchild || last == current->Rchild))){ printf("%c",current->data); Pop(TreeStack, last); } else{ if(current->Rchild != NULL){ Push(TreeStack,current->Rchild); } if(current->Lchild != NULL){ Push(TreeStack,current->Lchild); } } } printf("\n"); return OK; } } /*--------------------------*/ /*-----主函数-----*/ int main(){ TreeNode TreeRoot; TreeRoot = CreateBiTree(); PreOrderTraverse(TreeRoot); PostOrderTraverse(TreeRoot); } /*----------------*/
运行结果:
相关文章推荐
- 数据结构与算法学习记录--二叉树的创建,递归遍历,非递归遍历的实现
- 数据结构与算法分析学习笔记--第四章(搜索二叉树,递归和非递归实现删除、插入)
- 数据结构学习笔记8——简单二叉树的实现与遍历
- 【算法学习笔记】10.数据结构基础 二叉树初步练习3(遍历与递归复习)
- 数据结构与算法分析学习笔记--第四章(二叉树:创建、递归遍历、非递归遍历、根据数据删除结点等)
- 【应聘笔记系列】二叉树的递归与非递归遍历实现
- 数据结构学习笔记-二叉树的前、中、后序遍历,递归、非递归方式
- 【算法学习笔记】10.数据结构基础 二叉树初步练习3(遍历与递归复习)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- 二叉树的遍历(非递归实现)
- php学习笔记 数组遍历实现代码
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- 二叉树的遍历 非递归 C实现——先序遍历篇
- 每天学习一算法系列(35)(递归和非递归俩种方法实现二叉树的前序遍历)
- 树——二叉树的遍历之递归实现
- 二叉树的遍历(C++非递归实现)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树先序、中序、后序遍历的非递归实现
- STL学习笔记:用非递归的方法实现汉诺塔问题
- 二叉树的遍历方法及递归实现