假设二叉树采用二叉链表结构。设计并实现如下算法:输入某棵二叉树的广义表形式,建立该二叉树,并按层次遍历该二叉树。

loving_darling 2009-05-28 06:43:18
假设二叉树采用二叉链表结构。设计并实现如下算法:输入某棵二叉树的广义表形式,建立该二叉树,并按层次遍历该二叉树。 c语言版的!!!
...全文
1607 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen98059175 2010-04-21
  • 打赏
  • 举报
回复
好强大的代码
Cophie08 2010-04-14
  • 打赏
  • 举报
回复
see
sunzerui 2009-06-02
  • 打赏
  • 举报
回复
1楼的代码风格很好
我写学生系统的时候就是这样的
顶1楼!
xiaoling_315 2009-06-02
  • 打赏
  • 举报
回复
不好意思
上面有点复制错了
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100

struct BiNode{
char data;
struct BiNode *lchild;
struct BiNode *rchild;
};
typedef struct BiNode BiNode;
typedef struct BiNode *BiTree;

typedef struct
{
BiTree data[MAXSIZE];
int top;
int stacksize;
}SqStack;
typedef struct
{
BiTree s[MAXSIZE];
int front;
int rear;
}SqQueue;
void initQueue(SqQueue *qu)
{
qu->rear=qu->front=0;
}
int EnQueue(SqQueue *qu,BiTree e)
{
if((qu->rear+1)%MAXSIZE==qu->front)
return ERROR;
else
{
qu->s[qu->rear+1]=e;
qu->rear=(qu->rear++)%MAXSIZE;
return OK;
}
}
int DeQueue(SqQueue *qu,BiTree *e)
{
if(qu->rear==qu->front)
return ERROR;
else
{
*e=qu->s[qu->front];
qu->front++;
return OK;
}
}
int QueueEmpty(SqQueue qu)
{
if(qu.rear==qu.front)
return OK;
else
return ERROR;
}
int initstack(SqStack *s)
{
s->top=0;
return OK;
}
int Push(SqStack *s,BiTree e)
{
if(s->top==MAXSIZE)
return ERROR;
else{
s->data[s->top]=e;
s->top++;
return OK;
}
}
int Pop(SqStack *s, BiTree *e)
{
if(s->top==0)
return ERROR;
else{
*e=s->data[s->top-1];
s->top--;
return OK;
}
}
int Gettop(SqStack st,BiTree *e)
{
if(st.top==0)
return ERROR;
else
{
*e=st.data[st.top-1];
return OK;
}
}
int StackEmpty(SqStack s )
{
if(s.top==0)
return OK;
else
return ERROR;
}
/*void Greate(BiTree *T)
{
BiTree p,q;
SqStack s;
int flag=1;
char ch;
initstack(&s);
*T=NULL;
scanf("%c",&ch);
while(ch!='#')
{
if(ch>='A'&&ch<='Z')
{
ch='!';
}

switch(ch){
case'!':
p=(BiTree)malloc(sizeof(BiNode *));
p->data=ch;
p->lchild=p->rchild=NULL;
if(*T==NULL)
{*T=p;}
else
{
if(flag==0)
{
q=(BiTree)malloc(sizeof(BiNode *));
Gettop(s,&p);
p->lchild=q;
}

else
{
p->rchild=q;
}
}
break;

case'(':flag=0;Push(&s,p);break;
case',':flag=1;break;
case')':Pop(&s,&p);break;

}
}
}*/
void CreatebiTree(BiTree *BT)
{ SqStack s;

BiTree p,e;
int Flag=1;
char ch;
initstack(&s);
*BT=NULL;
scanf("%c",&ch);
while (ch !='#') {
switch(ch) {
case '(': { Push(&s,p);
Flag = 1;
break;}
case ',': { Flag = 0; break; }
case ')': { Pop(&s,&e); break;}
default:{
p=(BiNode *)malloc(sizeof(BiNode)) ;
p->lchild = p->rchild = NULL;
p->data = ch ;
if (!(*BT))
*BT = p ;
else { Gettop(s,&e);
if (Flag)
e->lchild = p ;
else
e->rchild = p ;
}
}
}
scanf("%c",&ch);
}

}
/* void deep_traverse(BiTree T)
{ BiTree p;
SqQueue q;
initQueue(&q);
p=T;


EnQueue(&q,p);
while (!QueueEmpty(q))
{ DeQueue(&q,&p);
printf("%c ", p->data);
if (p->lchild!=NULL) EnQueue(&q,p->lchild);
if (p->rchild!=NULL) EnQueue(&q,p->rchild);
}
}
*/
void deep_traverse(BiTree T)
{ BiTree p;
SqQueue q;
initQueue(&q);
p=T;
printf("%c ", p->data);


EnQueue(&q,p);
while (!QueueEmpty(q))
{ DeQueue(&q,&p);

if (p->lchild)
{ printf("%c ", (p->lchild)->data);
EnQueue(&q,p->lchild);
}
if (p->rchild)

{ printf("%c ", (p->rchild)->data);
EnQueue(&q,p->rchild);}
}
}


