树与二叉树的转换的实现。以及树的前序、后序的递归、非递归遍历算法,层次序的非递归遍历算法的实现,应包含建树的实现。
要求:遍历的内容应是千姿百态的。
请高手帮忙 谢谢!急!
7 个解决方案
#1
作业贴?
#2
[code=C/C++]#include"conio.h"
#include"stdio.h"
#include"alloc.h"
#include"string.h"
#include"graphics.h"
#define M 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode,*btree;
btree tree_creat(char str[])
{
struct node *t=NULL,*p,*stack[M];
char ch;
int i=0,flag,top=-1;
while(1)
{
ch=str[i++];
switch(ch)
{
case '#': return(t);
case '(': stack[++top]=p;
flag=1;
break;
case ')': top--;
break;
case ',': flag=2;
break;
default : p=(btree)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=NULL;
p->rchild=NULL;
if(t==NULL)
t=p;
else if(flag==1)
stack[top]->lchild=p;
else
stack[top]->rchild=p;
}
}
}
out_put(btree list)
{
int i=4,r,x1,x2,y1,y2,gd=DETECT,gm=0;
char c[8]="ABCDEFGH";
initgraph(&gd,&gm,"c:\\tc");
setbkcolor(0);
setcolor(12);
r=10;
x1=450;
y1=200;
while(i)
{
circle(x1,y1,r);
circle(x2,y2,r);
/*x=x-3;y=y-3;
gotoxy(25,13);
printf("%c",c[i++]);*/
outtextxy(x1-3,y1-3,"A");
outtextxy(x1+9,y1+4,"");
lineto(x1+26+6,y1+26-6);
x2=x1+26+9;
y2=y1+26+5;
outtextxy(x2-3,y2-3,"B");
outtextxy(x1-9,y1+5,"");
lineto(x1-26-6,y1+26-6);
x1=x1-26-9;
y1=y1+26+5;
circle(x1,y1,r);
circle(x2,y2,r);
outtextxy(x1-3,y1-3,"A");
i--;
}
}
preorder(btree list)
{
if(list!=NULL)
{
printf("%-2c",list->data);
preorder(list->lchild);
preorder(list->rchild);
}
}
inorder(btree list)
{
if(list!=NULL)
{
inorder(list->lchild);
printf("%-2c",list->data);
inorder(list->rchild);
}
}
postorder(btree list)
{
if(list!=NULL)
{
postorder(list->lchild);
postorder(list->rchild);
printf("%-2c",list->data);
}
}
main()
{
char str[M];
btree list;
clrscr();
out_put(list);
gotoxy(3,10);
printf("\n 二叉树遍历\n");
printf("\n 请输入树的元素(以括号的形式输入):");
gets(str);
list=tree_creat(str);
gotoxy(3,15);
printf("\前序遍历 :");
preorder(list);
printf("\n");
gotoxy(3,17);
printf("中序遍历 :");
inorder(list);
printf("\n");
gotoxy(3,19);
printf("后序遍历 :");
postorder(list);
getch();
}code]
#include"stdio.h"
#include"alloc.h"
#include"string.h"
#include"graphics.h"
#define M 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode,*btree;
btree tree_creat(char str[])
{
struct node *t=NULL,*p,*stack[M];
char ch;
int i=0,flag,top=-1;
while(1)
{
ch=str[i++];
switch(ch)
{
case '#': return(t);
case '(': stack[++top]=p;
flag=1;
break;
case ')': top--;
break;
case ',': flag=2;
break;
default : p=(btree)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=NULL;
p->rchild=NULL;
if(t==NULL)
t=p;
else if(flag==1)
stack[top]->lchild=p;
else
stack[top]->rchild=p;
}
}
}
out_put(btree list)
{
int i=4,r,x1,x2,y1,y2,gd=DETECT,gm=0;
char c[8]="ABCDEFGH";
initgraph(&gd,&gm,"c:\\tc");
setbkcolor(0);
setcolor(12);
r=10;
x1=450;
y1=200;
while(i)
{
circle(x1,y1,r);
circle(x2,y2,r);
/*x=x-3;y=y-3;
gotoxy(25,13);
printf("%c",c[i++]);*/
outtextxy(x1-3,y1-3,"A");
outtextxy(x1+9,y1+4,"");
lineto(x1+26+6,y1+26-6);
x2=x1+26+9;
y2=y1+26+5;
outtextxy(x2-3,y2-3,"B");
outtextxy(x1-9,y1+5,"");
lineto(x1-26-6,y1+26-6);
x1=x1-26-9;
y1=y1+26+5;
circle(x1,y1,r);
circle(x2,y2,r);
outtextxy(x1-3,y1-3,"A");
i--;
}
}
preorder(btree list)
{
if(list!=NULL)
{
printf("%-2c",list->data);
preorder(list->lchild);
preorder(list->rchild);
}
}
inorder(btree list)
{
if(list!=NULL)
{
inorder(list->lchild);
printf("%-2c",list->data);
inorder(list->rchild);
}
}
postorder(btree list)
{
if(list!=NULL)
{
postorder(list->lchild);
postorder(list->rchild);
printf("%-2c",list->data);
}
}
main()
{
char str[M];
btree list;
clrscr();
out_put(list);
gotoxy(3,10);
printf("\n 二叉树遍历\n");
printf("\n 请输入树的元素(以括号的形式输入):");
gets(str);
list=tree_creat(str);
gotoxy(3,15);
printf("\前序遍历 :");
preorder(list);
printf("\n");
gotoxy(3,17);
printf("中序遍历 :");
inorder(list);
printf("\n");
gotoxy(3,19);
printf("后序遍历 :");
postorder(list);
getch();
}code]
#3
自己多动手写哟,多模仿。这种问题在百度上一般都能搜索到的。
#4
http://blog.csdn.net/lazy_p/archive/2010/01/31/5275260.aspx
这里是我以前写的根据前序和中序,求后序。根据后序和中序求前序的代码,希望对你有所帮助!
这里是我以前写的根据前序和中序,求后序。根据后序和中序求前序的代码,希望对你有所帮助!
#5
LZ最好还是自己实现吧
不然自己能力是得不到提高的
不然自己能力是得不到提高的
#6
不会做作业。
#7
多好的作业呀,就是不肯做
#1
作业贴?
#2
[code=C/C++]#include"conio.h"
#include"stdio.h"
#include"alloc.h"
#include"string.h"
#include"graphics.h"
#define M 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode,*btree;
btree tree_creat(char str[])
{
struct node *t=NULL,*p,*stack[M];
char ch;
int i=0,flag,top=-1;
while(1)
{
ch=str[i++];
switch(ch)
{
case '#': return(t);
case '(': stack[++top]=p;
flag=1;
break;
case ')': top--;
break;
case ',': flag=2;
break;
default : p=(btree)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=NULL;
p->rchild=NULL;
if(t==NULL)
t=p;
else if(flag==1)
stack[top]->lchild=p;
else
stack[top]->rchild=p;
}
}
}
out_put(btree list)
{
int i=4,r,x1,x2,y1,y2,gd=DETECT,gm=0;
char c[8]="ABCDEFGH";
initgraph(&gd,&gm,"c:\\tc");
setbkcolor(0);
setcolor(12);
r=10;
x1=450;
y1=200;
while(i)
{
circle(x1,y1,r);
circle(x2,y2,r);
/*x=x-3;y=y-3;
gotoxy(25,13);
printf("%c",c[i++]);*/
outtextxy(x1-3,y1-3,"A");
outtextxy(x1+9,y1+4,"");
lineto(x1+26+6,y1+26-6);
x2=x1+26+9;
y2=y1+26+5;
outtextxy(x2-3,y2-3,"B");
outtextxy(x1-9,y1+5,"");
lineto(x1-26-6,y1+26-6);
x1=x1-26-9;
y1=y1+26+5;
circle(x1,y1,r);
circle(x2,y2,r);
outtextxy(x1-3,y1-3,"A");
i--;
}
}
preorder(btree list)
{
if(list!=NULL)
{
printf("%-2c",list->data);
preorder(list->lchild);
preorder(list->rchild);
}
}
inorder(btree list)
{
if(list!=NULL)
{
inorder(list->lchild);
printf("%-2c",list->data);
inorder(list->rchild);
}
}
postorder(btree list)
{
if(list!=NULL)
{
postorder(list->lchild);
postorder(list->rchild);
printf("%-2c",list->data);
}
}
main()
{
char str[M];
btree list;
clrscr();
out_put(list);
gotoxy(3,10);
printf("\n 二叉树遍历\n");
printf("\n 请输入树的元素(以括号的形式输入):");
gets(str);
list=tree_creat(str);
gotoxy(3,15);
printf("\前序遍历 :");
preorder(list);
printf("\n");
gotoxy(3,17);
printf("中序遍历 :");
inorder(list);
printf("\n");
gotoxy(3,19);
printf("后序遍历 :");
postorder(list);
getch();
}code]
#include"stdio.h"
#include"alloc.h"
#include"string.h"
#include"graphics.h"
#define M 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode,*btree;
btree tree_creat(char str[])
{
struct node *t=NULL,*p,*stack[M];
char ch;
int i=0,flag,top=-1;
while(1)
{
ch=str[i++];
switch(ch)
{
case '#': return(t);
case '(': stack[++top]=p;
flag=1;
break;
case ')': top--;
break;
case ',': flag=2;
break;
default : p=(btree)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=NULL;
p->rchild=NULL;
if(t==NULL)
t=p;
else if(flag==1)
stack[top]->lchild=p;
else
stack[top]->rchild=p;
}
}
}
out_put(btree list)
{
int i=4,r,x1,x2,y1,y2,gd=DETECT,gm=0;
char c[8]="ABCDEFGH";
initgraph(&gd,&gm,"c:\\tc");
setbkcolor(0);
setcolor(12);
r=10;
x1=450;
y1=200;
while(i)
{
circle(x1,y1,r);
circle(x2,y2,r);
/*x=x-3;y=y-3;
gotoxy(25,13);
printf("%c",c[i++]);*/
outtextxy(x1-3,y1-3,"A");
outtextxy(x1+9,y1+4,"");
lineto(x1+26+6,y1+26-6);
x2=x1+26+9;
y2=y1+26+5;
outtextxy(x2-3,y2-3,"B");
outtextxy(x1-9,y1+5,"");
lineto(x1-26-6,y1+26-6);
x1=x1-26-9;
y1=y1+26+5;
circle(x1,y1,r);
circle(x2,y2,r);
outtextxy(x1-3,y1-3,"A");
i--;
}
}
preorder(btree list)
{
if(list!=NULL)
{
printf("%-2c",list->data);
preorder(list->lchild);
preorder(list->rchild);
}
}
inorder(btree list)
{
if(list!=NULL)
{
inorder(list->lchild);
printf("%-2c",list->data);
inorder(list->rchild);
}
}
postorder(btree list)
{
if(list!=NULL)
{
postorder(list->lchild);
postorder(list->rchild);
printf("%-2c",list->data);
}
}
main()
{
char str[M];
btree list;
clrscr();
out_put(list);
gotoxy(3,10);
printf("\n 二叉树遍历\n");
printf("\n 请输入树的元素(以括号的形式输入):");
gets(str);
list=tree_creat(str);
gotoxy(3,15);
printf("\前序遍历 :");
preorder(list);
printf("\n");
gotoxy(3,17);
printf("中序遍历 :");
inorder(list);
printf("\n");
gotoxy(3,19);
printf("后序遍历 :");
postorder(list);
getch();
}code]
#3
自己多动手写哟,多模仿。这种问题在百度上一般都能搜索到的。
#4
http://blog.csdn.net/lazy_p/archive/2010/01/31/5275260.aspx
这里是我以前写的根据前序和中序,求后序。根据后序和中序求前序的代码,希望对你有所帮助!
这里是我以前写的根据前序和中序,求后序。根据后序和中序求前序的代码,希望对你有所帮助!
#5
LZ最好还是自己实现吧
不然自己能力是得不到提高的
不然自己能力是得不到提高的
#6
不会做作业。
#7
多好的作业呀,就是不肯做