A STAR的实现和应用(c++版) 下

下面是工程里的几个文件,我把他们贴上来(有谁能告诉我直接上传吗)

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)
{
        
forint 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*+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)
{
    
forint 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;
    
forint 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);
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值