Left-Stack inclined heap

Source: Internet
Author: User
Tags constructor int size

Heap.h

#ifndef _leftistheap_h #define _LEFTISTHEAP_H using namespace std;

Virtual base class Baseheap {public:virtual void Insert (int x) = 0;};
	Ordinary minimum heap class Heap:public baseheap {public:int size;   int *a;     Storage data Heap (int n=10000);  constructor void Merge (const heap &h);   Merge void insert with existing heap (int x);

Insert}; Left-hand Heap class leftistheap:public Baseheap {struct Leftistheapnode {//node int element;//element Leftistheapnode *left;// Left child Leftistheapnode *right; Right child int NPL; 0 path length Leftistheapnode (int x);
constructor}; Public:leftistheapnode *root; Root node leftistheap (leftistheapnode *root = nullptr); constructor static leftistheapnode* merge (leftistheapnode* H1, leftistheapnode* H2);//merge (implement function) void merge (const LEFTISTHEAP & AMP;H);//merge with existing heap (interface function) void Insert (int x);

Insert};  Oblique Heap class skewheap:public Baseheap {struct Skewheapnode {//node int element;  Element Skewheapnode *left;  Left child Skewheapnode *right;  Right child skewheapnode (int x);
constructor}; Public:skewheapnode *root; Root node skewheap (skewheapnode* root = nullptr); constructor static skewheapnode* Merge (skewheapnode* H1, skewheapnode* H2); Merge (implement function) void merge (const skewheap &h);//merge with existing heap (interface function) void Insert (int x);

Insert}; 
Global merge Function const SKEWHEAP &merge (const skewheap &H1, const skewheap &H2); 
Const LEFTISTHEAP &merge (const leftistheap &H1, const leftistheap &H2);  
Const heap &merge (const heap &H1, const heap &H2);
 void print (heap h);//print heap (for testing) #endif

Tmplate.h

#ifndef _template_h
#define _TEMPLATE_H
#include "heap.h"
#include <queue>
#include < iostream>

//Print heap (left-heap, oblique-heap) for testing
//sequence traversal, output by layer
template<typename t>
void print (T root)
{
	if (!root) return;
	int cur = 1;
	int curnum = 1;
	int nextnum = 0;
	queue<t>q; Queue
	Q.push (root);
	while (!q.empty ()) {//Non-empty time
		curnum--;
		T tmp = Q.front ();  Current team header elements out of queue
		cout << tmp->element << "";
		Q.pop ();
		if (tmp->left) {
			q.push (tmp->left);//left child into queue
			nextnum++;
		}
		if (tmp->right) {
			q.push (tmp->right);  Right child into queue
			nextnum++;
		}
		if (Curnum = = 0) {  //control per-layer output
			cout << Endl;
			cur++;
			Curnum = Nextnum;
			Nextnum = 0;
		}
	}
	cout << Endl;
}

Exchange Child
template<typename t>
void Swapchild (t &h)
{
	T tmp;
	TMP = h->right;
	H->right = h->left;
	H->left = tmp;
}

#endif

Heap.cpp

 #include "heap.h" #include <queue> #include <cstdlib> #include <climits> #include


<iostream> using namespace std;
	Heap::heap (int n): size (0) {a = (int*) malloc (sizeof (int) * (n+1));
	A[0] = int_min;
if (!a) printf ("error\n");
	}//initialization size is 0, Sentinel is minimum void Heap::merge (const heap &h) {int i;
	Insert the external heap directly for (i = 1; I! = size; i++) {insert (h.a[i]);
	}} const heap& merge (const heap &H1, const heap &H2) {int i;
	Heap *tmp = new Heap (h1.size+h2.size);//Create a heap for (i = 1; I <= h1.size; i++) {//Insert First heap Tmp->insert (H1.a[i]);
	} for (i = 1; I <= h2.size; i++) {//Insert a second heap Tmp->insert (H2.a[i]); } return *tmp;  return new Heap} void Heap::insert (int x) {int p = ++size;//size plus 1 a[p] = x;
	Insert int tmp = A[P];
	for (; a[p/2]>tmp; p/= 2) {//adjust position a[p] = A[P/2];
} A[p] = tmp;
	} void Print (heap h) {int i;
	for (i = 1; i < h.size; i++) {//Traverse output cout << h.a[i] << "";
} cout << Endl; }

Letist Heap.cpp

