求一个二叉树的完整程序,包括前序遍历创建二叉树,线索化,中序遍历二叉树,C语言版的!

shaoxiaojing5193 2011-05-27 04:49:28
求一个二叉树的完整程序,包括三个函数:前序遍历创建二叉树,线索化二叉树,中序遍历二叉树。C语言版的!小弟是初学者阿,希望前辈帮忙写个完整的程序,小弟在此先感谢了!(是普通的二叉树噢,小弟刚学。)
...全文
1209 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
newfarmerchi 2011-05-29
  • 打赏
  • 举报
回复

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

struct node
{
char data;
node *rchild,*lchild;
int rtag,ltag;
};

void create(node *&b)//创建二叉树
{
char c;
c=getchar();
getchar();//吸收回车,清空
if(c==' ')
b=NULL;
else
{
b=(node *)malloc(sizeof(node));
b->data=c;
b->rtag=0;
b->ltag=0;
create(b->lchild);
create(b->rchild);
}
}

void zxbl(node *b)//中序遍历
{
if(b!=NULL)
{
zxbl(b->lchild);
cout<<b->data;
zxbl(b->rchild);
}
}

node *pre=NULL;

void thread(node *&b)
{
if(b!=NULL)
{
thread(b->lchild);
if(b->lchild==NULL)
{
b->lchild=pre;
b->ltag=1;
}
//else
//b->ltag=0;
if(pre->rchild==NULL)
{
pre->rchild=b;
pre->rtag=1;
}
//else
//pre->rtag=0;
pre=b;
thread(b->rchild);
}
}
node * xstree(node *b)
{
node *root=(node *)malloc(sizeof(node));
root->rtag=1;
root->ltag=0;
root->rchild=root;
if(b==NULL)
root->lchild=root;
else
{
root->lchild=b;
pre=root;
thread(b);
pre->rchild=root;
pre->rtag=1;
root->rchild=pre;
}
return root;
}

void shuchu(node *b)
{
node *p=b->lchild;
while(p!=b)
{
while(p->ltag==0)
p=p->lchild;
cout<<p->data;
while(p->rtag==1&&p->rchild!=b)
{
p=p->rchild;
cout<<p->data;
}
p=p->rchild;
}
cout<<endl;
}





void main()
{
node *root;
cout<<"请输入二叉树的先序遍历序列,以建立二叉树:"<<endl;
create(root);
cout<<"二叉树的中序遍历为:";
zxbl(root);
cout<<endl;
root=xstree(root);
cout<<"线索二叉树的输出:";
shuchu(root);
}






shaoxiaojing5193 2011-05-29
  • 打赏
  • 举报
回复
我要有线索化的阿,还有标志域 lrag,rrag的结构。。。
yfk 2011-05-27
  • 打赏
  • 举报
回复

