#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;
}
ACM 左偏树(模板)
最新推荐文章于 2019-08-19 15:21:00 发布