下面是工程里的几个文件,我把他们贴上来(有谁能告诉我直接上传吗)
1.heap.h
#ifndef __HEAP_H_
#define __HEAP_H_
#define SIZE 51*51
#define LT( a,b) ( a < b)
#define BT( a,b) ( a > b)
namespace my ... {
template <class T>
void sswap( T& a,T& b)
...{
T tmp = a;
a = b;
b = tmp;
}
/**//*
heap.h
Copyright (c) 2006.10 by yuzehua
*/
template< class T>
class Heap
...{
public:
Heap(T* First,T* End)
...{
int dc =(char *)End - (char *)First;
m_size = dc/sizeof(T) ;
assert( m_size < SIZE);
m_cur = m_size + 1;
m_arry = new T[m_size +1];
memcpy( m_arry+1,First,dc); //copy
}
Heap( int size = SIZE)
...{
m_size = size;
m_cur = 1;
m_arry = new T[m_size +1];
}
~Heap()
...{
delete[] m_arry;
m_size = 0;
}
void make_heap(int position,int len);
void push_heap(T &ele);
void pop_heap();
T heap_front();
void sort_heap(int position,int len);
bool is_empty();
int get_idx2( T& );
bool in_heap( const T& );
void change_content(int idx, const T& val);
T operator[]( int idx)...{ return m_arry[idx];}
protected:
void heap_adj_up(T *a, int position, int len);
void heap_adj_down( T *a, int position,int len);
private:
Heap& operator=( const Heap& other)...{ return *this;}
Heap(Heap& rth)...{ }
T* m_arry;
int m_size;
int m_cur;
};
template<class T>
void Heap<T>::heap_adj_down(T *a, int position, int len)
...{
for( int i=position; i<=len;)
...{
int j;
if( i*2 > len) //no children
...{
break;
}
else if( i*2 == len) //have one child
...{
j = 2*i;
}
else //( i*2 +1 <= len) //have two child
...{
if( LT(a[i*2],a[i*2+1]) ) //record smaller one
j = 2*i;
else
j = 2*i +1;
}
if( BT(a[i],a[j]) )
...{
sswap<T>(a[i],a[j]);
i = j;
}
else
break;
}
}
template< class T>
void Heap<T>::heap_adj_up( T *a, int position,int len)
...{
for( int i=position; i>1; i/=2)
...{
if( LT( a[i],a[i/2])) //cur node is smaller than father
...{
sswap<T>(a[i],a[i/2]);
}
}
}
template< class T>
void Heap<T>::make_heap(int position,int len)
...{
m_cur = len +1;
if( len == 1)
return;
for( int i=len/2; i>=position; i--) //from breaf -1 to the top;
...{
heap_adj_down( m_arry,i,len );
}
}
template< class T>
void Heap<T>::push_heap(T &ele)
...{
assert( m_cur <= SIZE);
m_arry[m_cur++] = ele;
heap_adj_up(m_arry,m_cur-1,SIZE);
}
template< class T>
void Heap<T>:: pop_heap()
...{
assert(m_cur >=1);
m_arry[1] = m_arry[--m_cur];
heap_adj_down(m_arry,1,m_cur-1);
}
template< class T>
T Heap<T>::heap_front()
...{
return m_arry[1];
}
template< class T>
void Heap<T>::sort_heap(int position,int len)
...{
assert(position >=1);
assert( len<=SIZE);
int start = position;
#define __HEAP_H_
#define SIZE 51*51
#define LT( a,b) ( a < b)
#define BT( a,b) ( a > b)
namespace my ... {
template <class T>
void sswap( T& a,T& b)
...{
T tmp = a;
a = b;
b = tmp;
}
/**//*
heap.h
Copyright (c) 2006.10 by yuzehua
*/
template< class T>
class Heap
...{
public:
Heap(T* First,T* End)
...{
int dc =(char *)End - (char *)First;
m_size = dc/sizeof(T) ;
assert( m_size < SIZE);
m_cur = m_size + 1;
m_arry = new T[m_size +1];
memcpy( m_arry+1,First,dc); //copy
}
Heap( int size = SIZE)
...{
m_size = size;
m_cur = 1;
m_arry = new T[m_size +1];
}
~Heap()
...{
delete[] m_arry;
m_size = 0;
}
void make_heap(int position,int len);
void push_heap(T &ele);
void pop_heap();
T heap_front();
void sort_heap(int position,int len);
bool is_empty();
int get_idx2( T& );
bool in_heap( const T& );
void change_content(int idx, const T& val);
T operator[]( int idx)...{ return m_arry[idx];}
protected:
void heap_adj_up(T *a, int position, int len);
void heap_adj_down( T *a, int position,int len);
private:
Heap& operator=( const Heap& other)...{ return *this;}
Heap(Heap& rth)...{ }
T* m_arry;
int m_size;
int m_cur;
};
template<class T>
void Heap<T>::heap_adj_down(T *a, int position, int len)
...{
for( int i=position; i<=len;)
...{
int j;
if( i*2 > len) //no children
...{
break;
}
else if( i*2 == len) //have one child
...{
j = 2*i;
}
else //( i*2 +1 <= len) //have two child
...{
if( LT(a[i*2],a[i*2+1]) ) //record smaller one
j = 2*i;
else
j = 2*i +1;
}
if( BT(a[i],a[j]) )
...{
sswap<T>(a[i],a[j]);
i = j;
}
else
break;
}
}
template< class T>
void Heap<T>::heap_adj_up( T *a, int position,int len)
...{
for( int i=position; i>1; i/=2)
...{
if( LT( a[i],a[i/2])) //cur node is smaller than father
...{
sswap<T>(a[i],a[i/2]);
}
}
}
template< class T>
void Heap<T>::make_heap(int position,int len)
...{
m_cur = len +1;
if( len == 1)
return;
for( int i=len/2; i>=position; i--) //from breaf -1 to the top;
...{
heap_adj_down( m_arry,i,len );
}
}
template< class T>
void Heap<T>::push_heap(T &ele)
...{
assert( m_cur <= SIZE);
m_arry[m_cur++] = ele;
heap_adj_up(m_arry,m_cur-1,SIZE);
}
template< class T>
void Heap<T>:: pop_heap()
...{
assert(m_cur >=1);
m_arry[1] = m_arry[--m_cur];
heap_adj_down(m_arry,1,m_cur-1);
}
template< class T>
T Heap<T>::heap_front()
...{
return m_arry[1];
}
template< class T>
void Heap<T>::sort_heap(int position,int len)
...{
assert(position >=1);
assert( len<=SIZE);
int start = position;