凹入表示法 二叉树
来源:百度知道 编辑:UC知道 时间:2024/05/21 04:34:22
我已经写好了用先序和中序建立一个二叉树,但怎么把它用凹入法输出来呢?
求解!!!
贴一段我写的代码:
#include <iostream>
using namespace std;
struct BiNode
{
char data;
BiNode *lchild, *rchild;
};
typedef BiNode *BiTree;
int CreateBiTree(BiTree &T, const char *s1, const char *s2, int len)
{
if (len<=0)
{
T = NULL;
return 1;
}
else
{
T = new BiNode;
T->data = *s1;
int i;
for ( i=0; i<len; i++) if (s2[i]==*s1) break;
CreateBiTree(T->lchild, s1+1, s2, i);
CreateBiTree(T->rchild, s1+i+1, s2+i+1, len-(i+1));
}
return 1;
}
求解!!!
贴一段我写的代码:
#include <iostream>
using namespace std;
struct BiNode
{
char data;
BiNode *lchild, *rchild;
};
typedef BiNode *BiTree;
int CreateBiTree(BiTree &T, const char *s1, const char *s2, int len)
{
if (len<=0)
{
T = NULL;
return 1;
}
else
{
T = new BiNode;
T->data = *s1;
int i;
for ( i=0; i<len; i++) if (s2[i]==*s1) break;
CreateBiTree(T->lchild, s1+1, s2, i);
CreateBiTree(T->rchild, s1+i+1, s2+i+1, len-(i+1));
}
return 1;
}
/* 前序凹入表示法输出, 一般通过前导的空格来凹入 #*/
/*pre,sur分别为前导后续字符,一般前导为空格字符,#*/
void printTree(BiTree *t,char pre,char sur,int depth,int level){
/*如果为空树,return;*/
if(t==NULL) return ;
/*输出根*/
int i=0;
while(i++<level) printf("%c",pre); // 凹入
printf("%c",t->data); // 输出
while(i++<depth) printf("%c",sur); // 后续
printf("\n");
/*输出子树*/
printTree(t->lchild,pre,sur,depth,level+1);
printTree(t-