急急急!求C语言的数据结构二叉树递归遍历程序!

作者&投稿:蔚青 (若有异议请与网页底部的电邮联系)
~ #include"stdio.h"//二叉树
#include"stdlib.h"
typedef
struct
node
{
char
data;
struct
node
*lchild,*rchild;
}BinTNode;
typedef
BinTNode*
BinTree;
void
GreateBinTree(BinTree
*T)//以先序遍历为依据构造二叉树,T为指向根指针的指针.
{
//空结点以空格代替.
char
ch;
if((ch=getchar())=='
')
*T=NULL;
else
{
*T=(BinTree)malloc(sizeof(BinTNode));
(*T)->data=ch;
GreateBinTree(&((*T)->lchild));
GreateBinTree(&((*T)->rchild));
}
}
void
Lorder(BinTree
T)//先序遍历二叉树.
{
if(T)
{
printf("%c
",T->data);
Lorder(T->lchild);
Lorder(T->rchild);
}
}
void
Morder(BinTree
T)//中序遍历二叉树.
{
if(T)
{
Morder(T->lchild);
printf("%c
",T->data);
Morder(T->rchild);
}
}
void
Rorder(BinTree
T)//后序遍历二叉树.
{
if(T)
{
Rorder(T->lchild);
Rorder(T->rchild);
printf("%c
",T->data);
}
}

/******************************************************/
/*
二叉树的建立深度优先遍历求叶子个数求深度
*/
/******************************************************/
#include
"stdio.h"
#include
"string.h"
#include
"stdlib.h"
#define
NULL
0
typedef
struct
bitnode{
int
data;
struct
bitnode
*lchild,*rchild;
}bitnode,*bitree;
/*创建一个二杈树以#号结束*/
bitree
create(bitree
t){
char
ch;
ch=getchar();
if(ch=='#')
t=NULL;
else{
t=(bitree)malloc(sizeof(bitnode));
t->data=ch;
t->lchild=create(t->lchild);
t->rchild=create(t->rchild);
}
return
t;
}
/*递归遍历*/
void
preorder(bitree
t){
if(t){
printf("%c",t->data);
/*先序*/
preorder(t->lchild);
/*printf("%c",t->data);
中序*/
preorder(t->rchild);
/*printf("%c",t->data);
后序*/
}
}
/*求深度*/
int
depth(bitree
t){
int
depthval,depl,depr;
if(!t)
depthval=0;
else{
depl=depth(t->lchild);
depr=depth(t->rchild);
depthval=1+(depl>depr?depl:depr);
}
return
depthval;
}
/*求叶子数*/
int
countleaf(bitree
t){
int
count=0;
if(!t)
count=0;
else
if((!t->lchild)&&(!t->rchild))
count++;
else
count=countleaf(t->lchild)+countleaf(t->rchild);
return
count;
}
/*主函数*/
main(){
bitree
t=NULL;
printf("\nplease
input
a
tree:");
t=create(t);
preorder(t);
printf("\ndepth:%d\nleave:%d\n",depth(t),countleaf(t));
system("pause");
}
程序以调试通过!!!!!

急急急!求C语言的数据结构二叉树递归遍历程序!
答:Rorder(T->rchild);printf("%c ",T->data);} }

数据结构C语言2
答:其实链表不难,碰到复杂的,或看别人的链表程序,最重要的是要边看边画图,把关系表示出来。include <stdio.h> include <stdlib.h> typedef int elemtype;typedef struct LNode { elemtype data;struct LNode *next;}LNode,*LinkList;void create_list(LinkList &L,int n){ int i;LinkList p,...

c语言常见的数据结构有哪些?
答:(1)线性数据结构:元素之间一般存在元素之间存在一对一关系,是最常用的一类数据结构,典型的有:数组、栈、队列和线性表 (2)树形结构:结点间具有层次关系,每一层的一个结点能且只能和上一层的一个结点相关,但同时可以和下一层的多个结点相关,称为“一对多”关系,常见类型有:树、堆...

C语言 数据结构 二叉树实现的疑问
答:C语言 数据结构 二叉树实现的疑问 先敬仰一下楼主的勤奋!我主要针对第二个算法说,我觉得上面这段话也是在讲第二个算法。其实两个算法差不太多。1. 栈顶记录中的指针其实就是指栈顶,每次push()进去或者pop()出来的那个p。他代表的是正在访问的节点得下一个节点。比如,访问一个树t的左子树t-...

数据结构 c语言版二叉树(1) 建立一棵含有n个结点的二叉树,采用二叉链 ...
答:printf("%c",ptr->ch);} } void main(){ printf("构建一个二叉树(结点数为n):\n");root=create(root);printf("前序遍历二叉树:\n");preorder(root);printf("\n");printf("中序遍历二叉树:\n");inorder(root);printf("\n");printf("后序遍历二叉树:\n");postorder(root);...

数据结构教程(详细又简单——C语言实现)
答:树与二叉树:层次结构的奥秘 树,尤其是二叉树,是层次分明的数据结构,可以是有序或无序,甚至是特殊的如满二叉树或完全二叉树。二叉树每个节点最多有两个子节点,如决策树在机器学习中的应用。深入探索与学习资源 想要了解更多C语言数据结构的精彩细节和实现示例,可以参考相关教程专栏和链接,那里有...

C语言编写的数据结构
答:C语言编写的数据结构 实验一:用二叉链表作为存储结构,建立二叉树,对二叉树进行前序、后序遍历,并对建立的二叉树进行中序线索,再中序线索遍历。实验二:根据给定的权值建立哈夫曼树,进行前序遍历。/*建... 实验一:用二叉链表作为存储结构,建立二叉树,对二叉树进行前序、后序遍历,并对建立的二叉树进行中序线索,...

c语言编程 数据结构题
答:NODE *head = NULL; head = createlist(head, arrlen, arr); if (NULL == head) { printf("Failed to create list . Program Eixt . \n"); return 0; } printf("\n"); printlist(head); if (1 == insertnode(head, 2, 5)) { printf("\nIns...

求解数据结构(C语言): 求两个对称矩阵的和 谢谢!
答:1由A,B,C 三个结点构成的二叉树,共有多少种不同的结构 2给定表(55,63,44,38,75,80,31,56),用筛选法建立初始栈,则处世栈表为:?3已知二叉树中叶子数为50,仅有一个孩子的结点数为30,则总结点数为多少?4已知8个数据元素由(35,75,40,15,20,55,95,65)按照依次...