/*A(B(E(K,L),F),C(D(,H)))#*/
main()
{
BiTree t;
clrscr();
CreatebiTree(&t);
getch();
deep_traverse(t);
getch();
}
这个还好,广义表建树是正确的,不过运行好像还是有点问题,我也是做实验写的,
xiaoling_315 2009-06-02
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1

#define MAXSIZE 100

typedef char ElemType;

struct BiNode{

ElemType data;
struct BiNode *lchild;
struct BiNode *rchild;

};

typedef struct BiNode BiNode;

typedef struct BiNode *BiTree;


typedef struct
{
BiTree data[MAXSIZE];
int top;
int stacksize;

}SqStack;
typedef struct
{
BiTree s[MaxSize];
int front;
int rear;

}SqQueue;

void initQueue(SqQueue *qu)
{
qu->rear=qu->front=0;

}
int EnQueue(SqQueue *qu,SElemtype e)
{if((qu->rear+1)%MaxSize==qu->front)
return ERROR;
else
{qu->s[qu->rear+1].x=e.x;
qu->s[qu->rear+1].y=e.y;
qu->rear++;
return OK;
}
}
int DeQueue(SqQueue *qu,SElemtype *e)
{if(qu->rear==qu->front)
return ERROR;
else
{
e->x=qu->s[qu->front].x;
e->y=qu->s[qu->front].y;
qu->front++; return OK;
}

}

int QueueEmpty(SqQueue qu)
{if(qu.rear==qu.front)
return 1;
else
return 0;
}

int initstack(SqStack *s)
{
s->top=0;
return OK;
}
int Push(SqStack *s,BiTree e)
{

s->data[s->top]=e;
s->top++;
return OK;
}

int Pop(SqStack *s, BiTree *e)
{

*e=s->data[s->top-1];
return OK;
}
int Gettop(SqStack st,Bitree *e)
{if(st.top==-1)
return ERROR;
else
{
*e=st.data[st.top--];
return OK;
}
}

int StackEmpty(SqStack s )
{
if(s.top==0)
return OK;
else
return ERROR;
}

void Greate(BiTree T)
{
SqStack s;

int flag=0;
InitStack(s);

T=NULL;

scanf("%c",&ch);
while(ch!='#')
{
swith(ch):
case('A'~'Z'):
p=(BiTree)malloc(sizeof(BiNode));
p->data=ch;
p->lchild=p->rchild=NULL;
if(T=NULL)
{T=p;}
else
{
if(flag==0)
{
q=(BiTree)malloc(sizeof(BiNode));
Gettop(s,q);
q->lchild=p;
else
{
q->rchild=q;
}
}
}
case'(':
flag=0;
Push(&s,p);
case',':flag=1;
case')':Pop(&s,&p);

}
}
}
}
void deep_traverse(bitree *t)
{
queue q;
q.Enqueue(t);
while (!q.empty()) { // 如果队列不空
struct bitree *p = q.top(); //取出对头元素
q.pop();
printf("%d ", p->data);
if (p->lchild) q.push(p->lchild); //如果有左孩子,则左孩子入队
if (p->rchild) q.push(p->rchild); //如果有右孩子,则右孩子入队
}
}

/*A(B(E(K,L),F),C(D(,H)))#*/

#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100

struct BiNode{
char data;
struct BiNode *lchild;
struct BiNode *rchild;
};
typedef struct BiNode BiNode;
typedef struct BiNode *BiTree;