#include<stdlib.h>
#include<stdio.h>
#define MAX 50
#define MAS 20
#define CHAR 1
#if CHAR
typedef char TElemType;
TElemType Nil=' ';
#define form "%c"
#else
typedef int TElemType;
TElemType Nil=0;
#define form "%d"
#endif
typedef struct node
{TElemType data;
struct node *left;
struct node *right;
struct node *parent;
}BiTNode,*BiTree;
BiTNode *InitBiTree(BiTNode *bt)
{
bt=NULL;
return bt;
}
BiTNode *CreateBiTree(BiTNode *bt)
{TElemType ch;
scanf(form,&ch);
if(ch==Nil) bt=NULL;
else
{bt=(BiTNode *)malloc(sizeof(BiTNode));
if(!bt) exit(0);
bt->data=ch; bt->parent=NULL;
bt->left=CreateBiTree(bt->left);
if(bt->left) bt->left->parent=bt;
bt->right=CreateBiTree(bt->right);
if(bt->right) bt->right->parent=bt;
}
return bt;
}
void PrintTree(BiTNode *bt,int i)
{ if(bt!=NULL)
{PrintTree(bt->right,i+5);
#if CHAR
if(bt->data!=Nil)
printf("%*c\n",i,bt->data);
#else
if(bt->data!=Nil)
printf("%*d\n",i,bt->data);
#endif
PrintTree(bt->left,i+5);
i=i-5;
}
}
void Prorder1(BiTNode *bt,void(*visit)(TElemType))/*先序遍历*/
{if(bt!=NULL)
{visit(bt->data);
Prorder1(bt->left,visit);
Prorder1(bt->right,visit);
}
}
void Prorder2(BiTNode *bt,void(*visit)(TElemType))/*中序遍历*/
{BiTNode *p,*stack[MAS];
int top;
top=0; p=bt;
while(top!=0||p!=NULL)
{while(p!=NULL)
{stack[top]=p; top++;
p=p->left;
}
if(top!=0)
{p=stack[top-1];
top--;
visit(p->data);
p=p->right;
}
}
}
void Prorder3(BiTNode *bt,void(*visit)(TElemType))/*后序遍历*/
{BiTNode *p,*stack[MAS];
int top;
top=0;
stack[top]=bt; top++;
while(top>0)
{p=stack[top-1]; top--;
while(p!=NULL)
{visit(p->data);
stack[top]=p->right;
top++;
p=p->left;
}
}
}
void visit(TElemType e)
{printf(form" ",e);
}
int SumLefts(BiTNode *bt,int sum)
{
if (bt!=NULL)
{
if (bt->left==NULL && bt->right==NULL)
{
printf("%4c",bt->data); sum++;
}
sum=SumLefts(bt->left,sum);
sum=SumLefts(bt->right,sum);
}
return(sum);
}
int SumTree(BiTNode *bt)
{static int sum=0;
if(bt!=NULL)
{printf("%4c",bt->data);
sum++;
sum=SumTree(bt->left);
sum=SumTree(bt->right);
}
return(sum);
}
BiTNode *Findchar(BiTNode *bt,char ch) /*二叉树查找结点*/
{BiTNode *p; /*利用函数名返回结果*/
if(bt!=NULL)
{if(bt->data==ch) p=bt;
p=Findchar(bt->left,ch);
p=Findchar(bt->right,ch);
}
if(p!=NULL) return(p);
else return(NULL);
}

main()
{ int j,i,a,sum=0;
BiTree bt;
bt=InitBiTree(bt);
#if CHAR
printf("请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n");
#else
printf("请先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n");
#endif
bt=CreateBiTree(bt);
printf("输入建立的二叉树!!!\n");
PrintTree(bt,5);
do{
printf("------------------------------------------------------------");
printf("\n 主菜单");
printf("\n 1 二叉树先序遍历");
printf("\n 2 二叉树中序遍历");
printf("\n 3 二叉树后序遍历");
printf("\n 4 二叉树叶子结点数");
printf("\n 5 二叉树结点数");
printf("\n 6 二叉树查找x结点");
printf("\n 0 退出");
printf("\n----------------------------------------------------------");
printf("\n");
printf("输入你要选择的数据:");
scanf("%d",&i);
switch(i)
{case 1: printf("先序遍历结果为:");
Prorder1(bt,visit);
break;
case 2: printf("后序遍历结果为:");
Prorder2(bt,visit);
break;
case 3: printf("中序遍历结果为:");
Prorder3(bt,visit);
break;
case 4: j=SumLefts(bt,sum);
printf("树的叶子结点数为%d:",j);
break;
case 5: j=SumTree(bt);
printf("树的结点数为%d:",j);
break;
case 6: printf("输入要查找的结点字符x:");
scanf("%c",&a); scanf("%*c");
j=Findchar(bt,a);
printf("要查找的结点的指针为%d:",j);
break;
case 0: exit(0);
}
printf("\n");
getch();
}while(i>0||i<8);
}


shaoxiaojing5193 2011-05-27
  • 打赏
  • 举报
回复
嗯,我知道阿,我现在想要完整的代码,光理解了算法还是不够的。。。
w74839520 2011-05-27
  • 打赏
  • 举报
回复
数据结构 C语言版本 上面有。清华大学的。

好像是伪代码。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