二叉树遍历
来源:互联网 发布:矩阵图怎么做 编辑:程序博客网 时间:2024/06/13 07:15
#include<stdio.h>
//定义节点类型
typedef struct node //定义节点类型
{
char date; //根
struct node * LChild; //左孩子指针
struct node * RChild; //右孩子指针
typedef struct node //定义节点类型
{
char date; //根
struct node * LChild; //左孩子指针
struct node * RChild; //右孩子指针
}BTnode,*BiTree;
//创建一个新的二叉树
void CreateBiTree(BiTree * bitree)
{
char c = getchar();
if(c=='.')
{
*bitree=NULL;
}
else
{
(*bitree)=(BiTree)malloc(sizeof(BTnode));//建立一个新结点(*bitree
/* 给根,左,右传参*/
(*bitree)->date = c;
CreateBiTree(&((*bitree)->LChild)); //指针传参取地址
CreateBiTree(&((*bitree)->RChild));
}
}
//先序遍历
void PreOrder(BiTree root)
{
if (root != NULL)
{
printf("%c",root->date); //访问根结点
PreOrder(root->LChild); //遍历左子树
PreOrder(root->RChild); //遍历右子树
}
}
//中序遍历
void Inorder(BiTree root)
{
if(root != NULL)
{
Inorder(root->LChild); //左
printf("%c",root->date); //根
Inorder(root->RChild); //右
}
}
{
if(root != NULL)
{
Inorder(root->LChild); //左
printf("%c",root->date); //根
Inorder(root->RChild); //右
}
}
//后序遍历
void PostOrder(BiTree root)
{
if(root !=NULL)
{
PostOrder(root->LChild); //左
PostOrder(root->RChild); //右
printf("%c",root->date); //根
}
}
void PostOrder(BiTree root)
{
if(root !=NULL)
{
PostOrder(root->LChild); //左
PostOrder(root->RChild); //右
printf("%c",root->date); //根
}
}
int main()
{
{
BiTree tree;
CreateBiTree(&tree);
PreOrder(tree);
Inorder(tree);
PostOrder(tree);
CreateBiTree(&tree);
PreOrder(tree);
Inorder(tree);
PostOrder(tree);
}
阅读全文
0 0
- 二叉树遍历、分层遍历
- 遍历二叉树--二叉树
- 【树】遍历二叉树
- 二叉树遍历
- 二叉树遍历
- 二叉树的遍历
- (原创)遍历二叉树
- 二叉树遍历-php
- 查找--遍历二叉树
- 遍历二叉树
- 二叉树遍历规则
- 二叉树的遍历
- 二叉树遍历
- 遍历二叉树
- 二叉树遍历问题
- 二叉树遍历
- 二叉树遍历(zz)
- 二叉树的遍历
- 融云助制造业移动信息化建设 加速两化融合创新
- Linux应用程序设置进程调度策略
- JavaScript中的'use strict'
- android handle 消息发送和接受!!!!!
- Maven项目pom.xml各标签的含义
- 二叉树遍历
- html 输入框 只能输入数字 只能输入字母数字组合
- struts2令牌环token防止多次请求登录
- Oracle查询树结构表
- Spring MVC使用Flash Attribute展现提示消息
- 【Scikit-Learn 中文文档】数据集加载工具
- 大数据学习1——大数据概述
- solr6简单搭建 solr7可以
- SpringBoot项目以jar包的方式导出