左式堆的实现
来源:互联网 发布:用python进行量化分析 编辑:程序博客网 时间:2024/04/30 21:27
头文件:
#ifndef LeftistHeap_H#define LeftistHeap_H#include<iostream>using namespace std;template< typename T >struct LeftistNode{T data;LeftistNode *left;LeftistNode *right;int npl;LeftistNode(const T & theData,LeftistNode* lt = NULL,LeftistNode * rt = NULL,int np = 0):data(theData),left(lt),right(rt),npl(np){}};template< typename T >class LeftistHeap{private:LeftistNode< T > *root;LeftistNode< T > * mergetree(LeftistNode< T > *h1,LeftistNode< T > *h2);LeftistNode< T > * merge(LeftistNode< T > *h1,LeftistNode< T > *h2);void swapChildren(LeftistNode< T > *t);void Free(LeftistNode< T > *p);public:LeftistHeap();LeftistHeap(const T args[],const int n);~LeftistHeap();bool isEmpty() const;T findMin() const;void Insert(const T &x);void deleteMin();void makeEmpty();void merge(LeftistHeap &rhs);};#endif
具体的实现:
#include "LeftistHeap.h"template< typename T >LeftistHeap< T > ::LeftistHeap(const T args[],const int n){int i;root = NULL;for(i = 0;i < n;++i){Insert(args[i]);}}template< typename T >LeftistHeap< T > ::~LeftistHeap(){Free(root);}template< typename T >void LeftistHeap< T >::merge(LeftistHeap & rhs){if(this == &rhs)//avoid aliasing problems{return;}root = merge(root,rhs.root);rhs.root = NULL;}/** * Internal method to merge two roots.*/template< typename T >LeftistNode< T > * LeftistHeap< T > ::merge(LeftistNode< T > *h1,LeftistNode< T > *h2){if(h1 == NULL)return h2;if(h2 == NULL)return h1;if(h1->data < h2->data)return mergetree(h1,h2);elsereturn mergetree(h2,h1);}/** * Internal method to merge two roots. * Assumes trees are not empty,and h1's root contains smallest item*/template< typename T > LeftistNode< T > * LeftistHeap< T > ::mergetree(LeftistNode< T > *h1,LeftistNode< T > *h2){if(h1->left == NULL)h1->left = h2;else{h1->right = merge(h1->right,h2);if(h1->left->npl < h1->right->npl)swapChildren(h1);h1->npl = h1->right->npl+1;}return h1;} /*** Insert x,duplicates allowed*/template < typename T >void LeftistHeap< T >::Insert(const T &x){root = merge(new LeftistNode< T >(x),root);}/*** Remove the minimum item.* Throws underflowException if empty*/template < typename T >void LeftistHeap< T > ::deleteMin(){if(isEmpty()){cerr<<"Error!The heap is empty!";exit(1);}LeftistNode<T> *oldRoot = root;root = merge(root->left,root->right);delete oldRoot;}template< typename T >bool LeftistHeap< T >::isEmpty() const{return root == NULL;}template< typename T>void LeftistHeap< T >::swapChildren(LeftistNode< T> *t){LeftistNode< T > *left = t->left;t->left = t->right;t->right = left;}template< typename T >void LeftistHeap< T >::makeEmpty(){Free(root);}template< typename T >void LeftistHeap< T >::Free(LeftistNode< T > *p){if(p == NULL)return;LeftistNode< T > *tmp = p;Free(p->left);Free(p->right);delete tmp; }template< typename T >T LeftistHeap< T >::findMin() const{if(isEmpty()){cerr<<"Error! The heap is empty!";exit(1);}return root->data;}
作者博客:点击打开链接
- 左式堆的实现
- 左式堆合并的实现
- 数据结构之左式堆的实现
- 左式堆的实现与详解
- LeftistHeap左式堆的C++实现
- 三子棋的实现的实现的实现
- 优先队列——左式堆的实现
- 左式堆的简单实现(C语言描述)
- JAVA实现的时钟实现
- 模态框的的实现
- 杀毒软件的简单实现的简单实现
- android的实现电话号码的实现
- 音频的实现音乐声音的实现
- malloc的实现、内存池的实现
- heap的一些实现,二叉堆,左式堆,二项队列
- 异形窗体的实现
- 个性化的分页实现
- ASP数据库连接的实现
- ubuntu中rhythmbox音乐播放器乱码问题
- 写注释
- JVM内存模型及垃圾收集简介
- VC访问数据库程序 在编译器中执行正确 debug文件时无法访问数据库 提示“对象关闭时,无法访问数据库”
- Java像素级的操作
- 左式堆的实现
- yale_OS(2)——OS-xv6的源代码的调试
- 【小李木耳】2012年5月高兴事:哈尔滨集散中心成立、商标正式申请!
- 环境变量,include搜索路径,lib库搜索路径
- java用enum实现singleton的方法
- poj 1001 java大数类
- javascript Math 对象介绍
- javascript Date 对象介绍
- Java项目经验——程序员成长的关键