1. 程式人生 > >演算法導論第十九章二項堆

演算法導論第十九章二項堆

自己寫的bug應該很多

BinHeapNode.h

#include<iostream>
using namespace std;
class BinHeap;
class BinHeapNode
{
private:
	friend BinHeap;
	int key;
	BinHeapNode*parent;
	BinHeapNode*child;
	BinHeapNode*sibling;
	int degree;
	int maxsize;
public:
	BinHeapNode(int num):key(num),parent(NULL),child(NULL),sibling(NULL),degree(0){}
	~BinHeapNode()
	{
		
	}
	void Destory()
	{
	}
	

};

BinHeap.h

#include"BinHeapNode.h"
class BinHeap
{
private:
	BinHeapNode* head;
	int Degree;
public:
	BinHeap(int num):Degree(num),head(NULL){}
	~BinHeap()
	{
		Destory1(head);
	}
	void Destory1(BinHeapNode*node)
	{
		if(node!=NULL)
		{
			Destory1(node->sibling);
			Destory1(node->child);
			delete node;
		}
	}
	BinHeapNode* FindMinNum()
	{
		BinHeapNode *y=NULL,*p=head;
		int min=0x7fffffff;
		while(p)
		{
			if(p->key<min)
			{
				min=p->key;
				y=p;
			}
			p=p->sibling;
		}
		return y;
	}
	//將以y為根的二項樹和以z為根的二項樹合併,使z為y的父節點
	void BinLink(BinHeapNode* y,BinHeapNode*z)
	{
		y->parent=z;
		y->sibling=z->child;
		z->child=y;
		z->degree++;
	}
	BinHeapNode*BinHeapMerge(BinHeapNode*head1,BinHeapNode*head2)
	{
		if(head1==NULL)
		{
			head1=head2;
			return head1;
		}
		if(head2==NULL)
		{
			return head1;
		}
		BinHeapNode*x,*node1,*node2,*node3;
		if(head1->degree<=head2->degree)
		{
			x=head1;
			node1=x->sibling;
			node2=head2;
		}
		else
		{
			x=head2;
			node1=head1;
			node2=head2->sibling;
		}
		node3=x;
		while(node1&&node2)
		{
			if(node1->degree>node2->degree)
			{
				x->sibling=node2;
				node2->parent=x;
				x=node2;
				node2=node2->sibling;
			}
			else
			{
				x->sibling=node1;
				node1->parent=x;
				x=node1;
				node1=node1->sibling;
			}
		}
		if(node1==NULL)
		{
			while(node2)
			{
				x->sibling=node2;
				node2->parent=x;
				x=node2;
				node2=node2->sibling; 
			}
		}
		else
		{
			while(node1)
			{
				x->sibling=node1;
				node1->parent=x;
				x=node1;
				node1=node1->sibling;
			}
		}
		return node3;
	}
	void BinHeapUnion(BinHeapNode*head1,BinHeapNode*head2)
	{
		head=BinHeapMerge(head1,head2);
		if(head==NULL)
			return ;
		BinHeapNode *prex=NULL,*x=head,*nextx=x->sibling;
		while(nextx)
		{
			if(x->degree!=nextx->degree||(nextx->sibling!=NULL&&nextx->sibling->degree==x->degree))
			{
				prex=x;
				x=nextx;
			}
			else if(x->key<=nextx->key)
			{
				x->sibling=nextx->sibling;
				BinLink(nextx,x);
			}
			else 
			{
				if(prex==NULL)
				{
					head=nextx;
					head->parent=NULL;
				}
					
				else
					prex->sibling=nextx;

				BinLink(x,nextx);
				x=nextx;
			}
			nextx=nextx->sibling;
		}
	}
	void BinInsert(int x)
	{
		BinHeapNode*node=new BinHeapNode(x);
		node->parent=NULL;
		node->child=NULL;
		node->sibling=NULL;
		node->degree=0;
		BinHeapUnion(head,node);
	}
	BinHeapNode*ExtractMin()
	{
		BinHeapNode*x=FindMinNum();
		BinHeapNode*p=x->child;
		while(p->sibling)
		{
			p=p->sibling;
		}
		BinHeapNode*q=p,*node;
		while(q->parent)
		{
			q->sibling=q->parent;
			q=q->parent;
		}
		q=p;
		while(q->sibling)
		{
			q->sibling->parent=q;
			q=q->sibling;
		}
		p->parent=NULL;
		BinHeapUnion(head,p);
		return x;
	}
	void BinHeapDceraseKey(BinHeapNode*x,int k)
	{
		if(k>x->key)
		{
			cout<<"error"<<endl;
			return ;
		}
		x->key=k;
		BinHeapNode*y=x;
		BinHeapNode* z=y->parent;
		while(z!=NULL&&y->key<z->key)
		{
			int temp;
			temp=y->key;
			y->key=x->key;
			x->key=temp;
			y=z;
			z=z->parent;
		}
	}
	void BinHeapDelete(BinHeapNode*node)
	{
		BinHeapDceraseKey(node,0);
		ExtractMin();
	}
	void print(BinHeapNode*node)
	{
		if(node!=NULL)
		{
			print(node->sibling);
			print(node->child);
			cout<<"the key is : "<<node->key<<endl;
			if(node->parent!=NULL)
			{
				if(node->parent->sibling==node)
					cout<<"the node: "<<node->key<<" is the sibling of: "<<node->parent->key<<endl;
				else
					cout<<"the node: "<<node->key<<" is the child of: "<<node->parent->key<<endl;
			}
			else
				cout<<"the partent of "<<node->key<<" is : NULL"<<endl;
		}
	}
	void Print()
	{
		print(head);
	}

};

test.cpp

#include"BinHeap.h"
int main()
{
	int a[7]={12,7,25,15,28,41,33};
	BinHeap heap1(4),heap(4);
	int i;
/*	for(i=0;i<7;i++)
	{
		heap1.BinInsert(a[i]);
	}
	heap1.Print();
	*/
	int b[19]={18,3,37,6,8,29,10,44,30,23,22,48,31,17,45,32,24,50,55};
	BinHeap heap2(4);
	for(i=0;i<19;i++)
	{
		heap2.BinInsert(b[i]);
	}
	heap2.Print();
	return 0;
}