二叉树输出按广义表的形式!!!
#include<stdio.h>
#include<malloc.h>
typedef struct Node{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
void CreatBiTree(BiTree *bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(*bt)->data=ch;
CreatBiTree(&((*bt)->LChild));
CreatBiTree(&((*bt)->RChild));
}
}
void PreOrder(BiTree root)
{
if(root!=NULL)
{
printf("(");
printf("%c",root->data);
PreOrder(root->LChild);
if(root->RChild)
printf(",");
PreOrder(root->RChild);
printf(")");
}
}
void main()
{
BiTree LA;
LA=(BiTree)malloc(sizeof(BiTNode));
CreatBiTree(&LA);
PreOrder(LA);
printf("\n");
}
如输入AB..C..输出为(A(B,C))形式 谢谢了!!!!!!!
[解决办法]
#include<stdio.h>#include<malloc.h>typedef struct Node{ char data; struct Node *LChild; struct Node *RChild;}BiTNode,*BiTree;void PreOrderR(BiTree root);void CreatBiTree(BiTree *bt){ char ch; ch=getchar(); if(ch=='.') *bt=NULL; else { *bt=(BiTree)malloc(sizeof(BiTNode)); (*bt)->data=ch; CreatBiTree(&((*bt)->LChild)); CreatBiTree(&((*bt)->RChild)); }}void PreOrderL(BiTree root){ if(root!=NULL) { printf("("); printf("%c",root->data); PreOrderL(root->LChild); if(root->RChild) { printf(","); PreOrderR(root->RChild); printf(")"); } }}void PreOrderR(BiTree root){ if(root!=NULL) { printf("%c",root->data); PreOrderL(root->LChild); if(root->RChild) { printf(","); PreOrderR(root->RChild); } printf(")"); }}int main(){ BiTree LA; LA=(BiTree)malloc(sizeof(BiTNode)); CreatBiTree(&LA); PreOrderL(LA); printf("\n"); system("pause"); return 0;}