64,649
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using std::ostream;
class BiNode
{
public:
char name;//字母的名字
int freq;//出现频率
BiNode* left;//left child
BiNode* right;//right child
BiNode* parent;//parent
public:
BiNode()
{
}
BiNode(char n,int f = 0)
:name(n),freq(f),
left(0),right(0),parent(0)
{
}
~BiNode()
{
}
public:
bool operator==(const BiNode& src)
{
return (this->name==src.name) &&
(this->freq==src.freq)&&
(this->left==src.left)&&
(this->right==src.right)&&
(this->parent==src.parent);
}
bool operator!=(const BiNode& src)
{
return !operator==(src);
}
BiNode& operator=(const BiNode& src)
{
if(*this != src)
{
this->name = src.name;
this->freq = src.freq;
this->left = src.left;
this->right = src.right;
this->parent = src.parent;
}
return *this;
}
friend bool operator <(const BiNode& lhs,const BiNode& rhs);
friend bool operator>(const BiNode& lhs,const BiNode& rhs);
friend bool operator <=(const BiNode& lhs,const BiNode& rhs);
friend bool operator>=(const BiNode& lhs,const BiNode& rhs);
friend ostream& operator <<(ostream& os,const BiNode& node);
friend BiNode& operator+(BiNode& lhs,BiNode& rhs);
};
bool operator>(const BiNode& lhs,const BiNode& rhs)
{
return lhs.freq > rhs.freq;
}
bool operator <(const BiNode& lhs,const BiNode& rhs)
{
return lhs.freq < rhs.freq;
}
bool operator>=(const BiNode& lhs,const BiNode& rhs)
{
return !(operator <(lhs,rhs));
}
bool operator <=(const BiNode& lhs,const BiNode& rhs)
{
return !(operator>(lhs,rhs));
}
ostream& operator <<(ostream& os,const BiNode& node)
{
return os <<node.name <<':' <<node.freq;
}
BiNode& operator+(BiNode& lhs,BiNode& rhs)
{
BiNode* bi = new BiNode;
bi->freq = lhs.freq+rhs.freq;
bi->left = &lhs;
bi->right = &rhs;
bi->parent = 0;
lhs.parent = bi;
rhs.parent = bi;
return *bi;
}
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
template <class T>
class Heap{
int heap_size;//current size of the heap
int heap_length;//total length of the heap
T* A;
public:
explicit Heap(int length)
:heap_size(0),heap_length(length)
{
A = new T[heap_length+1];//第0个数组单元不存数据
}
~Heap()
{
delete [] A;
}
private:
void HeapAdjust(T A[],int i,int n)
{
int j = 2*i;//j is i's left child
T tmp = A[i];
while(j <= n)
{
if(j < n && A[j] > A[j+1])
++j;
if(tmp <= A[j])
break;
A[j/2] = A[j];
j *= 2;
}
A[j/2] = tmp;
}
public:
bool top(T& data)
{
if(heap_size <= 0)
{
cerr <<"The heap is empty";
return false;
}
data = A[1];
return true;
}
bool pop(T& data)
{
if(heap_size <= 0)
{
cerr <<"heap underflow";
return false;
}
data = A[1];
A[1] = A[heap_size--];
HeapAdjust(A,1,heap_size);
return true;
}
bool Heap_Insert(T data)
{
int i = ++heap_size;
if(i > heap_length)
{
cerr <<"heap overflow";
--heap_size;
return false;
}
while(i > 1 && A[i/2] > data)
{
A[i] = A[i/2];
i /= 2;
}
A[i] = data;
return true;
}
bool Heap_empty()
{
if(heap_size <= 0)
return true;
return false;
}
};
template <>//对BiNode* 的特化
class Heap <BiNode* >
{
int heap_size;
int heap_length;
BiNode** A;
public:
explicit Heap(int length)
:heap_length(length),heap_size(0)
{
A = new BiNode*[heap_length+1];
}
~Heap()
{
delete [] *A;
}
private:
void HeapAdjust(BiNode** A,int i,int n)
{
int j = i*2;
BiNode* tmp = A[i];
while(j < n)
{
if(j+1 < n && A[j]->freq > A[j+1]->freq)
++j;
if(tmp->freq <= A[j]->freq)
break;
A[j/2] = A[j];
j *= 2;
}
A[j/2] = tmp;
}
public:
bool top(BiNode* data)
{
if(heap_size <= 0)
{
cerr <<"The heap is empty";
return false;
}
*data = *A[1];
return true;
}
bool pop(BiNode*& data)
{
if(heap_size <= 0)
{
cerr <<"heap underflow";
return false;
}
data = A[1];
A[1] = A[heap_size--];
HeapAdjust(A,1,heap_size);
return true;
}
bool Heap_Insert(BiNode* data)
{
int i = ++heap_size;
if(i > heap_length)
{
cerr <<"heap overflow";
--heap_size;
return false;
}
while(i > 1 && A[i/2]->freq > data->freq)
{
A[i] = A[i/2];
i /= 2;
}
A[i] = data;
return true;
}
bool Heap_empty()
{
if(heap_size <= 0)
return true;
return false;
}
};
int main()
{
Heap <BiNode* > heap(30);
heap.Heap_Insert(new BiNode('a',12));
heap.Heap_Insert(new BiNode('b',11));
heap.Heap_Insert(new BiNode('c',1));
heap.Heap_Insert(new BiNode('d',2));
heap.Heap_Insert(new BiNode('e',5));
BiNode* p = 0;
while(!heap.Heap_empty())
{
heap.pop(p);
cout <<*p <<endl;
}
}
d:\>a
c:1
d:2
e:5
b:11
a:12
d:\>
template <class T, typename Less = std::less<T> > // 小于号函数类型
class Heap{
int heap_size;//current size of the heap
int heap_length;//total length of the heap
T* A;
Less less; // < 函数对象
...
void HeapAdjust(T A[],int i,int n)
{
int j = 2*i;//j is i's left child
T tmp = A[i];
while(j <= n)
{
if(j < n && A[j] > A[j+1])
++j;
if(less(tmp, A[j]))
break;
A[j/2] = A[j];
j *= 2;
}
A[j/2] = tmp;
}
}
struct less_p
{
bool operator () (const BiNode *const & lhs,const BiNode *const & rhs)
{
return lhs->freq < rhs->freq;
}
};
int main()
{
Heap <BiNode*, less_p> heap(30);
...
}