c语言实现二叉树的遍历和创建程序(附带注释)
来源:互联网 发布:java dos 编辑:程序博客网 时间:2024/05/19 20:44
/******************************************************************/
//树的递归思想,把每个节点当作是一棵树,以后序遍历为例
//步骤1:访问左子树.2访问右子树3.打印当前节点的值
//在节点遍历时如果某节点没有左节点或者是右节点是传递的指针是NULL
//说明下一步没有树了可以返回
/******************************************************************/
typedef char data_node;
typedef struct NODE
{
struct NODE *l_node,*r_node;
data_node value;
} node;
/******************************************************************/
//function:preordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int preordertraversal(node *pointer)
{
if(NULL == pointer) //is a new bitree? if not, return.
return 0;
else
{
//print it's value before ,print it's child tree
printf("%c ",pointer->value); //print value of this node
preordertraversal(pointer->l_node); //enter left tree
preordertraversal(pointer->r_node); //enter right tree
}
return 0;
}
/******************************************************************/
//function:inordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int inordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
inordertraversal(pointer->l_node); //enter left tree
//In this time ,left childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value); //print value of this node
inordertraversal(pointer->r_node); //enter right tree
}
return 0;
}
/******************************************************************/
//function:postordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int postordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
postordertraversal(pointer->l_node); //enter left tree
postordertraversal(pointer->r_node); //enter right tree
//In this time , two childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value); //print value of this node
}
return 0;
}
/******************************************************************/
//function:creat a new bitree
//argument:pointer to a char array contain many node and these nodes according to the preorder traversal is saved.
{
static int i=0; //静态存储防止递归调用时丢失上一次调用后i所指的a数组的位置
node *pointer;
data_node value;
if('*' == (value = a[i++]))
return NULL;
else
{
pointer = (node *)malloc(sizeof(node)); //创建该节点
pointer->value = value; //先为该节点赋值
pointer->l_node = creat_bitree(a); //创建该节点的左子树
pointer->r_node = creat_bitree(a); //创建该节点的右子树
}
//In this time,new bitree tree was be created and now return the point to this bitree to user. or last node
return pointer;
}
//树的递归思想,把每个节点当作是一棵树,以后序遍历为例
//步骤1:访问左子树.2访问右子树3.打印当前节点的值
//在节点遍历时如果某节点没有左节点或者是右节点是传递的指针是NULL
//说明下一步没有树了可以返回
/******************************************************************/
typedef char data_node;
typedef struct NODE
{
struct NODE *l_node,*r_node;
data_node value;
} node;
/******************************************************************/
//function:preordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int preordertraversal(node *pointer)
{
if(NULL == pointer) //is a new bitree? if not, return.
return 0;
else
{
//print it's value before ,print it's child tree
printf("%c ",pointer->value); //print value of this node
preordertraversal(pointer->l_node); //enter left tree
preordertraversal(pointer->r_node); //enter right tree
}
return 0;
}
/******************************************************************/
//function:inordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int inordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
inordertraversal(pointer->l_node); //enter left tree
//In this time ,left childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value); //print value of this node
inordertraversal(pointer->r_node); //enter right tree
}
return 0;
}
/******************************************************************/
//function:postordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int postordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
postordertraversal(pointer->l_node); //enter left tree
postordertraversal(pointer->r_node); //enter right tree
//In this time , two childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value); //print value of this node
}
return 0;
}
/******************************************************************/
//function:creat a new bitree
//argument:pointer to a char array contain many node and these nodes according to the preorder traversal is saved.
/******************************************************************/
//假设我们用二叉树前序遍历的思维,遍历结果保存在数组中,将printf打印改成malloc创建节点即可 ,
//注意:在遍历过程中如果当前节点没有左子树或者是右子树就用×代替,以达到给递归创造返回条件的效果
node *creat_bitree(char a[]){
static int i=0; //静态存储防止递归调用时丢失上一次调用后i所指的a数组的位置
node *pointer;
data_node value;
if('*' == (value = a[i++]))
return NULL;
else
{
pointer = (node *)malloc(sizeof(node)); //创建该节点
pointer->value = value; //先为该节点赋值
pointer->l_node = creat_bitree(a); //创建该节点的左子树
pointer->r_node = creat_bitree(a); //创建该节点的右子树
}
//In this time,new bitree tree was be created and now return the point to this bitree to user. or last node
return pointer;
}
1 0
- c语言实现二叉树的遍历和创建程序(附带注释)
- 二叉树的创建与遍历(C语言实现)
- C语言实现二叉树的创建以及遍历(递归)
- C语言实现二叉树的遍历
- 二叉树的创建、前/中/后序遍历、按层遍历C语言实现
- 二叉树创建、遍历、求深度--C语言实现
- 3.14(c程序实现)已知二叉树的前序遍历和中序遍历,求后序遍历
- C语言之二叉树(包括遍历的实现)
- 二叉树遍历的递归和非递归实现(C语言)
- C语言实现二叉树的递归遍历和非递归遍历
- 用c语言编程实现二叉树的建立和遍历二叉树
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- c语言二叉树创建及遍历
- 二叉树 二叉树的性质 存储结构 遍历二叉树 C实现二叉树的创建和遍历 线索二叉树
- 二叉树 二叉树的性质 存储结构 遍历二叉树 C实现二叉树的创建和遍历 线索二叉树
- 二叉树的创建、前序中序后序递归遍历与非递归遍历、层序遍历以及二叉树简单应用的C语言实现
- 二叉树的创建、遍历及搜索(C实现)
- sql server2008支持json函数以及2016版内置json函数
- POJ 3689 Equations 已翻译
- 不能访问 33GB 卷
- JavaEE-JSF
- Java学习路线
- c语言实现二叉树的遍历和创建程序(附带注释)
- 如何优化JAVA代码及提高执行效率
- 用新的思维方式去解决问题
- 华中农大HZAUOJ 1098 Yifan and War3 区间dp
- 将Django应用部署到Apache服务器
- 带权最短路 Dijkstra, SPFA, Bellman-Ford, ASP, Floyd-Warshall 算法分析
- hdu 3595 GG and MM (Every-SG游戏)
- 四、指定struts2处理请求的后缀
- 基于mac上搭建python开发环境