二叉树前序,中序,后序
来源:互联网 发布:java switch case用法 编辑:程序博客网 时间:2024/06/07 10:23
主要是自我的总结
前序遍历:根结点->左子树->右子树
中序遍历:左子树->根结点->右子树
后序遍历:左子树->右子树->根结点
二叉树的定义(为了输出好看,定义char类型):
struct binarytree{char value;binarytree* leftnode;binarytree* rightnode;binarytree(){value='0';leftnode=NULL;rightnode=NULL;}};
前序遍历:
void foresearch(binarytree*a){//前序遍历if(a==NULL)return;cout<<a->value;foresearch(a->leftnode);foresearch(a->rightnode);}
中序遍历:
void midsearch(binarytree*a){//中序遍历if(a==NULL)return;midsearch(a->leftnode);cout<<a->value;midsearch(a->rightnode);}
后序遍历:
void backsearch(binarytree*a){//后序遍历 if(a==NULL)return;backsearch(a->leftnode);backsearch(a->rightnode);cout<<a->value;}
利用前序序列和中序序列构造二叉树:
void CreateTree(binarytree*a,string preArray,string midArray){//利用前序和中序构造二叉树if(preArray=="")return;binarytree A;A.value=preArray[0]; //将前序序列的第一个当根节点int index;index=midArray.find(A.value);binarytree B;binarytree C;CreateTree(&B,preArray.substr(1,index),midArray.substr(0,index)); //进行分割,创造左子树CreateTree(&C,preArray.substr(1+index),midArray.substr(1+index)); //进行分割,创造右子树A.leftnode=&B;A.rightnode=&C;return;}
阅读全文
0 0
- 二叉树前序、中序、后序遍历
- 二叉树前序,中序,后序求法
- 二叉树前序、中序、后序遍历
- 二叉树前序,中序,后序遍历
- 二叉树前序,中序,后序
- 二叉树前序、中序、后序遍历
- 二叉树前序 中序 后序
- 二叉树前序、中序、后序遍历
- 二叉树前序遍历、中序遍历和后序遍历
- 二叉树前序,中序,后序遍历详解
- 二叉树前序,中序,后序遍历详解
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 5.1 父类和子类的类型转换总结
- Python中的print用法
- mapreduce工作流程图(带注解)
- Linux搭建tomcat+jdk+mysql环境
- renderer.material 方法过时
- 二叉树前序,中序,后序
- 启程篇
- 图像标注工具labelImg安装教程 Ubuntu16.04和win7
- 文章标题
- 堆排序在C#中的实现
- MySQL 非root用户创建database
- 排座位(百度2017秋招真题)
- 文章标题
- 循环赛日程表