树的几种遍历方式(递归/非递归)
来源:互联网 发布:屏保制作软件 编辑:程序博客网 时间:2024/06/05 12:47
树的几种遍历方式,前序遍历,中序遍历,后序遍历,包括它的递归实现以及非递归实现
#include<stdio.h>#include<stdlib.h>typedef struct tree{ int data; struct tree *left; struct tree *right; int flag ;}*pnode,node;pnode createbinary(int a[],int n,pnode p);pnode insertNode(int data,pnode root);void print(pnode root);void print1(pnode root);void print2(pnode root);void print3(pnode root);void print4(pnode root);void print5(pnode root);void print6(pnode root);void main(){ int a[9] = {6,3,8,5,2,9,4,7,11}; pnode root = NULL; root = createbinary(a,9,root); print6(root); free(root);}pnode createbinary(int a[],int n,pnode root){ for(int i=0; i<n; i++) { root = insertNode(a[i],root); } return root;}pnode insertNode(int value,pnode root){ pnode currentnode; pnode parentnode; pnode newnode = (pnode)malloc(sizeof(node)); newnode->data = value; newnode->left = NULL; newnode->right = NULL; if(root == NULL) { root = newnode; } else { currentnode = root; while(currentnode != NULL) { parentnode = currentnode; if(currentnode->data > value) currentnode = currentnode->left; else currentnode = currentnode->right; } if(parentnode->data > value) { parentnode->left = newnode; } else { parentnode->right = newnode; } } return root;}void print(pnode root)//后序遍历{ if(root != NULL) { print(root->left); print(root->right); printf("%d\t",root->data); }}void print1(pnode root)//前序遍历{ if(root != NULL) { printf("%d\t",root->data); print(root->left); print(root->right); }}void print2(pnode root)//中序遍历{ if(root != NULL) { print(root->left); printf("%d\t",root->data); print(root->right); }}void print3(pnode root) //层序遍历,队列存取{ pnode q = NULL; pnode Q[50]= {NULL}; int front = -1,rear = -1; if(root == NULL) { return ; } Q[++rear] = root; while(front != rear) { q = Q[++front]; printf("%d\t",q->data); if(q->left != NULL) Q[++rear] = q->left; if(q->right != NULL) Q[++rear] = q->right; }}void print4(pnode root) //前序遍历非递归{ int top = -1; pnode stack[50] = {NULL}; while(top != -1||root != NULL) { while(root != NULL) { printf("%d\t",root->data); stack[++top] = root; root = root->left; } if(top != -1) { root = stack[top--]; root = root->right; } }}void print5(pnode root) //中序遍历非递归实现{ int top = -1; pnode stack[50] = {NULL}; while(top != -1||root != NULL) { while(root != NULL) { stack[++top] = root; root = root->left; } if(top != -1) { root = stack[top--]; printf("%d\t",root->data); root = root->right; } }}void print6(pnode root) //后序遍历非递归算法{ int top = -1; pnode stack[50] = {NULL}; pnode prenode = NULL; while(top != -1||root !=NULL) { while(root !=NULL)//一直找到最左节点 { stack[++top] = root; root = root->left; } root = stack[top]; if(root->right == NULL || root->right == prenode)//如果右节点被访问,或者右节点不存在 { printf("%d\t",root->data); prenode = root; top--; root = NULL; } else { root = root ->right; } }}
阅读全文
1 0
- 树的几种遍历方式(递归/非递归)
- 非递归方式遍历二叉树的几种方法
- 四种遍历二叉树的方式(递归,非递归)
- 二叉树的三种遍历方式(递归、非递归和Morris遍历)
- 二叉树的几种遍历(递归,非递归)JAVA实现
- 树的遍历(递归、非递归)
- 二叉树的两种遍历方式[递归遍历][非递归遍历]
- 二叉树的四种遍历方式(非递归和递归)
- 二叉树的三种遍历方式(递归和非递归)
- 二叉树的三种遍历方式(递归与非递归详细实现)
- 二叉树的三种遍历方式(递归 和 非递归)
- 二叉树的三种遍历(递归+非递归)
- 二叉树的四种遍历(递归、非递归)
- 树的三种遍历(递归和非递归)
- 二叉树的前中后序遍历,递归和非递归方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 老版的IntelliJ IDEA启动太慢?教你安装破解最新版的IntelliJ IDEA
- inflate方法与getViewByID和setContentView的区别
- Tutorialspoint ABAP教程小结
- 服务器相关知识
- java诊断工具
- 树的几种遍历方式(递归/非递归)
- 实习一个月
- Request和Response详解
- SQL 查询语句
- 解决Smartgit中始终出现Please make sure you have the correct access rights问题
- 0728 Java关键字break、continue、return
- Maven学习总结(四)——pom.xml
- Windows Sockets网络编程(4)套接字重叠IO模型
- 编写函数判断当前的机器大端小端