typedef struct
{
BiTree data[MAXSIZE];
int top;
int stacksize;
}SqStack;
typedef struct
{
BiTree s[MAXSIZE];
int front;
int rear;
}SqQueue;
void initQueue(SqQueue *qu)
{
qu->rear=qu->front=0;
}
int EnQueue(SqQueue *qu,BiTree e)
{
if((qu->rear+1)%MAXSIZE==qu->front)
return ERROR;
else
{
qu->s[qu->rear+1]=e;
qu->rear++;
return OK;
}
}
int DeQueue(SqQueue *qu,BiTree *e)
{
if(qu->rear==qu->front)
return ERROR;
else
{
*e=qu->s[qu->front];
qu->front++;
return OK;
}
}
int QueueEmpty(SqQueue qu)
{
if(qu.rear==qu.front)
return OK;
else
return ERROR;
}
int initstack(SqStack *s)
{
s->top=0;
return OK;
}
int Push(SqStack *s,BiTree e)
{
if(s->top==MAXSIZE)
return ERROR;
else{
s->data[s->top]=e;
s->top++;
return OK;
}
}
int Pop(SqStack *s, BiTree *e)
{
if(s->top==0)
return ERROR;
else{
*e=s->data[s->top-1];
s->top--;
return OK;
}
}
int Gettop(SqStack st,BiTree *e)
{
if(st.top==0)
return ERROR;
else
{
*e=st.data[st.top-1];
return OK;
}
}
int StackEmpty(SqStack s )
{
if(s.top==0)
return OK;
else
return ERROR;
}
void Greate(BiTree *T)
{
BiTree p,q;
SqStack s;
int flag=1;
char ch;
initstack(&s);
*T=NULL;
scanf("%c",&ch);
while(ch!='#')
{
if(ch>='A'&&ch<='Z')
{
ch='!';
}

switch(ch){
case'!':
p=(BiTree)malloc(sizeof(BiNode *));
p->data=ch;
p->lchild=p->rchild=NULL;
if(*T==NULL)
{*T=p;}
else
{
if(flag==0)
{
q=(BiTree)malloc(sizeof(BiNode *));
Gettop(s,&p);
p->lchild=q;
}

else
{
p->rchild=q;
}
}
break;

case'(':flag=0;Push(&s,p);break;
case',':flag=1;break;
case')':Pop(&s,&p);break;

}
}
}
void deep_traverse(BiTree *T)
{ BiTree p;
SqQueue q;
initQueue(&q);
p=*T;

EnQueue(&q,p);
while (!QueueEmpty(q))
{ DeQueue(&q,&p);
printf("%c ", p->data);
if (p->lchild!=NULL) EnQueue(&q,p->lchild);
if (p->rchild!=NULL) EnQueue(&q,p->rchild);
}
}
/*A(B(E(K,L),F),C(D(,H)))#*/
main()
{
BiTree t;
clrscr();
Greate(&t);
getch();
deep_traverse(&t);
getch();
}

loving_darling 2009-05-30
  • 打赏
  • 举报
回复
假设二叉树采用二叉链表结构。设计并实现如下算法:输入某棵二叉树的广义表形式,建立该二叉树,并按层次遍历该二叉树。

你这个不符合要求呀

我做实验的时候要满足要求的


谢谢咯

(*^__^*) 嘻嘻……
goregrypeck 2009-05-29
  • 打赏
  • 举报
回复
一楼比较详细,
捧剑者 2009-05-28
  • 打赏
  • 举报
回复
kao
hua_zhixing_ 2009-05-28
  • 打赏
  • 举报
回复 1
这个没广义表形式,其它都有。参考参考吧。
#include<iostream>
#include<string>
using namespace std;
#define MAXLEN 100
typedef struct bt //定义二叉树结构体
{
char data;
bt* lchild;
bt* rchild;
}bt;

bt *createBt(void);
void showTree(bt *t);
void preorder(bt *t);
void postorder(bt *t);
void levelorder(bt *t);
void inorder(bt *t);
int leafnum(bt *t);
int nodenum(bt *t);
int treeDepth(bt *t);
void printTree(bt *t);
int count;//定义计算结点个数的变量

int main() //树子系统主函数
{
bt *t=NULL;
char ch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='Y')
{
printf("\n\n\n\n");
printf("\t\t\t 二叉树子系统\n");
printf("\n\t\t************************************");
printf("\n\t\t* 1-------建 二 叉 树 *");
printf("\n\t\t* 2-------凹 入 显 示 *");
printf("\n\t\t* 3-------先 序 遍 历 *");
printf("\n\t\t* 4-------中 序 遍 历 *");
printf("\n\t\t* 5-------后 序 遍 历 *");
printf("\n\t\t* 6-------层 次 遍 历 *");
printf("\n\t\t* 7-------求 叶 子 数 *");
printf("\n\t\t* 8-------求 结 点 数 *");
printf("\n\t\t* 9-------求 树 深 度 *");
printf("\n\t\t* x-------打印二叉树 *");
printf("\n\t\t* 0-------返 回 *");
printf("\n\t\t************************************");
printf("\n\n\t\t请选择菜单号(0--9):");
cin>>ch2;
switch(ch2)
{
case '1':
printf("\n\t\t请输入按先序建立二叉树的结点序列:");
printf("\n\t\t说明:‘00’代表后继结点为空,逐个输入,按回车输入下一结点。");
printf("\n\t\t请输入根结点:");
t=createBt();
printf("\n\t\t二叉树成功建立!\n");break;
case '2':showTree(t);break;
case '3':
printf("\n\t\t该二叉树的先序遍历序列为:");
preorder(t);break;
case '4':
printf("\n\t\t该二叉树的中序遍历序列为:");
inorder(t);break;
case '5':
printf("\n\t\t该二叉树的后序遍历序列为:");
postorder(t);break;
case '6':
printf("\n\t\t该二叉树的层次遍历序列为:");
levelorder(t);break;
case '7':
count=0;leafnum(t);
printf("\n\t\t该二叉树有%d个叶子。\n",count);break;
case '8':
count=0;nodenum(t);
printf("\n\t\t该二叉树总共有%d个结点。\n",count);break;
case '9':
printf("\n\t\t该树的深度是:%d",treeDepth(t));break;
case 'x':cout<<"\n\t\t该二叉树为:";printTree(t);break;
case '0':
ch1='n';break;
default:
printf("\n\t\t***请注意:输入有误!***");
}
if(ch2!='0')
{
printf("\n\n\t\t按回车键继续,按任意键返回主菜单!\n");
a=getchar();
if(a!='\xA')//??????????????
{ getchar(); ch1='n'; }
}
}
return 1;
}

