首页 技术 正文
技术 2022年11月11日
0 收藏 733 点赞 4,496 浏览 2107 个字
 #include <iostream>
#include <vector>
#include <queue>
using namespace std;
template <typename T>
class LeftlistNode
{
public:
T key;
int npl;
LeftlistNode* left;
LeftlistNode* right; LeftlistNode(T& value, LeftlistNode* l = NULL, LeftlistNode* r = NULL, int n = ):
key(value), left(l), right(r), npl(n){}
}; template <typename T>
class LeftlistHeap{
private:
LeftlistNode<T>* mRoot;
public:
LeftlistHeap(){}
LeftlistHeap(vector<T>& items){
mRoot = buildHeap(items);
}
~LeftlistHeap(){
destory(mRoot);
}
void merge(LeftlistHeap<T>* other){
if(this == other)
return;
mRoot = _merge(mRoot, other->mRoot);
}
void insert(T& key){
mRoot = _merge(mRoot, new LeftlistNode<T>(key));
}
void remove(){
LeftlistNode<T> old = mRoot;
mRoot = _merge(mRoot->Left, mRoot->right);
delete old;
}
T get_min(){
return mRoot->key;
} void destory(LeftlistNode<T>* & mRoot){
if(mRoot == NULL)
return;
if(mRoot->left != NULL)
destory(mRoot->left);
if(mRoot->right != NULL)
destory(mRoot->right);
delete mRoot;
} private:
LeftlistNode<T>* _merge1(LeftlistNode<T>* x, LeftlistNode<T>* y){
if(x == NULL)
return y;
if(y == NULL)
return x;
if(x->key < y->key)
_merge2(x, y);
else
_merge2(y, x);
}
LeftlistNode<T>* _merge2(LeftlistNode<T>* x, LeftlistNode<T>* y){
if(x->left == NULL)
x->left = y;
else{
x->right = _merge1(x->right, y);
if(x->left->npl < x->right->npl){
LeftlistNode<T>* temp = x->left;
x->left = x->right;
x->right = temp;
}
x->npl = x->right->npl + ;
}
return x;
} LeftlistNode<T>* _merge(LeftlistNode<T>* x, LeftlistNode<T>* y){
if(x == NULL && y == NULL)
return NULL;
else if(x == NULL && y != NULL)
return y;
else if(y == NULL && x != NULL)
return x;
else{
if(x->key > y->key){
LeftlistNode<T>* tmp = x;
x = y;
y = tmp;
}
if(x->left == NULL)
x->left = y;
else{
x->right = _merge(x->right, y);
if(x->left->npl < x->right->npl){
LeftlistNode<T>* tmp = x->left;
x->left = x->right;
x->right = tmp;
}
x->npl = x->right->npl + ;
}
}
return x;
}
LeftlistNode<T>* buildHeap(vector<T>& items){
queue<LeftlistNode<T>*> tmp_queue;
for(int i = ; i < items.size(); ++i)
tmp_queue.push(new LeftlistNode<T>(items[i]));
while(tmp_queue.size() > ){
LeftlistNode<T>* t1 = tmp_queue.front();
tmp_queue.pop();
LeftlistNode<T>* t2 = tmp_queue.front();
tmp_queue.pop();
tmp_queue.push(_merge1(t1, t2));
}
return tmp_queue.front();
} }; int main(){
int a[]= {,,,,,,,};
vector<int> lt(a, a + );
LeftlistHeap<int> yj(lt);
cout << yj.get_min() << endl;
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898