二叉树遍历-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 ...
随机推荐
- MySQL5.7.23 VS MySQL5.6.21 分区表性能对比测试
为评估MySQL从5.6.21升级到5.7.23版本的性能,针对分区表的读写做了对比测试. [测试环境] 1. 两台HP380的物理机,配置一致,CPU:Intel(R) Xeon(R) CPU E5 ...
- linux 学习笔记 mysql安装总结
1 安装方式 下载2禁制源码安装包 mysql-5.5.27-linux2.6-i686.tar.gz 备注:2禁制额包解压缩后直接就可以使用 不用Make 2 步骤 shell>groupad ...
- XamarinSQLite教程添加列
XamarinSQLite教程添加列 如果开发者想要在现有的表中添加列,并不需要删除重新创建数据表,只需要修改数据表.操作步骤如下. (1)右击需要添加列的表,单击Add column…(beta)命 ...
- 【PYTHON】 Missing parentheses in call to 'print'
Microsoft Windows [版本 10.0.15063] (c) 2017 Microsoft Corporation.保留所有权利. C:\Users\Jam>python Pyth ...
- Django 自定义模板语法
from django import template from django.utils.safestring import mark_safe register = template.Librar ...
- Linux vi/vim命令高效助记图
图片来源网上,如有侵权,请告知,我会删除掉,谢谢~ 常用编辑按键: 1 vi +[num] file 打开文件,并将光标置于第n行首 2 vi + file 打开文件,并将光标置于最后一行首 3 vi ...
- C#编程 - 交通灯模拟
程序写的有点繁杂,但大体功能出来的! 效果图: using System; using System.Collections.Generic; using System.Linq; using Sys ...
- python网络编程(十)
select版-TCP服务器 1. select 原理 在多路复用的模型中,比较常用的有select模型和epoll模型.这两个都是系统接口,由操作系统提供.当然,Python的select模块进行了 ...
- Windbg live debug步骤
步骤: 1.安装windbg对应版本:X86,X64 2.Attach到对应进程 3.加载SOS:.load sos clr 4.启用捕获frist chance exception: sxe clr ...
- Understanding Built-In User and Group Accounts in IIS 7
Understanding Built-In User and Group Accounts in IIS 7 By lzb October 19, 2018 Introduction In earl ...