主题:[求助]关于Huffman树的建立程序问题。
我今天写了个利用最小堆建立哈夫漫树。在最小堆每个单独的程序我都编译过没有问题了,但是在放在建立huffman树中就出现了,问题。哪位高手帮我解决一下。非常感谢!
一下是程序:
树结点类实现。
//“HuffmanTreeNode.h"
#if ! defined(_HUFFMANTREENODE_H)
#define _HUFFMANTREENODE_H
template<class Elem>
class HuffmanTreeNode
{
private:
HuffmanTreeNode* left;
HuffmanTreeNode* right;
HuffmanTreeNode* parent;
Elem element;
public:
HuffmanTreeNode(Elem elementval,HuffmanTreeNode* leftval=NULL,
HuffmanTreeNode* rightval=NULL,
HuffmanTreeNode* parentval=NULL)
{
element=elementval;
left=leftval;
right=rightval;
parent=parentval;
}
HuffmanTreeNode(HuffmanTreeNode* leftval=NULL,HuffmanTreeNode* rightval=NULL,
HuffmanTreeNode* parentval=NULL)
{
left=leftval;
right=rightval;
parent=parentval;
}
virtual ~HuffmanTreeNode(){}
void setVal(Elem& e)
{
element=e;
}
Elem& getVal()
{
return element;
}
HuffmanTreeNode* getLeft()
{
return left;
}
void setLeft(HuffmanTreeNode* T)
{
left=T;
}
HuffmanTreeNode* getRight()
{
return right;
}
void setRight(HuffmanTreeNode* T)
{
right=T;
}
HuffmanTreeNode* getParent()
{
return parent;
}
void setParent(HuffmanTreeNode* T)
{
parent=T;
}
};
#endif
最小堆优先队列。
"MinHeap.h"
#if ! defined(_MINHEAP_H)
#define _MINHEAP_H
template<class Elem>
class MinHeap
{
private:
Elem* heapArray;
int CurrentSize;
int MaxSize;
public:
MinHeap(const int n);
virtual~ MinHeap()
{
delete []heapArray;
}
void BuildHeap();
bool isLeaf(int pos) const;
int leftChild(int pos) const;
int rightChild(int pos) const;
//return parent position
int parent(int pos) const;
//删除给定下标的元素。
bool Remove(int pos,Elem& node);
void SiftDown(int left);
void SiftUp(int position);
bool Insert(const Elem& newNode);
Elem& RemoveMin();
bool print() const;
};
template<class Elem>
MinHeap<Elem>::MinHeap(const int n)
{
if(n<=0)
return ;
CurrentSize=0;
MaxSize=n;
heapArray=new Elem[MaxSize];
BuildHeap();
}
template<class Elem>
void MinHeap<Elem>::BuildHeap()
{
for(int i=CurrentSize/2-1;i>=0;i--)
SiftDown(i);
}
template<class Elem>
bool MinHeap<Elem>::isLeaf(int pos) const
{
return (pos>=CurrentSize/2) && (pos<CurrentSize);
}
template<class Elem>
int MinHeap<Elem>::leftChild(int pos) const
{
return 2*pos+1;
}
template<class Elem>
int MinHeap<Elem>::rightChild(int pos) const
{
return 2*pos+2;
}
template<class Elem>
int MinHeap<Elem>::parent(int pos) const
{
return (pos-1)/2;
}
//向下筛选。
template<class Elem>
void MinHeap<Elem>::SiftDown(int left)
{
int i=left; //标记父结点。
int j=2*i+1;
Elem temp=heapArray[i];
while(j<CurrentSize)
{
if((j<CurrentSize-1) && (heapArray[j]>heapArray[i]))
j++; //指向右子结点
if(temp>heapArray[j])
{
heapArray[i]=heapArray[j];
i=j;
j=2*j+1;
}
else
break;
}
heapArray[i]=temp;
}
//向上筛选
template<class Elem>
void MinHeap<Elem>::SiftUp(int position)
{
int temppos=position;
Elem temp=heapArray[temppos];
while((temppos>0) && (heapArray[parent(temppos)]>temp))
{
heapArray[temppos]=heapArray[parent(temppos)];
temppos=parent(temppos);
}
heapArray[temppos]=temp;
}
template<class Elem>
bool MinHeap<Elem>::Insert(const Elem& newNode)
{
if(CurrentSize==MaxSize)
return false;
heapArray[CurrentSize]=newNode;
SiftUp(CurrentSize);
CurrentSize++;
return true;
}
template<class Elem>
Elem& MinHeap<Elem>::RemoveMin()
{
if(CurrentSize==0)
{
cout<<"Can't Delete ";
}
else
{
Elem temp=heapArray[0];
heapArray[0]=heapArray[CurrentSize-1];
CurrentSize--;
if(CurrentSize>1)
SiftDown(0);
return temp;
}
}
template<class Elem>
bool MinHeap<Elem>::Remove(int pos,Elem& node)
{
if((pos<0) || (pos>CurrentSize))
return false;
Elem temp=heapArray[pos];
heapArray[pos]=heapArray[--CurrentSize];
SiftUp(pos);
SiftDown(pos);
node=temp;
return true;
}
template<class Elem>
bool MinHeap<Elem>::print() const
{
if(CurrentSize<0)
return false;
for(int i=0;i<CurrentSize;i++)
cout<<heapArray[i]<<" ";
return true;
}
#endif
Huffman树类实现。
"HuffmanTree.H"
#if !defined(_HUFFMANTREE_H)
#define _HUFFMANTREE_H
template<class Elem>
class HuffmanTree
{
private:
HuffmanTreeNode<Elem>* root;
void MergeTree(HuffmanTreeNode<Elem> & ht1,
HuffmanTreeNode<Elem> &ht2,
HuffmanTreeNode<Elem>* parent);
public:
HuffmanTree(Elem Weiht[],int n);
void DeleteTree(HuffmanTreeNode<Elem>* root);
~HuffmanTree();
void print(HuffmanTreeNode<Elem>* root) const;
HuffmanTreeNode<Elem>* getRoot()
{
return root;
}
};
template<class Elem>
void HuffmanTree<Elem>::MergeTree(HuffmanTreeNode<Elem> &ht1,
HuffmanTreeNode<Elem> &ht2,
HuffmanTreeNode<Elem> *parent)
{
Elem it;
parent->setLeft(&ht1);
parent->setRight(&ht2);
it=ht1.getVal()+ht2.getVal();
parent->setVal(it);
parent->setParent(NULL);
ht1.setParent(parent);
ht2.setParent(parent);
}
template<class Elem>
HuffmanTree<Elem>::HuffmanTree(Elem Weight[],int n)
{
MinHeap<HuffmanTreeNode<Elem> > heap(n);
HuffmanTreeNode<Elem> *parent,firstChild,secondChild;
HuffmanTreeNode<Elem>* NodeList=new HuffmanTreeNode<Elem>[n];
for(int i=0;i<n;i++)
{
NodeList[i].setVal(Weight[i]);
NodeList[i].setParent(NULL);
NodeList[i].setLeft(NULL);
NodeList[i].setRight(NULL);
heap.Insert(NodeList[i]);
}
for(i=0;i<n;i++)
{
parent=new HuffmanTreeNode<Elem>;
firstChild=heap.RemoveMin();
secondChild=heap.RemoveMin();
MergeTree(firstChild,secondChild,parent);
heap.Insert(*parent);
root=parent;
}
delete []NodeList;
}
template<class Elem>
void HuffmanTree<Elem>::DeleteTree(HuffmanTreeNode<Elem>* root)
{
if(root!=NULL)
{
DeleteTree(root->getLeft());
DeleteTree(root->getRight());
delete root;
}
}
template<class Elem>
HuffmanTree<Elem>::~HuffmanTree()
{
DeleteTree(root);
}
template<class Elem>
void HuffmanTree<Elem>::print(HuffmanTreeNode<Elem>* root) const
{
if(root!=NULL)
{
print(root->getLeft());
cout<<root->getVal()<<" ";
print(root->getRight());
}
}
#endif
主程序:
"main.cpp"
#include <iostream>
#include "MinHeap.h"
#include "HuffmanTreeNode.h"
#include "HuffmanTree.h"
using namespace std;
int main()
{
int ch[20];
for(int i=0;i<20;i++)
ch[i]=i;
HuffmanTree<int> A(ch,20);
A.print(A.getRoot());
return 0;
}
请哪位高手帮我在VC6.0中运行看一下哪里出错了,对每个算法是没有错误的。只是当他们连在一起要注意些什么。请高手指点。谢谢。
一下是程序:
树结点类实现。
//“HuffmanTreeNode.h"
#if ! defined(_HUFFMANTREENODE_H)
#define _HUFFMANTREENODE_H
template<class Elem>
class HuffmanTreeNode
{
private:
HuffmanTreeNode* left;
HuffmanTreeNode* right;
HuffmanTreeNode* parent;
Elem element;
public:
HuffmanTreeNode(Elem elementval,HuffmanTreeNode* leftval=NULL,
HuffmanTreeNode* rightval=NULL,
HuffmanTreeNode* parentval=NULL)
{
element=elementval;
left=leftval;
right=rightval;
parent=parentval;
}
HuffmanTreeNode(HuffmanTreeNode* leftval=NULL,HuffmanTreeNode* rightval=NULL,
HuffmanTreeNode* parentval=NULL)
{
left=leftval;
right=rightval;
parent=parentval;
}
virtual ~HuffmanTreeNode(){}
void setVal(Elem& e)
{
element=e;
}
Elem& getVal()
{
return element;
}
HuffmanTreeNode* getLeft()
{
return left;
}
void setLeft(HuffmanTreeNode* T)
{
left=T;
}
HuffmanTreeNode* getRight()
{
return right;
}
void setRight(HuffmanTreeNode* T)
{
right=T;
}
HuffmanTreeNode* getParent()
{
return parent;
}
void setParent(HuffmanTreeNode* T)
{
parent=T;
}
};
#endif
最小堆优先队列。
"MinHeap.h"
#if ! defined(_MINHEAP_H)
#define _MINHEAP_H
template<class Elem>
class MinHeap
{
private:
Elem* heapArray;
int CurrentSize;
int MaxSize;
public:
MinHeap(const int n);
virtual~ MinHeap()
{
delete []heapArray;
}
void BuildHeap();
bool isLeaf(int pos) const;
int leftChild(int pos) const;
int rightChild(int pos) const;
//return parent position
int parent(int pos) const;
//删除给定下标的元素。
bool Remove(int pos,Elem& node);
void SiftDown(int left);
void SiftUp(int position);
bool Insert(const Elem& newNode);
Elem& RemoveMin();
bool print() const;
};
template<class Elem>
MinHeap<Elem>::MinHeap(const int n)
{
if(n<=0)
return ;
CurrentSize=0;
MaxSize=n;
heapArray=new Elem[MaxSize];
BuildHeap();
}
template<class Elem>
void MinHeap<Elem>::BuildHeap()
{
for(int i=CurrentSize/2-1;i>=0;i--)
SiftDown(i);
}
template<class Elem>
bool MinHeap<Elem>::isLeaf(int pos) const
{
return (pos>=CurrentSize/2) && (pos<CurrentSize);
}
template<class Elem>
int MinHeap<Elem>::leftChild(int pos) const
{
return 2*pos+1;
}
template<class Elem>
int MinHeap<Elem>::rightChild(int pos) const
{
return 2*pos+2;
}
template<class Elem>
int MinHeap<Elem>::parent(int pos) const
{
return (pos-1)/2;
}
//向下筛选。
template<class Elem>
void MinHeap<Elem>::SiftDown(int left)
{
int i=left; //标记父结点。
int j=2*i+1;
Elem temp=heapArray[i];
while(j<CurrentSize)
{
if((j<CurrentSize-1) && (heapArray[j]>heapArray[i]))
j++; //指向右子结点
if(temp>heapArray[j])
{
heapArray[i]=heapArray[j];
i=j;
j=2*j+1;
}
else
break;
}
heapArray[i]=temp;
}
//向上筛选
template<class Elem>
void MinHeap<Elem>::SiftUp(int position)
{
int temppos=position;
Elem temp=heapArray[temppos];
while((temppos>0) && (heapArray[parent(temppos)]>temp))
{
heapArray[temppos]=heapArray[parent(temppos)];
temppos=parent(temppos);
}
heapArray[temppos]=temp;
}
template<class Elem>
bool MinHeap<Elem>::Insert(const Elem& newNode)
{
if(CurrentSize==MaxSize)
return false;
heapArray[CurrentSize]=newNode;
SiftUp(CurrentSize);
CurrentSize++;
return true;
}
template<class Elem>
Elem& MinHeap<Elem>::RemoveMin()
{
if(CurrentSize==0)
{
cout<<"Can't Delete ";
}
else
{
Elem temp=heapArray[0];
heapArray[0]=heapArray[CurrentSize-1];
CurrentSize--;
if(CurrentSize>1)
SiftDown(0);
return temp;
}
}
template<class Elem>
bool MinHeap<Elem>::Remove(int pos,Elem& node)
{
if((pos<0) || (pos>CurrentSize))
return false;
Elem temp=heapArray[pos];
heapArray[pos]=heapArray[--CurrentSize];
SiftUp(pos);
SiftDown(pos);
node=temp;
return true;
}
template<class Elem>
bool MinHeap<Elem>::print() const
{
if(CurrentSize<0)
return false;
for(int i=0;i<CurrentSize;i++)
cout<<heapArray[i]<<" ";
return true;
}
#endif
Huffman树类实现。
"HuffmanTree.H"
#if !defined(_HUFFMANTREE_H)
#define _HUFFMANTREE_H
template<class Elem>
class HuffmanTree
{
private:
HuffmanTreeNode<Elem>* root;
void MergeTree(HuffmanTreeNode<Elem> & ht1,
HuffmanTreeNode<Elem> &ht2,
HuffmanTreeNode<Elem>* parent);
public:
HuffmanTree(Elem Weiht[],int n);
void DeleteTree(HuffmanTreeNode<Elem>* root);
~HuffmanTree();
void print(HuffmanTreeNode<Elem>* root) const;
HuffmanTreeNode<Elem>* getRoot()
{
return root;
}
};
template<class Elem>
void HuffmanTree<Elem>::MergeTree(HuffmanTreeNode<Elem> &ht1,
HuffmanTreeNode<Elem> &ht2,
HuffmanTreeNode<Elem> *parent)
{
Elem it;
parent->setLeft(&ht1);
parent->setRight(&ht2);
it=ht1.getVal()+ht2.getVal();
parent->setVal(it);
parent->setParent(NULL);
ht1.setParent(parent);
ht2.setParent(parent);
}
template<class Elem>
HuffmanTree<Elem>::HuffmanTree(Elem Weight[],int n)
{
MinHeap<HuffmanTreeNode<Elem> > heap(n);
HuffmanTreeNode<Elem> *parent,firstChild,secondChild;
HuffmanTreeNode<Elem>* NodeList=new HuffmanTreeNode<Elem>[n];
for(int i=0;i<n;i++)
{
NodeList[i].setVal(Weight[i]);
NodeList[i].setParent(NULL);
NodeList[i].setLeft(NULL);
NodeList[i].setRight(NULL);
heap.Insert(NodeList[i]);
}
for(i=0;i<n;i++)
{
parent=new HuffmanTreeNode<Elem>;
firstChild=heap.RemoveMin();
secondChild=heap.RemoveMin();
MergeTree(firstChild,secondChild,parent);
heap.Insert(*parent);
root=parent;
}
delete []NodeList;
}
template<class Elem>
void HuffmanTree<Elem>::DeleteTree(HuffmanTreeNode<Elem>* root)
{
if(root!=NULL)
{
DeleteTree(root->getLeft());
DeleteTree(root->getRight());
delete root;
}
}
template<class Elem>
HuffmanTree<Elem>::~HuffmanTree()
{
DeleteTree(root);
}
template<class Elem>
void HuffmanTree<Elem>::print(HuffmanTreeNode<Elem>* root) const
{
if(root!=NULL)
{
print(root->getLeft());
cout<<root->getVal()<<" ";
print(root->getRight());
}
}
#endif
主程序:
"main.cpp"
#include <iostream>
#include "MinHeap.h"
#include "HuffmanTreeNode.h"
#include "HuffmanTree.h"
using namespace std;
int main()
{
int ch[20];
for(int i=0;i<20;i++)
ch[i]=i;
HuffmanTree<int> A(ch,20);
A.print(A.getRoot());
return 0;
}
请哪位高手帮我在VC6.0中运行看一下哪里出错了,对每个算法是没有错误的。只是当他们连在一起要注意些什么。请高手指点。谢谢。