ACM 左偏樹(模板)

#include <iostream>

using namespace std;

struct Node
{
    int Dat;
    int dist;
    Node *pLeft,*pRight;
};

void Swap(Node * &pA,Node * &pB)
{
    Node *pt=pA;
    pA=pB;
    pB=pt;
}

int Dist(Node *p)
{
    if(p==NULL) return 0;
    else return p->dist;
}

Node *Merge(Node * &pA,Node * &pB)
{
    if(pA==NULL) return pB;
    if(pB==NULL) return pA;
    if(pA->Dat>pB->Dat) Swap(pA,pB);
    pA->pRight=Merge(pA->pRight,pB);
    if(Dist(pA->pRight)>Dist(pA->pLeft)) Swap(pA->pRight,pA->pLeft);
    pA->dist=Dist(pA->pRight)+1;
    return pA;
}

int main()
{
    Node *pA,*pB;

    pA=new Node;
    pB=new Node;
    pA->Dat=1;
    pA->pLeft=NULL;pA->pRight=NULL;
    pB->Dat=2;
    pB->pLeft=NULL;pB->pRight=NULL;

    pA=Merge(pA,pB);
    cout<<pA->pRight<<endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章