帮忙看看这个错误是什么原因造成的

tw4452852 2009-04-05 05:21:48
#include <iostream>

template <class T>
struct BTNode
{
T element;
BTNode<T>* lchild,*rchild;
BTNode()
{
lchild=rchild=NULL;
}
BTNode(T& x)
{
element=x;
lchild=rchild=NULL;
}
BTNode(T& x,BTNode<T>* l,BTNode<T>* r)
{
element=x;
lchild=l;
rchild=r;
}
};
template <class T> class BinaryTree;
template <class T>
class queue
{
public:
int front,rear,size;
BTNode<T>** q;
queue(int msize)
{
size=msize;
q=new BTNode<T>*[size];
front=rear=0;
}
BTNode<T>* dequeue()
{
int output=(front+1)%size;
front=output;
BinaryTree<T>* order[100];
return q[output];

}
void enqueue(BTNode<T>* x)
{
q[(rear=(rear+1)%size)]=x;
}
bool isempty()
{
return rear==front;
}
friend class BinaryTree<T>;

};



template <class T>
class BinaryTree
{
public:
BTNode<T>* root;
friend class BTNode<T>;
BinaryTree()
{
root=NULL;
}

void Clear(BTNode<T>* x)
{
if(x!=NULL)
{
Clear(x->lchild);
Clear(x->rchild);
delete x;
x=NULL;
}
}
void MakeTree(T x,BinaryTree<T>& left,BinaryTree<T>& right)
{
if(root||&left==&right)
{
return;
}
root=new BTNode<T>(x,left.root,right.root);
left.root=right.root=NULL;
}
int Height(BTNode<T>* x)
{
int hl,hr;
if(x==NULL)
{
return 0;
}
hl=Height(x->lchild);
hr=Height(x->rchild);
return 1+(hr>hl?hr:hl);
}
int Leaves(BTNode<T>* x)
{
int ll,rl;
if(x==NULL)
{
return 0;
}
else
{
if(x->lchild==NULL&&x->rchild==NULL)
{
return 1;
}
else
{
ll=Leaves(x->lchild);
rl=Leaves(x->rchild);
return ll+rl;
}
}
}
BTNode<T>* CopyTree(BTNode<T>* x)
{
BTNode<T> *tmp;
if(tmp==NULL)
{
return NULL;
}
else
{
tmp=new BTNode<T>();
tmp->element=x->element;
tmp->lchild=CopyTree(x->lchild);
tmp->rchild=CopyTree(x->rchild);
return tmp;
}
}
void Change(BTNode<T>* x)
{
BTNode<T>* tmp=NULL;
if(x!=NULL)
{
tmp=x->lchild;
x->lchild=x->rchild;
x->rchild=tmp;
Change(x->lchild);
Change(x->rchild);
}
}
void LevelOrder(BTNode<T>* x)
{
queue<T> order(100);
BTNode<T>* tmp=x;
BTNode<T>* out;
order.enqueue(tmp);
std::cout<<x->element;
if(tmp->lchild)
{
order.enqueue(tmp->lchild);
}
if(tmp->rchild)
{
order.enqueue(tmp->rchild);
}
while(!(order.isempty()))
{
out=order.dequeue();
tmp=(order.q)[order.front+1];
if(tmp->lchild)
{
order.enqueue(tmp->lchild);
}
if(tmp->rchild)
{
order.enqueue(tmp->rchild);
}
std::cout<<out->element<<',';
}
}
};

int main()
{
BinaryTree<char> a,b,x,y,z,c;
int leave,height;
y.MakeTree('E',a,b);
z.MakeTree('F',a,b);
x.MakeTree('C',y,z);
y.MakeTree('D',a,b);
z.MakeTree('B',y,x);
leave=z.Leaves(z.root);
height=z.Height(z.root);
std::cout<<"leave:"<<leave<<'\n';
std::cout<<"height:"<<height<<'\n';
z.LevelOrder(z.root);
z.Change(z.root);
z.LevelOrder(z.root);
c.root=z.CopyTree(z.root);
c.LevelOrder(c.root);
return 0;
}



我在linux下用g++编译通过,运行时显示:
leave:3
height:3
Segmentation fault

我想可能是指针出了问题,但是看了半天都没看出来,拜托各位帮忙看看,谢谢了~
...全文
123 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tw4452852 2009-04-05
  • 打赏
  • 举报
回复
非常感谢楼上的这位朋友,的确是tmp出范围了,经过修改终于完成了,太感谢了大家了,谢谢!!
xfinal2006 2009-04-05
  • 打赏
  • 举报
回复
理解错LZ的意思了

LevelOrder中
while(!(order.isempty()))
{
out=order.dequeue();
tmp=(order.q)[order.front+1];
if(tmp->lchild)
....
}

tmp会出范围的,具体原因没仔细看了, 太长了...
tw4452852 2009-04-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xfinal2006 的回复:]
要为T类型重载cout
[/Quote]
而且我又做了小程序:
#include <iostream>

template <class T>
class wow
{
public:
T a;
wow(T c)
{
a=c;
}
void output()
{
std::cout<<a;
}
};
int main()
{
wow<char> b('E');
b.output();
return 0;
}
却可以正常输出。
tw4452852 2009-04-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xfinal2006 的回复:]
要为T类型重载cout
[/Quote]
我为BTNode<T>添加了如下成员函数:
void visit(std::ostream& out)
{
out<<element;
}

但是在void LevelOrder(BTNode<T>* x)函数中进行如下调用:
(x->visit)(std::cout);
还是没有显示阿??依旧是Segmentation fault错误...
rongkun06 2009-04-05
  • 打赏
  • 举报
回复
路过
mengde007 2009-04-05
  • 打赏
  • 举报
回复
并非没人知道,只不过入不敷出……
xfinal2006 2009-04-05
  • 打赏
  • 举报
回复
要为T类型重载cout
tw4452852 2009-04-05
  • 打赏
  • 举报
回复
没人知道??
liliangbao 2009-04-05
  • 打赏
  • 举报
回复
帮顶~
tw4452852 2009-04-05
  • 打赏
  • 举报
回复
那我把问题简化以下:
请问:
void LevelOrder(BTNode <T>* x)
{
queue <T> order(100);
BTNode <T>* tmp=x;
BTNode <T>* out;
order.enqueue(tmp);
std::cout < <x->element;
if(tmp->lchild)
{
order.enqueue(tmp->lchild);
}
if(tmp->rchild)
{
order.enqueue(tmp->rchild);
}
while(!(order.isempty()))
{
out=order.dequeue();
tmp=(order.q)[order.front+1];
if(tmp->lchild)
{
order.enqueue(tmp->lchild);
}
if(tmp->rchild)
{
order.enqueue(tmp->rchild);
}
std::cout < <out->element < <',';
}
}
};
为什么我用z.LevelOrder(z.root)调用的时候,std::cout < <x->element这句为什么没在终端显示?
mengde007 2009-04-05
  • 打赏
  • 举报
回复
太长了,要是100分,我可以帮你调试一下。
lw1a2 2009-04-05
  • 打赏
  • 举报
回复
先学会调试

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