二叉树的前序、中序、后序及层次遍历的递归与非递归源码实现
来源:互联网 发布:数据库冗余字段 编辑:程序博客网 时间:2024/05/22 15:39
二叉树是基本却非常重要的数据结构,经常在面试中出现,现在我主要是针对面试中出现比率非常高的前序、中序、后序及层次遍历,给出自己的递归与非递归实现,源码仅供参考。
另外,如果觉得我写的好,可以关注我的github帐号(https://github.com/chenqiangzhishen). 本文代码部分我也贴在了如下的目录中。
(https://github.com/chenqiangzhishen/Algorithms/blob/master/binaryTree/binaryTree.c)
有了github,我会经常更新些新的,高效的,经典的算法和其他新的知识与大家分享,开源快乐~
注:我的代码不出意外,都可以直接运行,采用gcc/g++/gdb进行的开发与调试工具及vim开发工具。
因为比较喜欢在Linux下编程,不大喜欢用中文切来切去的进行描述,所以直接采用英文进行注释。
#include <stdio.h>#include <stdlib.h>#define MAX 100typedef struct BiTNode { int data; struct BiTNode *lchild, *rchild;} BiTNode, *BiTree;BiTNode* stack[MAX];BiTree createBiTree(void){ int data; BiTNode *root = NULL; scanf("%d", &data); if(data == -1) { root = NULL; } else { root = (BiTNode *)malloc(sizeof(BiTNode)); root->data = data; root->lchild = createBiTree(); root->rchild = createBiTree(); } return root;}void preOrderTraverse(BiTree root){ if(root == NULL) return; printf("%d ", root->data); preOrderTraverse(root->lchild); preOrderTraverse(root->rchild);}void inOrderTraverse(BiTree root){ if(root == NULL) return; inOrderTraverse(root->lchild); printf("%d ", root->data); inOrderTraverse(root->rchild);}void postOrderTraverse(BiTree root){ if(root == NULL) return; postOrderTraverse(root->lchild); postOrderTraverse(root->rchild); printf("%d ", root->data);}void preOrderTraverse_none_recursive(BiTree root){ if(root == NULL) return; int top = 0; while(root || top>0) { while(root) { printf("%d ", root->data); stack[top++] = root; root = root->lchild; } root = stack[--top]; root = root->rchild; } printf("\n");}void inOrderTraverse_none_recursive(BiTree root){ if(root == NULL) return; int top = 0; while(root || top>0) { while(root) { stack[top++] = root; root = root->lchild; } root = stack[--top]; printf("%d ", root->data); root = root->rchild; } printf("\n");}void postOrderTraverse_none_recursive(BiTree root){ if(root == NULL) return; BiTNode *visited = NULL; int top = 0; while(root || top>0) { while(root) { stack[top++] = root; root = root->lchild; } root = stack[top -1]; //if do not have right child or the right child is visited, then visit the parent(root) node. if(root->rchild==NULL || visited==root->rchild) { printf("%d ", root->data); visited = root; --top; root = NULL;//note that: let the node become NULL } else { root = root->rchild; } } printf("\n");}void levelTraverse(BiTree root){ if(root == NULL) return; BiTNode* p = NULL; int top = 0; int beg = 0; stack[top++] = root; int end = top; while(beg < end) { while(beg < end) { p = stack[beg++]; printf("%d ", p->data); if(p->lchild) stack[top++] = p->lchild; if(p->rchild) stack[top++] = p->rchild; } end = top; }}int main(void){ printf("please input the data to create binary tree, input -1 to exit\n"); BiTree root = createBiTree(); printf("The binary tree is:\n"); printf("The preOrderTraverse is:\n"); preOrderTraverse(root); printf("\n"); printf("The inOrderTraverse is:\n"); inOrderTraverse(root); printf("\n"); printf("The postOrderTraverse is:\n"); postOrderTraverse(root); printf("\n"); printf("The preOrderTraverse with none recursive is:\n"); preOrderTraverse_none_recursive(root); printf("The inOrderTraverse with none recursive is:\n"); inOrderTraverse_none_recursive(root); printf("The postOrderTraverse with none recursive is:\n"); postOrderTraverse_none_recursive(root); printf("The level traverse for binary tree is:\n"); levelTraverse(root); printf("\n"); return 0;}
0 0
- 二叉树的前序、中序、后序及层次遍历的递归与非递归源码实现
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- 二叉树的前序、中序、后序、层次遍历的递归与非递归实现
- 初探二叉树之动态创建,前.中,后序遍历的递归及非递归实现,层次遍历,树状输出,叶节点及节点总数的统计
- 【二叉树遍历算法】——前/中/后序递归与非递归的实现
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- 二叉树的非递归遍历以及层次遍历(前序、中序、后序)
- 二叉树的先序、中序、后序的递归及非递归实现,以及层次遍历的实现:
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- shell 脚本统计当前目录下普通文件个数
- ubuntu 下使用crontab定时执行java程序
- 2.5年, 从0->阿里
- 黑马程序员————java注解初步
- Hadoop2.6.0 MapReduce 源码学习
- 二叉树的前序、中序、后序及层次遍历的递归与非递归源码实现
- div自动换行
- sqilte操作 -- 取排序的第N条数据
- Java笔记之DOS常用命令以及windows常用快捷键
- Q: IOException parsing XML document
- linux文本处理 sort,grep,sed,awk,uniq 用法
- freescale github for imx6
- IOS各种调试技巧豪华套餐
- 《剑指Offer》学习笔记--面试题42:翻转单词顺序VS坐旋转字符串