import java.io.*;
class Node{
public Node(){};
public char c=' ';
public Node lchlid=null,rchlid=null;
public Node(char dat){
c=dat;
}
}
public class ex12_1 {
public static void createtree(Node root,Node a[],int m,int n,Node b[],int x,int y){
if(m>n){root=null;}
else{
root=a[m]; / 这里 !!!!!!!! /
int i;int j;
for(i=x,j=m;i<=y;i++,j++){
if(b[i]==root)
break;
}
createtree(root.lchlid,a,m+1,j,b,x,i-1); / 还有这里 !!!!/
createtree(root.rchlid,a,j+1,n,b,i+1,y);
}
}
public static void postorder(Node n){
if(n.lchlid!=null) postorder(n.lchlid);
else if(n.rchlid!=null) postorder(n.rchlid);
else System.out.print(n.c+" ");
}
public static void main(String args[])throws IOException{
System.out.println("输入节点数:");
int t;
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(in.readLine());
Node a[]=new Node[i];Node b[]=new Node[i];
System.out.println("输入先序遍历结果:");
for(t=0;ta[t]=new Node((char)System.in.read());
}
System.out.println("输入中序遍历结果:");
for(t=0;tb[t]=new Node((char)System.in.read());
}
Node root=new Node();
createtree(root,a,0,i-1,b,0,i-1);
postorder(root);
}
}
我最后用后序输出了,但是运行时说
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at ex12_1.createtree(ex12_1.java:18)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.createtree(ex12_1.java:25)
at ex12_1.main(ex12_1.java:51)
错误在上面标出来了,不懂哪里有问题啊,是算法的问题?那是我在c数据结构课上学的啊。。。。。
数据结构,关于用先序和中序建立二叉树的问题,求高手。。好的会追加分
答案:2 悬赏:0 手机版
解决时间 2021-02-12 01:23
- 提问者网友:暗中人
- 2021-02-11 08:14
最佳答案
- 五星知识达人网友:像个废品
- 2021-02-11 09:24
void createtree(node* &root, node *a, int m, int n, node *b, int x, int y)
{
if( m >= n ){
root = NULL;
return;
}
root = new node();
root->c = a[m].c;
root->lchild = NULL;
root->rchild = NULL;
int i = x, j = m;
for( ; i < y ; i ++, j++)
{
if( b[i].c == root->c)
break;
}
createtree(root->lchild, a, m+1, j+1, b, x, i);
createtree(root->rchild, a, j+1, n, b, i+1, y);
};
void postorder(node* &root)
{
if(root->lchild != NULL)
postorder(root->lchild);
if( root->rchild != NULL)
postorder(root->rchild);
cout << root->c;
};
{
if( m >= n ){
root = NULL;
return;
}
root = new node();
root->c = a[m].c;
root->lchild = NULL;
root->rchild = NULL;
int i = x, j = m;
for( ; i < y ; i ++, j++)
{
if( b[i].c == root->c)
break;
}
createtree(root->lchild, a, m+1, j+1, b, x, i);
createtree(root->rchild, a, j+1, n, b, i+1, y);
};
void postorder(node* &root)
{
if(root->lchild != NULL)
postorder(root->lchild);
if( root->rchild != NULL)
postorder(root->rchild);
cout << root->c;
};
全部回答
- 1楼网友:愁杀梦里人
- 2021-02-11 10:51
#define len sizeof(struct tree)
#define null 0
#include
#include
struct tree
{
char data;
struct tree *lchild,*rchild;
};
//创建二叉树
struct tree *creat()
{
char c;
struct tree *t;
c=getchar();
if(c==' ')
t=null;
else
{
t=(struct tree*)malloc(len);
t->data=c;
t->lchild=creat();
t->rchild=creat();
}
return t;
}
//前序遍历
void preprint(struct tree*t)
{
if(t!=null)
{
printf("%c->",t->data);
preprint(t->lchild);
preprint(t->rchild);
}
}
//中序遍历
void inprint(struct tree*t)
{
if(t!=null)
{
inprint(t->lchild);
printf("%c->",t->data);
inprint(t->rchild);
}
}
//后序遍历
void postprint(struct tree*t)
{
if(t!=null)
{
postprint(t->lchild);
postprint(t->rchild);
printf("%c->",t->data);
}
}
main()
{
struct tree *t;
printf("please input tree in order:\n");
t=creat();
printf("the result of preorder traversal is\n");
preprint(t);
printf("^\nthe result of inorder traversal is\n");
inprint(t);
printf("^\nthe result of postorder traversal is\n");
postprint(t);
printf("^\n");
getch();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