二叉树的创建、先序、中序、后序遍历
来源:互联网 发布:电气火灾监控软件 编辑:程序博客网 时间:2024/05/29 04:41
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct bitree
{
int data;
struct bitree *lchild;
struct bitree *rchild;
}bitree_s,*pbitree;
//以先序方式,创建一颗二叉树;
int creat_tree(pbitree *T,int j,int* i)
{
(*T) = (bitree_s *)malloc(sizeof(bitree_s));
if(!(*T)) return ERROR;
(*T)->data = (*i);
printf("%d\n",((*T)->data));
(*T)->lchild = NULL;
(*T)->rchild = NULL;
(*i) += 1;
j -= 1;
if(j==0)
{
return OK;
}
creat_tree(&((*T)->lchild),j,i);
creat_tree(&((*T)->rchild),j,i);
}
//以先序进行遍历
int preorder(pbitree T)
{
if(!T) return ERROR;
printf("%d\n",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
//以中序进行遍历
int minorder(pbitree T)
{
if(!T) return ERROR;
minorder(T->lchild);
printf("%d\n",T->data);
minorder(T->rchild);
}
//以后序进行遍历
int lastorder(pbitree T)
{
if(!T) return ERROR;
lastorder(T->rchild);
printf("%d\n",T->data);
lastorder(T->lchild);
}
int main()
{
int count = 1;
int deep = 3;
pbitree tree;
printf("hello world\n");
creat_tree(&tree,deep,&count);
printf("pre order transvers\n");
preorder(tree);
printf("min order transvers\n");
minorder(tree);
printf("last order transvers\n");
lastorder(tree);
}
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct bitree
{
int data;
struct bitree *lchild;
struct bitree *rchild;
}bitree_s,*pbitree;
//以先序方式,创建一颗二叉树;
int creat_tree(pbitree *T,int j,int* i)
{
(*T) = (bitree_s *)malloc(sizeof(bitree_s));
if(!(*T)) return ERROR;
(*T)->data = (*i);
printf("%d\n",((*T)->data));
(*T)->lchild = NULL;
(*T)->rchild = NULL;
(*i) += 1;
j -= 1;
if(j==0)
{
return OK;
}
creat_tree(&((*T)->lchild),j,i);
creat_tree(&((*T)->rchild),j,i);
}
//以先序进行遍历
int preorder(pbitree T)
{
if(!T) return ERROR;
printf("%d\n",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
//以中序进行遍历
int minorder(pbitree T)
{
if(!T) return ERROR;
minorder(T->lchild);
printf("%d\n",T->data);
minorder(T->rchild);
}
//以后序进行遍历
int lastorder(pbitree T)
{
if(!T) return ERROR;
lastorder(T->rchild);
printf("%d\n",T->data);
lastorder(T->lchild);
}
int main()
{
int count = 1;
int deep = 3;
pbitree tree;
printf("hello world\n");
creat_tree(&tree,deep,&count);
printf("pre order transvers\n");
preorder(tree);
printf("min order transvers\n");
minorder(tree);
printf("last order transvers\n");
lastorder(tree);
}
阅读全文
0 0
- 二叉树的创建,先序遍历,中序遍历,后序遍历
- 二叉树的递归创建,先序(中序、后序)递归遍历二叉树
- 二叉树的操作(二叉树的创建、先序遍历--->先根、中序遍历---->先左、后续遍历--->后根)
- 先序遍历/后序遍历创建线索二叉树
- 二叉树创建、先序遍历、中序遍历、后序遍历、树深度
- C++ 二叉树实现 创建,先序遍历,中序遍历,后序遍历
- 创建一个二叉树(先序遍历、中序遍历、后序遍历)
- 根据中序遍历和先序遍历,后序遍历创建二叉树。
- 二叉树的创建及递归的先,中,后序遍历
- 二叉树的创建和先序,中序,后序,递归,非递归遍历
- 二叉树的创建、先序、中序以及后序遍历
- 数据结构---二叉树的创建、先序、中序、后序遍历
- 二叉树的创建与三种遍历(先序,中序,后序)
- 二叉树递归的创建及三种遍历(先序、中序、后序)
- 二叉树的创建、先序、中序、后序遍历
- 二叉树(先序创建,递归先序,中序,后序遍历)-----1
- 二叉树的遍历(先序、中序、后序)
- 二叉树的先序、中序、后序遍历
- spring面向切面编程aop的学习(1)
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的办法
- Wannafly挑战赛5 D题题解 数学
- ajax使用函数中的this变量问题
- mybatis系列六:使用getMapper方式实现面向接口的编程
- 二叉树的创建、先序、中序、后序遍历
- Spring整合ActiveMQ
- 求下一个质数
- 并查集模板
- ubuntu 16.04 LTS
- 设计模式之建造者模式(与工厂模式的区别以及JDK中StringBuilder类的应用)
- Concurrent包中类的详解
- 记录数据库修改comment 和导出脚本
- 太平洋时区