BTreeNode *BLinkTree::Create(int first,char x)
{
BTreeNode *current;
if(n>=NodeNum && n>=0)
{
current=NULL;
n=n/2;
return current;
}
else
{
current=new BTreeNode;
current->data=x;
m--;
if(m<=0) return current;
if(first)
{
head=current;
first=0;
}
current->lchild=Create(0,i[(n=2*n+1)]);
current->rchild=Create(0,i[(n=2*n-2)]);
return current;
}
}
就帮忙把上面的代码修改一下!
VC2003平台成功编译运行,
#include <stdio.h>
struct BTreeNode
{
char data;
BTreeNode *lchild, *rchild;
};
BTreeNode *create(char* str, int pose, int size)
{
char ch;
BTreeNode * t;
char* p=str;
ch = p[pose];
if(ch==' '|| ch=='\n'|| pose>=size)
return NULL; // 表示空结点
else
{
t=(BTreeNode *)malloc(sizeof(BTreeNode)); //非空则构造新结点
t->data=ch; //新结点数据域即为读入字符
t->lchild=create(p, 2*pose+1,size); //建立左子树
t->rchild=create(p, 2*pose+2,size); //建立右子树
}
return(t);
}
void preorder(BTreeNode *root)
{
BTreeNode *t=root;
if(NULL!=t)
{
printf("%c", t->data); //访问根结点
preorder(t->lchild); //先序遍历左子树
preorder(t->rchild); //先序遍历右子树
}
}
//主函数
void main()
{
BTreeNode *root;
char a[20];
printf("给出建立二叉树的字符串:比如abcde\n");
scanf("%s", a);
root=create(a, 0,strlen(a));
printf("\n先序遍历序列: ");
preorder(root);
printf("\n");
}
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息