#include "heap.h" #include "template.h" using namespace std;
Leftistheap::leftistheap (Leftistheapnode *root): root (Root) {} leftistheap::leftistheapnode::leftistheapnode (int x) {element = x;//Assignment left = right = nullptr;//child set to empty NPL = 0;//0 path length initialized to 0} leftistheap::leftistheapnode* leftistheap :: Merge (leftistheapnode* H1, leftistheapnode* h2) {if (!H1) return H2;//h1 is empty, return H2 if (!H2) return H1;//h2 is empty, return H1 if (H1->element > H2->element)
		{//Maintain H1 element is always less than H2 leftistheapnode* tmp = h2;
		H2 = H1;
	H1 = tmp; } if (!h1->left) h1->left = h2;  If H1 has no left child, let H2 be H1 's left child else {//if H1 has left child h1->right = Merge (h1->right, H2);  
		Merge H1 right child and H2 if (H1-&GT;LEFT-&GT;NPL&LT;H1-&GT;RIGHT-&GT;NPL) Swapchild (H1); If the nature of the left-hand heap is destroyed, that is, the left child's 0 path length is less than the right child, exchange about child H1-&GT;NPL = H1-&GT;RIGHT-&GT;NPL + 1;
Update H1 's 0 path long} return H1; } const LEFTISTHEAP &merge (const leftistheap &AMP;H1, const leftistheap &AMP;H2) {//return value Leftistheapnode converted to Leftisthe APS, achieving standardized interfaces Leftistheap *ans=new LeftiStheap (Leftistheap::merge (H1.root, h2.root));
return *ans;

} void Leftistheap::merge (const leftistheap &h) {//calls an existing merge root = merge (root, h.root);}
	void Leftistheap::insert (int x) {if (!root) {///does not exist root node, new root = newly leftistheapnode (x);
		} else {//Otherwise, merge the new node and the original heap leftistheapnode *tmp = new Leftistheapnode (x);
	Root = Merge (root, TMP); }
}

Skewheap.cpp

#include "heap.h" #include "template.h" using namespace std;   Skewheap::skewheap (skewheapnode* root): root (Root) {} skewheap::skewheapnode::skewheapnode (int x) {element = x;   Assignment left = right = nullptr; Left and right children are empty} skewheap::skewheapnode* Skewheap::merge (skewheapnode* H1, skewheapnode* h2) {if (!H1) return H2;//h1 is empty, return H 2 if (!H2) return H1;
		H2 is empty, return H1 if (H1->element > H2->element) {//Maintain H1 element is always less than H2 skewheapnode* tmp = h2;
		H2 = H1;
	H1 = tmp; } if (!h1->left) h1->left = h2;  If H1 has no left child, let H2 be H1 's left child else {h1->right = merge (H1->right, H2);//merge H1 right child and H2 Swapchild (H1);//Swap Child} return
H1; } const SKEWHEAP &merge (const skewheap &AMP;H1, const skewheap &AMP;H2) {//return value Skewheapnode converted to SKEWHEAP for standardized interfaces Ske
	wheap* ans = new Skewheap (Skewheap::merge (H1.root, h2.root));
return *ans;

} void Skewheap::merge (const skewheap &h) {//calls an existing merge root = merge (root, h.root);} void Skewheap::insert (int x) {if (!root) {root = new SKEWHEAPNOde (x);//The root node is not present, new} else {///otherwise, the new node and the original heap are merged skewheapnode *tmp = new Skewheapnode (x);
	Root = Merge (root,tmp); }
}

Main.cpp

#include "heap.h" #include "template.h" #include <algorithm> #include <cstdio> #include <chrono> using

namespace Std;
	int main () {Srand (Unsigned (Time (nullptr)));
	int n = 10000;
	int i;
	int count = 0;
	Double sum1=0, sum2=0, sum3=0;//statistics three time chrono::time_point<chrono::high_resolution_clock> start,end;//start, end
		Chrono::d uration<double> seconds;//Chronograph while (n) {//n for test data size count++;
			if (count = = 100) {//Calculate 100 times after output printf ("%d:\n%lf,%lf,%lf\n", N, Sum1, sum2, sum3);
			Count = 0; N-= 1000; Test data reduction of sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
		Updated sum of 0} leftistheap H1, H2, H3;
		Skewheap H4, H5, H6;

		Heap H7 (N), H8 (n), H9 (n);

		int a[10002];
		for (i = 0; i < n; i++) {A[i] = 2 * i; } random_shuffle (A, a + N);
			Generate data (even) for (int i = 0; i < n; i++) {//Generate First Tree H1.insert (A[i]);
			H4.insert (A[i]);

		H7.insert (A[i]);
		} for (i = 0; i < n; i++) {A[i] = 2 * i + 1; } random_shuffle (A, a + N); Generate Data (odd) for (int i = 0; i < n; i++) {//Generate second Tree H2.insert (A[i]);
			H5.insert (A[i]);
		H8.insert (A[i]);
		}//Calculate leftistheap start = Chrono::high_resolution_clock::now ();
		H3 = Merge (H1, H2);
		End = Chrono::high_resolution_clock::now ();
		seconds = End-start;

		SUM1 = sum1 + Seconds.count ();
		Calculate skewheap start = Chrono::high_resolution_clock::now ();
		h6 = Merge (H4, H5);
		End = Chrono::high_resolution_clock::now ();
		seconds = End-start;

		sum2 = sum2 + Seconds.count ();
		Calculate Heap start = Chrono::high_resolution_clock::now ();
		H9 = Merge (H7, H8);
		End = Chrono::high_resolution_clock::now ();
		seconds = End-start;
	SUM3 = sum3 + Seconds.count ();
} system ("Pause"); }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.