【数据结构】二叉堆与左偏树

// 二叉堆
void up( int i )
{
    int dad = i >> 1,data = heap[i];
    while( dad != 0 && heap[dad] > data )
    {
        heap[i] = heap[dad];
        i = dad;
        dad = i >> 1;
    }
    heap[i] = data;
}

void down( int i )
{
    int son = i << 1,data = heap[i];
    while( son <= sum )
    {
        if( son < sum && heap[son] > heap[son+1] ) son++;
        if( heap[son] >= data ) break;
        heap[i] = heap[son];
        i = son;
        son = i << 1;
    }
    heap[i] = data;
}


// 左偏树
int merge( int aa,int bb )
{
    if( !aa ) return bb;
    if( !bb ) return aa;
    if( key[aa] > key[bb] ) swap( aa,bb );
    rt[aa] = merge( rt[aa],bb );
    if( dis[ rt[aa] ] > dis[ lt[aa] ] ) swap( rt[aa],lt[aa] );
    if( !rt[aa] ) dis[aa] = 0; else dis[aa] = dis[ rt[aa] ] + 1;
    return a;
}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...