这个程序有的能运行出来,但是到Assign 和seach的地方结果没出来,请大家帮我看看啦。可以的话帮我修改下啊。
#include <stdio.h>#include <stdlib.h>
#include <string>
#include <windows.h>
using namespace std;
#define MaxLength 7
#define OK 1
#define OVERFLOW -2
void clrscr(void);
void clrscr(void);
void gotoxy(int ,int );
typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
int Win_x;//坐标
void PostOrder(BiTree b)
{
if(b!=NULL){
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c",b->data);
}
}
void LevelOrderTraverse(BiTree b){//层序遍历
BiTree Q[MaxLength];
int front=0,rear=0;
BiTree p;
if(b){ //根结点入队
Q[rear]=b;
rear=(rear+1)%MaxLength;
}
while(front!=rear){
p=Q[front]; //队头元素出队
front=(front+1)%MaxLength;
printf("%c",p->data);
printf("\n");
if(p->lchild){ //左孩子不为空,入队
Q[rear]=p->lchild;
rear=(rear+1)%MaxLength;
}
if(p->rchild){ //右孩子不为空,入队
Q[rear]=p->rchild;
rear=(rear+1)%MaxLength;
}
}
}
BiTree CreateBiTree(char pre[],char in[],int n)
{
BiTNode *s;
char *p;
int k;
if(n<=0)return NULL;
s=(BiTree)malloc(sizeof(BiTNode));
s->data=*pre;
for(p=in;p<in+n;p++)
if(*p==*pre)break;
k=p-in;
s->lchild=CreateBiTree(pre+1,in,k);
s->rchild=CreateBiTree(pre+k+1,p+1,n-k-1);
return s;
}//
void clrscr(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
COORD Home = {0,0};
DWORD dummy;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
FillConsoleOutputCharacter(hConsoleOut,' ',csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy);
csbiInfo.dwCursorPosition.X = 0;
csbiInfo.dwCursorPosition.Y = 0;
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
void gotoxy(int x,int y)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
void inorder(BiTree b,int y) //以二叉树形状显示
{
BiTree p=b;
if(p!=NULL)
{
inorder(p->lchild,y+2);
gotoxy(Win_x-1,y);
printf("%c",p->data);
Win_x=Win_x+1;
inorder(p->rchild,y+2);
}
else
{
gotoxy(Win_x,y);
printf(" . ");
Win_x=Win_x+2;
}
}
//初始条件:二叉树T存在,e 是T 中某个结点
//操作结果:结点e赋值为x
int Assign(BiTree b,BiTNode* e,char x)
{
if(!b) return 0;
e->data=x;
return 1;
}
bool search( BiTree b, BiTNode* e, char g)
{
if (b == NULL)
return false;
e->data = g;
if (search(b,e->lchild, g) || search(b,e->rchild, g))
return 1;
}
int main()
{
char x;
BiTNode * e;
BiTree b;int n;char s1[100],s2[100],g;
scanf("%s",&s1);
scanf("%s",&s2);
scanf("%c",&x);
scanf("%c",&g);
n=strlen(s1);
b=CreateBiTree(s1,s2,n);
PostOrder(b);
LevelOrderTraverse( b);
// clrscr();
int y=1;//坐标
Win_x=2;
inorder(b,y);
search(b,e,g);
Assign(b,e,x);
return 0;
}