概要
本章介绍斜堆。和以往一样,本文会先对斜堆的理论知识进行简单介绍,然后给出C语言的实现。后续再分别给出C++和Java版本的实现;实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可。若文章有错误或不足的地方,请不吝指出!
目录
1. 斜堆的介绍
2. 斜堆的基本操作
3. 斜堆的C实现(完整源码)
4. 斜堆的C测试程序
转载请注明出处:斜堆(一)之 C语言的实现 - 如果天空不死 - 博客园
更多内容:数据结构与算法系列 目录
斜堆的介绍
斜堆(Skew heap)也叫自适应堆(self-adjusting heap),它是左倾堆的一个变种。和左倾堆一样,它通常也用于实现优先队列。它的合并操作的时间复杂度也是O(lg n)。
相比于左倾堆,斜堆的节点没有"零距离"这个属性。除此之外,它们斜堆的合并操作也不同。斜堆的合并操作算法如下:
(01) 如果一个空斜堆与一个非空斜堆合并,返回非空斜堆。
(02) 如果两个斜堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
(03) 合并后,交换新堆根节点的左孩子和右孩子。
第(03)步是斜堆和左倾堆的合并操作差别的关键所在,如果是左倾堆,则合并后要比较左右孩子的零距离大小,若右孩子的零距离 > 左孩子的零距离,则交换左右孩子;最后,在设置根的零距离。
斜堆的基本操作
1. 头文件
#ifndef _SKEW_HEAP_H_ #define _SKEW_HEAP_H_ typedef int Type; typedef struct _SkewNode{ Type key; // 关键字(键值) struct _SkewNode *left; // 左孩子 struct _SkewNode *right; // 右孩子 }SkewNode, *SkewHeap; // 前序遍历"斜堆" void preorder_skewheap(SkewHeap heap); // 中序遍历"斜堆" void inorder_skewheap(SkewHeap heap); // 后序遍历"斜堆" void postorder_skewheap(SkewHeap heap); // 获取最小值(保存到pval中),成功返回0,失败返回-1。 int skewheap_minimum(SkewHeap heap, int *pval); // 合并"斜堆x"和"斜堆y",并返回合并后的新树 SkewNode* merge_skewheap(SkewHeap x, SkewHeap y); // 将结点插入到斜堆中,并返回根节点 SkewNode* insert_skewheap(SkewHeap heap, Type key); // 删除结点(key为节点的值),并返回根节点 SkewNode* delete_skewheap(SkewHeap heap); // 销毁斜堆 void destroy_skewheap(SkewHeap heap); // 打印斜堆 void print_skewheap(SkewHeap heap); #endif
SkewNode是斜堆对应的节点类。
2. 合并
/* * 合并"斜堆x"和"斜堆y" * * 返回值: * 合并得到的树的根节点 */ SkewNode* merge_skewheap(SkewHeap x, SkewHeap y) { if(x == NULL) return y; if(y == NULL) return x; // 合并x和y时,将x作为合并后的树的根; // 这里的操作是保证: x的key < y的key if(x->key > y->key) swap_skewheap_node(x, y); // 将x的右孩子和y合并, // 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。 SkewNode *tmp = merge_skewheap(x->right, y); x->right = x->left; x->left = tmp; return x; }
merge_skewheap(x, y)的作用是合并x和y这两个斜堆,并返回得到的新堆。merge_skewheap(x, y)是递归实现的。
3. 添加
/* * 新建结点(key),并将其插入到斜堆中 * * 参数说明: * heap 斜堆的根结点 * key 插入结点的键值 * 返回值: * 根节点 */ SkewNode* insert_skewheap(SkewHeap heap, Type key) { SkewNode *node; // 新建结点 // 如果新建结点失败,则返回。 if ((node = (SkewNode *)malloc(sizeof(SkewNode))) == NULL) return heap; node->key = key; node->left = node->right = NULL; return merge_skewheap(heap, node); }
insert_skewheap(heap, key)的作用是新建键值为key的结点,并将其插入到斜堆中,并返回堆的根节点。
4. 删除
/* * 取出根节点 * * 返回值: * 取出根节点后的新树的根节点 */ SkewNode* delete_skewheap(SkewHeap heap) { SkewNode *l = heap->left; SkewNode *r = heap->right; // 删除根节点 free(heap); return merge_skewheap(l, r); // 返回左右子树合并后的新树 }
delete_skewheap(heap)的作用是删除斜堆的最小节点,并返回删除节点后的斜堆根节点。
注意:关于斜堆的"前序遍历"、"中序遍历"、"后序遍历"、"打印"、"销毁"等接口就不再单独介绍了。后文的源码中有给出它们的实现代码,Please RTFSC(Read The Fucking Source Code)!
斜堆的C实现(完整源码)
斜堆的头文件(skewheap.h)
View Code
斜堆的实现文件(skewheap.c)
View Code
斜堆的测试程序(skewheap_test.c)
View Code
斜堆的C测试程序
斜堆的测试程序已经包含在它的实现文件(skewheap_test.c)中了,这里仅给出它的运行结果:
== 斜堆(ha)中依次添加: 10 40 24 30 36 20 12 16 == 斜堆(ha)的详细信息: 10 is root 16 is 10's left child 20 is 16's left child 30 is 20's left child 40 is 30's left child 12 is 10's right child 24 is 12's left child 36 is 24's left child == 斜堆(hb)中依次添加: 17 13 11 15 19 21 23 == 斜堆(hb)的详细信息: 11 is root 13 is 11's left child 17 is 13's left child 23 is 17's left child 19 is 13's right child 15 is 11's right child 21 is 15's left child == 合并ha和hb后的详细信息: 10 is root 11 is 10's left child 12 is 11's left child 15 is 12's left child 21 is 15's left child 24 is 12's right child 36 is 24's left child 13 is 11's right child 17 is 13's left child 23 is 17's left child 19 is 13's right child 16 is 10's right child 20 is 16's left child 30 is 20's left child 40 is 30's left child
3.2 斜堆的C++实现(完整源码)斜堆的实现文件(SkewHeap.h)
/**
* C++: 斜堆
*
* @author skywang
* @date 2014/03/31
*/
#ifndef _SKEW_HEAP_HPP_
#define _SKEW_HEAP_HPP_
#include <iomanip>
#include <iostream>
using namespace std;
template <class T>
class SkewNode{
public:
T key; // 关键字(键值)
SkewNode *left; // 左孩子
SkewNode *right; // 右孩子
SkewNode(T value, SkewNode *l, SkewNode *r):
key(value), left(l),right(r) {}
};
template <class T>
class SkewHeap {
private:
SkewNode<T> *mRoot; // 根结点
public:
SkewHeap();
~SkewHeap();
// 前序遍历"斜堆"
void preOrder();
// 中序遍历"斜堆"
void inOrder();
// 后序遍历"斜堆"
void postOrder();
// 将other的斜堆合并到this中。
void merge(SkewHeap<T>* other);
// 将结点(key为节点键值)插入到斜堆中
void insert(T key);
// 删除结点(key为节点键值)
void remove();
// 销毁斜堆
void destroy();
// 打印斜堆
void print();
private:
// 前序遍历"斜堆"
void preOrder(SkewNode<T>* heap) const;
// 中序遍历"斜堆"
void inOrder(SkewNode<T>* heap) const;
// 后序遍历"斜堆"
void postOrder(SkewNode<T>* heap) const;
// 交换节点x和节点y
void swapNode(SkewNode<T> *&x, SkewNode<T> *&y);
// 合并"斜堆x"和"斜堆y"
SkewNode<T>* merge(SkewNode<T>* &x, SkewNode<T>* &y);
// 销毁斜堆
void destroy(SkewNode<T>* &heap);
// 打印斜堆
void print(SkewNode<T>* heap, T key, int direction);
};
/*
* 构造函数
*/
template <class T>
SkewHeap<T>::SkewHeap():mRoot(NULL)
{
}
/*
* 析构函数
*/
template <class T>
SkewHeap<T>::~SkewHeap()
{
destroy(mRoot);
}
/*
* 前序遍历"斜堆"
*/
template <class T>
void SkewHeap<T>::preOrder(SkewNode<T>* heap) const
{
if(heap != NULL)
{
cout<< heap->key << " " ;
preOrder(heap->left);
preOrder(heap->right);
}
}
template <class T>
void SkewHeap<T>::preOrder()
{
preOrder(mRoot);
}
/*
* 中序遍历"斜堆"
*/
template <class T>
void SkewHeap<T>::inOrder(SkewNode<T>* heap) const
{
if(heap != NULL)
{
inOrder(heap->left);
cout<< heap->key << " " ;
inOrder(heap->right);
}
}
template <class T>
void SkewHeap<T>::inOrder()
{
inOrder(mRoot);
}
/*
* 后序遍历"斜堆"
*/
template <class T>
void SkewHeap<T>::postOrder(SkewNode<T>* heap) const
{
if(heap != NULL)
{
postOrder(heap->left);
postOrder(heap->right);
cout<< heap->key << " " ;
}
}
template <class T>
void SkewHeap<T>::postOrder()
{
postOrder(mRoot);
}
/*
* 交换两个节点的内容
*/
template <class T>
void SkewHeap<T>::swapNode(SkewNode<T> *&x, SkewNode<T> *&y)
{
SkewNode<T> *tmp = x;
x = y;
y = tmp;
}
/*
* 合并"斜堆x"和"斜堆y"
*/
template <class T>
SkewNode<T>* SkewHeap<T>::merge(SkewNode<T>* &x, SkewNode<T>* &y)
{
if(x == NULL)
return y;
if(y == NULL)
return x;
// 合并x和y时,将x作为合并后的树的根;
// 这里的操作是保证: x的key < y的key
if(x->key > y->key)
swapNode(x, y);
// 将x的右孩子和y合并,
// 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
SkewNode<T> *tmp = merge(x->right, y);
x->right = x->left;
x->left = tmp;
return x;
}
/*
* 将other的斜堆合并到this中。
*/
template <class T>
void SkewHeap<T>::merge(SkewHeap<T>* other)
{
mRoot = merge(mRoot, other->mRoot);
}
/*
* 新建键值为key的结点并将其插入到斜堆中
*
* 参数说明:
* heap 斜堆的根结点
* key 插入的结点的键值
* 返回值:
* 根节点
*/
template <class T>
void SkewHeap<T>::insert(T key)
{
SkewNode<T> *node; // 新建结点
// 新建节点
node = new SkewNode<T>(key, NULL, NULL);
if (node==NULL)
{
cout << "ERROR: create node failed!" << endl;
return ;
}
mRoot = merge(mRoot, node);
}
/*
* 删除结点
*/
template <class T>
void SkewHeap<T>::remove()
{
if (mRoot == NULL)
return NULL;
SkewNode<T> *l = mRoot->left;
SkewNode<T> *r = mRoot->right;
// 删除根节点
delete mRoot;
// 左右子树合并后的新树
mRoot = merge(l, r);
}
/*
* 销毁斜堆
*/
template <class T>
void SkewHeap<T>::destroy(SkewNode<T>* &heap)
{
if (heap==NULL)
return ;
if (heap->left != NULL)
destroy(heap->left);
if (heap->right != NULL)
destroy(heap->right);
delete heap;
}
template <class T>
void SkewHeap<T>::destroy()
{
destroy(mRoot);
}
/*
* 打印"斜堆"
*
* key -- 节点的键值
* direction -- 0,表示该节点是根节点;
* -1,表示该节点是它的父结点的左孩子;
* 1,表示该节点是它的父结点的右孩子。
*/
template <class T>
void SkewHeap<T>::print(SkewNode<T>* heap, T key, int direction)
{
if(heap != NULL)
{
if(direction==0) // heap是根节点
cout << setw(2) << heap->key << " is root" << endl;
else // heap是分支节点
cout << setw(2) << heap->key << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") << endl;
print(heap->left, heap->key, -1);
print(heap->right,heap->key, 1);
}
}
template <class T>
void SkewHeap<T>::print()
{
if (mRoot != NULL)
print(mRoot, mRoot->key, 0);
}
#endif
斜堆的测试程序(SkewHeapTest.cpp)
/**
* C 语言: 斜堆
*
* @author skywang
* @date 2014/03/31
*/
#include <iostream>
#include "SkewHeap.h"
using namespace std;
int main()
{
int i;
int a[]= {10,40,24,30,36,20,12,16};
int b[]= {17,13,11,15,19,21,23};
int alen=sizeof(a)/sizeof(a[0]);
int blen=sizeof(b)/sizeof(b[0]);
SkewHeap<int>* ha=new SkewHeap<int>();
SkewHeap<int>* hb=new SkewHeap<int>();
cout << "== 斜堆(ha)中依次添加: ";
for(i=0; i<alen; i++)
{
cout << a[i] <<" ";
ha->insert(a[i]);
}
cout << "\n== 斜堆(ha)的详细信息: " << endl;
ha->print();
cout << "\n== 斜堆(hb)中依次添加: ";
for(i=0; i<blen; i++)
{
cout << b[i] <<" ";
hb->insert(b[i]);
}
cout << "\n== 斜堆(hb)的详细信息: " << endl;
hb->print();
// 将"斜堆hb"合并到"斜堆ha"中。
ha->merge(hb);
cout << "\n== 合并ha和hb后的详细信息: " << endl;
ha->print();
// 销毁
ha->destroy();
return 0;
}
斜堆的C++测试程序
斜堆的测试程序已经包含在它的实现文件(SkewHeapTest.cpp)中了,这里仅给出它的运行结果:
== 斜堆(ha)中依次添加: 10 40 24 30 36 20 12 16 == 斜堆(ha)的详细信息: 10 is root 16 is 10's left child 20 is 16's left child 30 is 20's left child 40 is 30's left child 12 is 10's right child 24 is 12's left child 36 is 24's left child == 斜堆(hb)中依次添加: 17 13 11 15 19 21 23 == 斜堆(hb)的详细信息: 11 is root 13 is 11's left child 17 is 13's left child 23 is 17's left child 19 is 13's right child 15 is 11's right child 21 is 15's left child == 合并ha和hb后的详细信息: 10 is root 11 is 10's left child 12 is 11's left child 15 is 12's left child 21 is 15's left child 24 is 12's right child 36 is 24's left child 13 is 11's right child 17 is 13's left child 23 is 17's left child 19 is 13's right child 16 is 10's right child 20 is 16's left child 30 is 20's left child 40 is 30's left child
3.3 斜堆的Java实现(完整源码)
斜堆的实现文件(SkewHeap.java)
/**
* Java 语言: 斜堆
*
* @author skywang
* @date 2014/03/31
*/
public class SkewHeap<T extends Comparable<T>> {
private SkewNode<T> mRoot; // 根结点
private class SkewNode<T extends Comparable<T>> {
T key; // 关键字(键值)
SkewNode<T> left; // 左孩子
SkewNode<T> right; // 右孩子
public SkewNode(T key, SkewNode<T> left, SkewNode<T> right) {
this.key = key;
this.left = left;
this.right = right;
}
public String toString() {
return "key:"+key;
}
}
public SkewHeap() {
mRoot = null;
}
/*
* 前序遍历"斜堆"
*/
private void preOrder(SkewNode<T> heap) {
if(heap != null) {
System.out.print(heap.key+" ");
preOrder(heap.left);
preOrder(heap.right);
}
}
public void preOrder() {
preOrder(mRoot);
}
/*
* 中序遍历"斜堆"
*/
private void inOrder(SkewNode<T> heap) {
if(heap != null) {
inOrder(heap.left);
System.out.print(heap.key+" ");
inOrder(heap.right);
}
}
public void inOrder() {
inOrder(mRoot);
}
/*
* 后序遍历"斜堆"
*/
private void postOrder(SkewNode<T> heap) {
if(heap != null)
{
postOrder(heap.left);
postOrder(heap.right);
System.out.print(heap.key+" ");
}
}
public void postOrder() {
postOrder(mRoot);
}
/*
* 合并"斜堆x"和"斜堆y"
*/
private SkewNode<T> merge(SkewNode<T> x, SkewNode<T> y) {
if(x == null) return y;
if(y == null) return x;
// 合并x和y时,将x作为合并后的树的根;
// 这里的操作是保证: x的key < y的key
if(x.key.compareTo(y.key) > 0) {
SkewNode<T> tmp = x;
x = y;
y = tmp;
}
// 将x的右孩子和y合并,
// 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
SkewNode<T> tmp = merge(x.right, y);
x.right = x.left;
x.left = tmp;
return x;
}
public void merge(SkewHeap<T> other) {
this.mRoot = merge(this.mRoot, other.mRoot);
}
/*
* 新建结点(key),并将其插入到斜堆中
*
* 参数说明:
* key 插入结点的键值
*/
public void insert(T key) {
SkewNode<T> node = new SkewNode<T>(key,null,null);
// 如果新建结点失败,则返回。
if (node != null)
this.mRoot = merge(this.mRoot, node);
}
/*
* 删除根结点
*
* 返回值:
* 返回被删除的节点的键值
*/
public T remove() {
if (this.mRoot == null)
return null;
T key = this.mRoot.key;
SkewNode<T> l = this.mRoot.left;
SkewNode<T> r = this.mRoot.right;
this.mRoot = null; // 删除根节点
this.mRoot = merge(l, r); // 合并左右子树
return key;
}
/*
* 销毁斜堆
*/
private void destroy(SkewNode<T> heap) {
if (heap==null)
return ;
if (heap.left != null)
destroy(heap.left);
if (heap.right != null)
destroy(heap.right);
heap=null;
}
public void clear() {
destroy(mRoot);
mRoot = null;
}
/*
* 打印"斜堆"
*
* key -- 节点的键值
* direction -- 0,表示该节点是根节点;
* -1,表示该节点是它的父结点的左孩子;
* 1,表示该节点是它的父结点的右孩子。
*/
private void print(SkewNode<T> heap, T key, int direction) {
if(heap != null) {
if(direction==0) // heap是根节点
System.out.printf("%2d is root\n", heap.key);
else // heap是分支节点
System.out.printf("%2d is %2d's %6s child\n", heap.key, key, direction==1?"right" : "left");
print(heap.left, heap.key, -1);
print(heap.right,heap.key, 1);
}
}
public void print() {
if (mRoot != null)
print(mRoot, mRoot.key, 0);
}
}
斜堆的测试程序(SkewHeapTest.java)
/**
* Java 语言: 斜堆
*
* @author skywang
* @date 2014/03/31
*/
public class SkewHeapTest {
public static void main(String[] args) {
int a[]= {10,40,24,30,36,20,12,16};
int b[]= {17,13,11,15,19,21,23};
SkewHeap<Integer> ha=new SkewHeap<Integer>();
SkewHeap<Integer> hb=new SkewHeap<Integer>();
System.out.printf("== 斜堆(ha)中依次添加: ");
for(int i=0; i<a.length; i++) {
System.out.printf("%d ", a[i]);
ha.insert(a[i]);
}
System.out.printf("\n== 斜堆(ha)的详细信息: \n");
ha.print();
System.out.printf("\n== 斜堆(hb)中依次添加: ");
for(int i=0; i<b.length; i++) {
System.out.printf("%d ", b[i]);
hb.insert(b[i]);
}
System.out.printf("\n== 斜堆(hb)的详细信息: \n");
hb.print();
// 将"斜堆hb"合并到"斜堆ha"中。
ha.merge(hb);
System.out.printf("\n== 合并ha和hb后的详细信息: \n");
ha.print();
}
}
斜堆的Java测试程序
斜堆的测试程序已经包含在它的实现文件(SkewHeapTest.java)中了,这里仅给出它的运行结果:
== 斜堆(ha)中依次添加: 10 40 24 30 36 20 12 16 == 斜堆(ha)的详细信息: 10 is root 16 is 10's left child 20 is 16's left child 30 is 20's left child 40 is 30's left child 12 is 10's right child 24 is 12's left child 36 is 24's left child == 斜堆(hb)中依次添加: 17 13 11 15 19 21 23 == 斜堆(hb)的详细信息: 11 is root 13 is 11's left child 17 is 13's left child 23 is 17's left child 19 is 13's right child 15 is 11's right child 21 is 15's left child == 合并ha和hb后的详细信息: 10 is root 11 is 10's left child 12 is 11's left child 15 is 12's left child 21 is 15's left child 24 is 12's right child 36 is 24's left child 13 is 11's right child 17 is 13's left child 23 is 17's left child 19 is 13's right child 16 is 10's right child 20 is 16's left child 30 is 20's left child 40 is 30's left child