![]() |
#2
xg56992011-09-16 01:15
![]() #include<string> #include<iostream> using std::cout; using std::string; template<typename type> class HfTree { private: static const int ROOT=-1; struct node { type data; int weight; int parent,lchild,rchild; }; int length; node* elem; struct hfCode { type data;// store the character string code;// the code of the data }; hfCode* result; void getCode(); public: HfTree(const type* d,const int* w,int size); ~HfTree(){delete [] elem;delete [] result;} void printCode(); }; template<typename type> HfTree<type>::HfTree(const type *d, const int *w, int size) { length=size*2-1; elem=new node[length]; //init table"elem" for(int i=size-1;i<length;++i) { elem[i].data=d[i-size+1]; elem[i].weight=w[i-size+1]; elem[i].lchild=elem[i].rchild=elem[i].parent=ROOT; } elem[0].parent=ROOT;//It shore the root of the hfTree and has no father for(int i=size-2;i>=0;--i) { int xMIN_SHORT=32767; int yMIN_SHORT=32767; int xIndex=ROOT; int yIndex=ROOT; for(int j=i+1;j<length;++j) { if(elem[j].parent==ROOT)//if this is true,then it has not been visited. { if(elem[j].weight<xMIN_SHORT) { yMIN_SHORT=xMIN_SHORT; yIndex=xIndex; xMIN_SHORT=elem[j].weight; xIndex=j; } else if(elem[j].weight<yMIN_SHORT) { yMIN_SHORT=elem[j].weight; yIndex=j; } } } elem[i].weight=xMIN_SHORT+yMIN_SHORT; elem[i].lchild=xIndex; elem[i].rchild=yIndex; elem[xIndex].parent=elem[yIndex].parent=i; } } template<typename type> void HfTree<type>::getCode() { int size=length/2+1; int father,index; result=new hfCode[size]; for(int i=size-1;i<length;++i) { result[i-size+1].data=elem[i].data; result[i-size+1].code=""; father=elem[i].parent; index=i; while(father!=ROOT) { if(elem[father].lchild==index) result[i-size+1].code='0'+result[i-size+1].code; else result[i-size+1].code='1'+result[i-size+1].code; index=father; father=elem[father].parent; cout<<father; //估计错在"这里",因为把cout移上去是正常输出4,但是如果放到这里就是-842150451,father变成了这个当然要出错啦 } } } template<typename type> void HfTree<type>::printCode() { getCode(); int size=length/2-1; for(int i=0;i<size;++i) cout<<result[i].data<<" :"<<result[i].code<<'\n'; cout<<'\n'; } int main() { char ch[]={"aeistdn"}; int w[]={10,15,12,3,4,13,1}; HfTree<char> tree(ch,w,7); tree.printCode(); return 0; } |
编译没有问题,就是一运行就出现 window正在查找解决方案(在vc2005中提示:访问冲突)
不知问题出在哪儿,请大神帮帮忙
代码如下:

#include<string>
#include<iostream>
using std::cout;
using std::string;
template<typename type>
class HfTree
{
private:
static const int ROOT=-1;
struct node
{
type data;
int weight;
int parent,lchild,rchild;
};
int length;
node* elem;
struct hfCode
{
type data;// store the character
string code;// the code of the data
};
hfCode* result;
void getCode();
public:
HfTree(const type* d,const int* w,int size);
~HfTree(){delete [] elem;delete [] result;}
void printCode();
};
template<typename type>
HfTree<type>::HfTree(const type *d, const int *w, int size)
{
length=size*2-1;
elem=new node[length];
//init table"elem"
for(int i=size-1;i<length;++i)
{
elem[i].data=d[i-size+1];
elem[i].weight=w[i-size+1];
elem[i].lchild=elem[i].rchild=elem[i].parent=ROOT;
}
elem[0].parent=ROOT;//It shore the root of the hfTree and has no father
for(int i=size-2;i>=0;--i)
{
int xMIN_SHORT=32767;
int yMIN_SHORT=32767;
int xIndex=ROOT;
int yIndex=ROOT;
for(int j=i+1;j<length;++j)
{
if(elem[j].parent==ROOT)//if this is true,then it has not been visited.
{
if(elem[j].weight<xMIN_SHORT)
{
yMIN_SHORT=xMIN_SHORT;
yIndex=xIndex;
xMIN_SHORT=elem[j].weight;
xIndex=j;
}
else if(elem[j].weight<yMIN_SHORT)
{
yMIN_SHORT=elem[j].weight;
yIndex=j;
}
}
}
elem[i].weight=xMIN_SHORT+yMIN_SHORT;
elem[i].lchild=xIndex;
elem[i].rchild=yIndex;
elem[xIndex].parent=elem[yIndex].parent=i;
}
}
template<typename type>
void HfTree<type>::getCode()
{
int size=length/2+1;
int father,index;
result=new hfCode[size];
for(int i=size-1;i<length;++i)
{
result[i-size+1].data=elem[i].data;
result[i-size+1].code="";
father=elem[i].parent;
index=i;
while(father!=ROOT)
{
if(elem[father].lchild==index)
result[i-size+1].code='0'+result[i-size+1].code;
else
result[i-size+1].code='1'+result[i-size+1].code;
index=father;
father=elem[father].parent;
}
}
}
template<typename type>
void HfTree<type>::printCode()
{
getCode();
int size=length/2-1;
for(int i=0;i<size;++i)
cout<<result[i].data<<" :"<<result[i].code<<'\n';
cout<<'\n';
}
int main()
{
char ch[]={"aeistdn"};
int w[]={10,15,12,3,4,13,1};
HfTree<char> tree(ch,w,7);
tree.printCode();
return 0;
}