bt *createBt(void) //建立二叉树
{
bt *t=NULL;
char x;
cin>>x;
getchar();//接受回车符
if(x!='#')
{
t=new bt;
t->data=x;
printf("\n\t\t请输入%c结点的左子结点:",t->data);
t->lchild=createBt();
printf("\n\t\t请输入%c结点的右子结点:",t->data);
t->rchild=createBt();
}
else t=NULL;
return t;
}

void preorder(bt *t) //先序遍历
{
if(t)
{
cout<<t->data;
preorder(t->lchild);
preorder(t->rchild);
}
}

void inorder(bt *t) //中序遍历
{
if(t)
{
inorder(t->lchild);
cout<<t->data;
inorder(t->rchild);
}
}

void postorder(bt *t) //后序遍历
{
if(t)
{
postorder(t->lchild);
postorder(t->rchild);
cout<<t->data;
}
}

void levelorder(bt *t) //层次遍历????????????????????
{
int i,j;
bt *q[100],*p;
p=t;
if(p!=NULL)
{ i=1; q[i]=p; j=2; }
while(i!=j)
{
p=q[i];
cout<<p->data;
if(p->lchild!=NULL)
{ q[j]=p->lchild; j++;}
if(p->rchild!=NULL)
{ q[j]=p->rchild; j++; }
i++;
}
}

int leafnum(bt *t) //求叶结点数
{

if(t)
{
if(t->lchild==NULL && t->rchild==NULL)
count++;
leafnum(t->lchild);
leafnum(t->rchild);
}
return count;
}

int nodenum(bt *t) //求二叉树总结点数
{

if(t)
{
count++;
nodenum(t->lchild);
nodenum(t->rchild);
}
return count;
}

int treeDepth(bt *t) //求树深度
{
int ldep,rdep;
if(t==NULL) return 0;
else
{
ldep=treeDepth(t->lchild);
rdep=treeDepth(t->rchild);
if(ldep>rdep) return ldep+1;
else return rdep+1;
}
}

void showTree(bt *t) //凹入法显示二叉树?????
{
bt *stack[MAXLEN],*p;
int level[MAXLEN][2],top,n,i,width=4;
if(t!=NULL)
{
printf("\n\t\t二叉树的凹入表示法:\n\t\t");
top=1;
stack[top]=t; //根结点入栈
level[top][0]=width;
while(top>0)
{
p=stack[top];//退栈并显示结点的从值
n=level[top][0];
for(i=1;i<=n;i++) //n为显示宽度,字符以右对齐显示
printf(" ");
cout<<p->data;
for(i=n+1;i<50;i+=2)
printf("▃");
printf("\n\t\t");
top--;
if(p->rchild!=NULL) //左子树根结点入栈
{
top++;
stack[top]=p->rchild;
level[top][0]=n+width; //显示宽度增加width
level[top][1]=2; //左子树根结点入栈
}
if(p->lchild!=NULL) //右子树根结点入栈????????????
{
top++;
stack[top]=p->rchild;
level[top][0]=n+width; //显示宽度增加width
level[top][1]=2; //左子树根结点入栈
}
}
}
}



void printTree(bt *t){
if(t){
cout<<t->data;
if(t->lchild||t->rchild)
cout<<"(";
printTree(t->lchild);
if(t->lchild && t->rchild)
cout<<",";
printTree(t->rchild);
if(t->lchild || t->rchild)
cout<<")";
}
}

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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