二叉树遍历-c实现
这里主要是三种遍历,先序(preorder,NLR),中序(Inorder,LNR),后序(Postorder,LRN)
N:node,L:left,R:right
基本排序:先序(NLR,节点,左,右),中序(LNR,左,节点,右),后序(LRN,左,右,节点)
要点:在每一种排序里,必须遵守基本排序。看图:
为了更加直观的了解,看下面的c语言实现的代码,参考了:https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data){
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return node;
}
void printPostorder(struct node* node){
if(node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ",node->data);
}
void printInorder(struct node* node){
if(node==NULL){
return;
}
printInorder(node->left);
printf("%d ",node->data);
printInorder(node->right);
}
void printPreorder(struct node* node){
if(node==NULL){
return;
}
printf("%d ",node->data);
printPreorder(node->left);
printPreorder(node->right);
}
int main(){
struct node *root=newNode();
root->left=newNode();
root->right=newNode();
root->left->left=newNode();
root->left->right=newNode();
root->right->left=newNode();
root->right->right=newNode();
root->left->left->left=newNode();
root->left->left->right=newNode();
root->left->right->left=newNode();
root->left->right->right=newNode();
root->right->left->left=newNode();
root->right->left->right=newNode();
root->right->right->left=newNode();
root->right->right->right=newNode();
printf("\nPreorder raversal of binary tree is \n");
printPreorder(root);
printf("\nInorder raversal of binary tree is \n");
printInorder(root);
printf("\nPostorder raversal of binary tree is \n");
printPostorder(root);
return ;
}
输出:
Preorder raversal of binary tree is Inorder raversal of binary tree is Postorder raversal of binary tree is
写一个中序输出的图解:
二叉树遍历-c实现的更多相关文章
- C++ 二叉树遍历实现
原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...
- python实现二叉树遍历算法
说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
- D - 二叉树遍历(推荐)
二叉树遍历问题 Description Tree Recovery Little Valentine liked playing with binary trees very much. Her ...
- 二叉树遍历 C#
二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...
- 二叉树——遍历篇(递归/非递归,C++)
二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...
- 二叉树遍历(flist)(二叉树,已知中序层序,求先序)
问题 C: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 76 解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...
- 二叉树遍历(flist)(已知中序和按层遍历,求先序 )
问题 F: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...
随机推荐
- APP开发,微信第三方登录的介绍
去年做了一阵APP相关的开发,经常遇到第三方登陆的需求,比如微信.微博.fb的第三方登陆等等,其实主要的流程都大同小异,这里就以微信为例来介绍,希望对大家有帮助. 微信开放平台(open.weixin ...
- .net(二)
1.维护数据库的完整性.一致性.你喜欢用触发器还是自写业务逻辑?为什么? 答:尽可能用约束(包括CHECK.主键.唯一键.外键.非空字段)实现,这种方式的效率最好:其次用触发器,这种方式可以保证无论何 ...
- Vue渲染列表,在更新data属性后,列表未更新问题
初始化代码如下var tableModel = new Vue({el: '#datatable',data: {items: listData}}); 当我对列表操作时,listData=>n ...
- AE模板
== 蓝色浩瀚星辰中一道光速展示企业LOGO AE模板 科技公司品牌开场动画. == 线条科技感图片商品介绍 Tech Grid Show免费下载 科技科幻风格. 产品介绍不错. == 炫光三维金属质 ...
- python流程控制之if、 while和for 循环
1.if 语句 语法1 if 条件:# 代码1# ... # cls='human'# sex='female'# age=18## if cls == 'human' and sex == 'fem ...
- 04 树莓派截图软件scrot的安装和使用
2017-08-22 13:52:52 sudo apt-get install scrot 捕捉活动窗口(按下回车后,3秒之内点击要捕捉的窗口): scrot -d 3 -u 捕捉选定的区域(按下回 ...
- shell脚本使用技巧4--读取字符,重复执行
ls | cat -n > out.txt 给输出的信息加行号并导出到out.txt 利用shell生成一个独立的进程 pwd; (cd /bin; ls); pwd; 开启一个子shell,不 ...
- mysql命令(command)
连接mysql命令: -uuserName -pPassword 显示表的索引: SHOW INDDEX FROM table_name 查看mysql的超时时间:SHOW GLOBAL VARIAB ...
- Python基础-列表推导式、匿名函数、os/sys/time/datetime/pymysql/xlwt/hashlib模块
列表推导式 [表达式 for 变量 in range(n) if 条件] 等效于 for 变量 in in range(n): if 条件: 表达式 优点:书写方便,缺点:不易读 注意:用的是方括号 ...
- function前加运算符实现立即执行函数
我们知道函数的调用方式通常是FunctionName() 但如果我们尝试为一个"定义函数"末尾加上(),解析器是无法理解的. function msg(){ alert('mess ...