| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1351 人关注过本帖
标题:[求助]关于递归我有些不明白
只看楼主 加入收藏
菜鸟上路
Rank: 4
等 级:贵宾
威 望:14
帖 子:1120
专家分:0
注 册:2006-3-21
收藏
得分:0 
结束本次调用。
能找到多个,但是不能全部找到。

2006-06-08 18:39
gaobaoqiang
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-5-12
收藏
得分:0 

#include <stdio.h>
#include <stdlib.h>

/*二叉树的链式存储表示 */
typedef char DataType; /*应由用户定义DataType的实际类型 */
typedef struct node
{ DataType data;
struct node *lchild, *rchild; /*左右孩子指针 */
} BinTNode; /*结点类型 */
typedef BinTNode *BinTree;


/*构造二叉链表 */
void CreateBinTree(BinTree *T)
{
char ch;
if ((ch=getchar())==' ')
*T=NULL;
else
{ /*读入非空格 */
*T=(BinTNode *)malloc(sizeof(BinTNode)); /*生成结点 */
(*T)->data=ch;
CreateBinTree(&(*T)->lchild ); /*构造左子树 */
CreateBinTree(&(*T)->rchild ); /*构造右子树 */
}
}
/*按树状打印二叉树
void PrintTree(BinTree boot,int n){

if(boot==NULL)return;
PrintTree(boot->rchild,n+1);
for(int i=0;i<n;i++)
printf(" ");
printf("%c\n",boot->data);
PrintTree(boot->lchild,n+1);
}
//用广义表表示二叉树
void ListBinTree(BinTree T)
{
if (T!=NULL)
{
printf("%c",T->data);
if (T->lchild!=NULL||T->rchild!=NULL)
{
printf("(");
ListBinTree(T->lchild);
if (T->rchild!=NULL)
printf(",");
ListBinTree(T->rchild);
printf(")");
}
}
}
*/
//用凹入表表示二叉树
void DisplayBinTree(BinTree T)
{
BinTree stack[100],p;
int level[100],top,n,i;
if (T!=NULL)
{
printf("用凹入表表示二叉树:\n");
top=1;
stack[top]=T;
level[top]=3;
while(top>0)
{
p=stack[top];
n=level[top];
for (i=1;i<=n; i++)
printf(" ");
printf("%c\n",p->data);
top--;
if (p->rchild!=NULL)
{
top++;
stack[top]=p->rchild;
level[top]=n+3;
}
if (p->lchild!=NULL)
{
top++;
stack[top]=p->lchild;
level[top]=n+3;
}
}
}
}

//前序遍历二叉树
void Preorder(BinTree T)
{
if(T)
{
printf("%c",T->data); //访问结点
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void main()
{

BinTree T;
printf("请输入先序序列(虚结点用空格表示):\n");
CreateBinTree(&T);
DisplayBinTree(T);}

2006-06-08 19:02
gaobaoqiang
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-5-12
收藏
得分:0 

用递归建立数并打印树

2006-06-08 19:04
快速回复:[求助]关于递归我有些不明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027373 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved