zoukankan      html  css  js  c++  java
  • 左倾堆(二)之 C++的实现

    概要

    上一章介绍了左倾堆的基本概念,并通过C语言实现了左倾堆。本章是左倾堆的C++实现。

    目录
    1. 左倾堆的介绍
    2. 左倾堆的图文解析
    3. 左倾堆的C++实现(完整源码)
    4. 左倾堆的C++测试程序

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3638342.html


    更多内容:数据结构与算法系列 目录

    (01) 左倾堆(一)之 图文解析 和 C语言的实现
    (02) 左倾堆(二)之 C++的实现
    (03) 左倾堆(三)之 Java的实现

    左倾堆的介绍

    左倾堆(leftist tree 或 leftist heap),又被成为左偏树、左偏堆,最左堆等。
    它和二叉堆一样,都是优先队列实现方式。当优先队列中涉及到"对两个优先队列进行合并"的问题时,二叉堆的效率就无法令人满意了,而本文介绍的左倾堆,则可以很好地解决这类问题。

    左倾堆的定义

    上图是一颗左倾树,它的节点除了和二叉树的节点一样具有左右子树指针外,还有两个属性:键值和零距离。
    (01) 键值的作用是来比较节点的大小,从而对节点进行排序。
    (02) 零距离(英文名NPL,即Null Path Length)则是从一个节点到一个"最近的不满节点"的路径长度。不满节点是指该该节点的左右孩子至少有有一个为NULL。叶节点的NPL为0,NULL节点的NPL为-1。

    左倾堆有以下几个基本性质:
    [性质1] 节点的键值小于或等于它的左右子节点的键值。
    [性质2] 节点的左孩子的NPL >= 右孩子的NPL。
    [性质3] 节点的NPL = 它的右孩子的NPL + 1。

    左倾堆的图文解析

    合并操作是左倾堆的重点。合并两个左倾堆的基本思想如下:
    (01) 如果一个空左倾堆与一个非空左倾堆合并,返回非空左倾堆。
    (02) 如果两个左倾堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
    (03) 如果新堆的右孩子的NPL > 左孩子的NPL,则交换左右孩子。
    (04) 设置新堆的根节点的NPL = 右子堆NPL + 1

     

    下面通过图文演示合并以下两个堆的过程。

    提示这两个堆的合并过程和测试程序相对应!

    第1步:将"较小堆(根为10)的右孩子"和"较大堆(根为11)"进行合并。
    合并的结果,相当于将"较大堆"设置"较小堆"的右孩子,如下图所示:

     

    第2步:将上一步得到的"根11的右子树"和"根为12的树"进行合并,得到的结果如下:

     

    第3步:将上一步得到的"根12的右子树"和"根为13的树"进行合并,得到的结果如下:

     

    第4步:将上一步得到的"根13的右子树"和"根为16的树"进行合并,得到的结果如下:

     

    第5步:将上一步得到的"根16的右子树"和"根为23的树"进行合并,得到的结果如下:

    至此,已经成功的将两棵树合并成为一棵树了。接下来,对新生成的树进行调节。

     

    第6步:上一步得到的"树16的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

     

    第7步:上一步得到的"树12的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

     

    第8步:上一步得到的"树10的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

    至此,合并完毕。上面就是合并得到的左倾堆!


    下面看看左倾堆的基本操作的代码

    1. 基本定义

    template <class T>
    class LeftistNode{
        public:
            T key;                // 关键字(键值)
            int npl;            // 零路经长度(Null Path Length)
            LeftistNode *left;    // 左孩子
            LeftistNode *right;    // 右孩子
    
            LeftistNode(T value, LeftistNode *l, LeftistNode *r):
                key(value), npl(0), left(l),right(r) {}
    };

    LeftistNode是左倾堆对应的节点类。

    template <class T>
    class LeftistHeap {
        private:
            LeftistNode<T> *mRoot;    // 根结点
    
        public:
            LeftistHeap();
            ~LeftistHeap();
    
            // 前序遍历"左倾堆"
            void preOrder();
            // 中序遍历"左倾堆"
            void inOrder();
            // 后序遍历"左倾堆"
            void postOrder();
    
             // 将other的左倾堆合并到this中。
            void merge(LeftistHeap<T>* other);
            // 将结点(key为节点键值)插入到左倾堆中
            void insert(T key);
            // 删除结点(key为节点键值)
            void remove();
    
            // 销毁左倾堆
            void destroy();
    
            // 打印左倾堆
            void print();
        private:
    
            // 前序遍历"左倾堆"
            void preOrder(LeftistNode<T>* heap) const;
            // 中序遍历"左倾堆"
            void inOrder(LeftistNode<T>* heap) const;
            // 后序遍历"左倾堆"
            void postOrder(LeftistNode<T>* heap) const;
    
            // 交换节点x和节点y
            void swapNode(LeftistNode<T> *&x, LeftistNode<T> *&y);
            // 合并"左倾堆x"和"左倾堆y"
            LeftistNode<T>* merge(LeftistNode<T>* &x, LeftistNode<T>* &y);
            // 将结点(z)插入到左倾堆(heap)中
            LeftistNode<T>* insert(LeftistNode<T>* &heap, T key);
            // 删除左倾堆(heap)中的结点(z),并返回被删除的结点
            LeftistNode<T>* remove(LeftistNode<T>* &heap);
    
            // 销毁左倾堆
            void destroy(LeftistNode<T>* &heap);
    
            // 打印左倾堆
            void print(LeftistNode<T>* heap, T key, int direction);
    };

    LeftistHeap是左倾堆类,它包含了左倾堆的根节点,以及左倾堆的操作。

    2. 合并

    /*
     * 合并"左倾堆x"和"左倾堆y"
     */
    template <class T>
    LeftistNode<T>* LeftistHeap<T>::merge(LeftistNode<T>* &x, LeftistNode<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的右孩子。
        x->right = merge(x->right, y);
    
        // 如果"x的左孩子为空" 或者 "x的左孩子的npl<右孩子的npl"
        // 则,交换x和y
        if(x->left == NULL || x->left->npl < x->right->npl)
        {
            LeftistNode<T> *tmp = x->left;
            x->left = x->right;
            x->right = tmp;
        }
        // 设置合并后的新树(x)的npl
        if (x->right == NULL || x->left == NULL)
            x->npl = 0;
        else
            x->npl = (x->left->npl > x->right->npl) ? (x->right->npl + 1) : (x->left->npl + 1);
    
        return x;
    }
    
    /*
     * 将other的左倾堆合并到this中。
     */
    template <class T>
    void LeftistHeap<T>::merge(LeftistHeap<T>* other)
    {
        mRoot = merge(mRoot, other->mRoot);
    }

    merge(x, y)是内部接口,作用是合并x和y这两个左倾堆,并返回得到的新堆的根节点。
    merge(other)是外部接口,作用是将other合并到当前堆中。


    3. 添加

    /* 
     * 将结点插入到左倾堆中,并返回根节点
     *
     * 参数说明:
     *     heap 左倾堆的根结点
     *     key 插入的结点的键值
     * 返回值:
     *     根节点
     */
    template <class T>
    LeftistNode<T>* LeftistHeap<T>::insert(LeftistNode<T>* &heap, T key)
    {
        LeftistNode<T> *node;    // 新建结点
    
        // 新建节点
        node = new LeftistNode<T>(key, NULL, NULL);
        if (node==NULL)
        {
            cout << "ERROR: create node failed!" << endl;
            return heap;
        }
    
        return merge(mRoot, node);
    }
    
    template <class T>
    void LeftistHeap<T>::insert(T key)
    {
        mRoot = insert(mRoot, key);
    }

    insert(heap, key)是内部接口,它是以节点为操作对象的。
    insert(key)是外部接口,它的作用是新建键值为key的节点,并将其加入到当前左倾堆中。

    4. 删除

    /* 
     * 删除结点,返回根节点
     *
     * 参数说明:
     *     heap 左倾堆的根结点
     * 返回值:
     *     根节点
     */
    template <class T>
    LeftistNode<T>* LeftistHeap<T>::remove(LeftistNode<T>* &heap)
    {
        if (heap == NULL)
            return NULL;
    
        LeftistNode<T> *l = heap->left;
        LeftistNode<T> *r = heap->right;
    
        // 删除根节点
        delete heap;
    
        return merge(l, r); // 返回左右子树合并后的新树
    }
    
    template <class T>
    void LeftistHeap<T>::remove()
    {
        mRoot = remove(mRoot);
    }

    remove(heap)是内部接口,它是以节点为操作对象的。
    remove()是外部接口,它的作用是删除左倾堆的最小节点。

    注意关于左倾堆的"前序遍历"、"中序遍历"、"后序遍历"、"打印"、"销毁"等接口就不再单独介绍了。后文的源码中有给出它们的实现代码,Please RTFSC(Read The Fucking Source Code)!

    左倾堆的C++实现(完整源码)

    左倾堆的实现文件(LeftistHeap.h)

      1 /**
      2  * C++: 左倾堆
      3  *
      4  * @author skywang
      5  * @date 2014/03/31
      6  */
      7 
      8 #ifndef _LEFTIST_TREE_HPP_
      9 #define _LEFTIST_TREE_HPP_
     10 
     11 #include <iomanip>
     12 #include <iostream>
     13 using namespace std;
     14 
     15 template <class T>
     16 class LeftistNode{
     17     public:
     18         T key;                // 关键字(键值)
     19         int npl;            // 零路经长度(Null Path Length)
     20         LeftistNode *left;    // 左孩子
     21         LeftistNode *right;    // 右孩子
     22 
     23         LeftistNode(T value, LeftistNode *l, LeftistNode *r):
     24             key(value), npl(0), left(l),right(r) {}
     25 };
     26 
     27 template <class T>
     28 class LeftistHeap {
     29     private:
     30         LeftistNode<T> *mRoot;    // 根结点
     31 
     32     public:
     33         LeftistHeap();
     34         ~LeftistHeap();
     35 
     36         // 前序遍历"左倾堆"
     37         void preOrder();
     38         // 中序遍历"左倾堆"
     39         void inOrder();
     40         // 后序遍历"左倾堆"
     41         void postOrder();
     42 
     43          // 将other的左倾堆合并到this中。
     44         void merge(LeftistHeap<T>* other);
     45         // 将结点(key为节点键值)插入到左倾堆中
     46         void insert(T key);
     47         // 删除结点(key为节点键值)
     48         void remove();
     49 
     50         // 销毁左倾堆
     51         void destroy();
     52 
     53         // 打印左倾堆
     54         void print();
     55     private:
     56 
     57         // 前序遍历"左倾堆"
     58         void preOrder(LeftistNode<T>* heap) const;
     59         // 中序遍历"左倾堆"
     60         void inOrder(LeftistNode<T>* heap) const;
     61         // 后序遍历"左倾堆"
     62         void postOrder(LeftistNode<T>* heap) const;
     63 
     64         // 交换节点x和节点y
     65         void swapNode(LeftistNode<T> *&x, LeftistNode<T> *&y);
     66         // 合并"左倾堆x"和"左倾堆y"
     67         LeftistNode<T>* merge(LeftistNode<T>* &x, LeftistNode<T>* &y);
     68         // 将结点(z)插入到左倾堆(heap)中
     69         LeftistNode<T>* insert(LeftistNode<T>* &heap, T key);
     70         // 删除左倾堆(heap)中的结点(z),并返回被删除的结点
     71         LeftistNode<T>* remove(LeftistNode<T>* &heap);
     72 
     73         // 销毁左倾堆
     74         void destroy(LeftistNode<T>* &heap);
     75 
     76         // 打印左倾堆
     77         void print(LeftistNode<T>* heap, T key, int direction);
     78 };
     79 
     80 /* 
     81  * 构造函数
     82  */
     83 template <class T>
     84 LeftistHeap<T>::LeftistHeap():mRoot(NULL)
     85 {
     86 }
     87 
     88 /* 
     89  * 析构函数
     90  */
     91 template <class T>
     92 LeftistHeap<T>::~LeftistHeap() 
     93 {
     94     destroy(mRoot);
     95 }
     96 
     97 /*
     98  * 前序遍历"左倾堆"
     99  */
    100 template <class T>
    101 void LeftistHeap<T>::preOrder(LeftistNode<T>* heap) const
    102 {
    103     if(heap != NULL)
    104     {
    105         cout<< heap->key << " " ;
    106         preOrder(heap->left);
    107         preOrder(heap->right);
    108     }
    109 }
    110 
    111 template <class T>
    112 void LeftistHeap<T>::preOrder() 
    113 {
    114     preOrder(mRoot);
    115 }
    116 
    117 /*
    118  * 中序遍历"左倾堆"
    119  */
    120 template <class T>
    121 void LeftistHeap<T>::inOrder(LeftistNode<T>* heap) const
    122 {
    123     if(heap != NULL)
    124     {
    125         inOrder(heap->left);
    126         cout<< heap->key << " " ;
    127         inOrder(heap->right);
    128     }
    129 }
    130 
    131 template <class T>
    132 void LeftistHeap<T>::inOrder() 
    133 {
    134     inOrder(mRoot);
    135 }
    136 
    137 /*
    138  * 后序遍历"左倾堆"
    139  */
    140 template <class T>
    141 void LeftistHeap<T>::postOrder(LeftistNode<T>* heap) const
    142 {
    143     if(heap != NULL)
    144     {
    145         postOrder(heap->left);
    146         postOrder(heap->right);
    147         cout<< heap->key << " " ;
    148     }
    149 }
    150 
    151 template <class T>
    152 void LeftistHeap<T>::postOrder() 
    153 {
    154     postOrder(mRoot);
    155 }
    156 
    157 /*
    158  * 交换两个节点的内容
    159  */
    160 template <class T>
    161 void LeftistHeap<T>::swapNode(LeftistNode<T> *&x, LeftistNode<T> *&y)
    162 {
    163     LeftistNode<T> *tmp = x;
    164     x = y;
    165     y = tmp;
    166 }
    167 
    168 
    169 /*
    170  * 合并"左倾堆x"和"左倾堆y"
    171  */
    172 template <class T>
    173 LeftistNode<T>* LeftistHeap<T>::merge(LeftistNode<T>* &x, LeftistNode<T>* &y)
    174 {
    175     if(x == NULL)
    176         return y;
    177     if(y == NULL)
    178         return x;
    179 
    180     // 合并x和y时,将x作为合并后的树的根;
    181     // 这里的操作是保证: x的key < y的key
    182     if(x->key > y->key)
    183         swapNode(x, y);
    184 
    185     // 将x的右孩子和y合并,"合并后的树的根"是x的右孩子。
    186     x->right = merge(x->right, y);
    187 
    188     // 如果"x的左孩子为空" 或者 "x的左孩子的npl<右孩子的npl"
    189     // 则,交换x和y
    190     if(x->left == NULL || x->left->npl < x->right->npl)
    191     {
    192         LeftistNode<T> *tmp = x->left;
    193         x->left = x->right;
    194         x->right = tmp;
    195     }
    196     // 设置合并后的新树(x)的npl
    197     if (x->right == NULL || x->left == NULL)
    198         x->npl = 0;
    199     else
    200         x->npl = (x->left->npl > x->right->npl) ? (x->right->npl + 1) : (x->left->npl + 1);
    201 
    202     return x;
    203 }
    204 
    205 /*
    206  * 将other的左倾堆合并到this中。
    207  */
    208 template <class T>
    209 void LeftistHeap<T>::merge(LeftistHeap<T>* other)
    210 {
    211     mRoot = merge(mRoot, other->mRoot);
    212 }
    213 
    214 /* 
    215  * 将结点插入到左倾堆中,并返回根节点
    216  *
    217  * 参数说明:
    218  *     heap 左倾堆的根结点
    219  *     key 插入的结点的键值
    220  * 返回值:
    221  *     根节点
    222  */
    223 template <class T>
    224 LeftistNode<T>* LeftistHeap<T>::insert(LeftistNode<T>* &heap, T key)
    225 {
    226     LeftistNode<T> *node;    // 新建结点
    227 
    228     // 新建节点
    229     node = new LeftistNode<T>(key, NULL, NULL);
    230     if (node==NULL)
    231     {
    232         cout << "ERROR: create node failed!" << endl;
    233         return heap;
    234     }
    235 
    236     return merge(mRoot, node);
    237 }
    238 
    239 template <class T>
    240 void LeftistHeap<T>::insert(T key)
    241 {
    242     mRoot = insert(mRoot, key);
    243 }
    244 
    245 /* 
    246  * 删除结点,返回根节点
    247  *
    248  * 参数说明:
    249  *     heap 左倾堆的根结点
    250  * 返回值:
    251  *     根节点
    252  */
    253 template <class T>
    254 LeftistNode<T>* LeftistHeap<T>::remove(LeftistNode<T>* &heap)
    255 {
    256     if (heap == NULL)
    257         return NULL;
    258 
    259     LeftistNode<T> *l = heap->left;
    260     LeftistNode<T> *r = heap->right;
    261 
    262     // 删除根节点
    263     delete heap;
    264 
    265     return merge(l, r); // 返回左右子树合并后的新树
    266 }
    267 
    268 template <class T>
    269 void LeftistHeap<T>::remove()
    270 {
    271     mRoot = remove(mRoot);
    272 }
    273 
    274 /* 
    275  * 销毁左倾堆
    276  */
    277 template <class T>
    278 void LeftistHeap<T>::destroy(LeftistNode<T>* &heap)
    279 {
    280     if (heap==NULL)
    281         return ;
    282 
    283     if (heap->left != NULL)
    284         destroy(heap->left);
    285     if (heap->right != NULL)
    286         destroy(heap->right);
    287 
    288     delete heap;
    289 }
    290 
    291 template <class T>
    292 void LeftistHeap<T>::destroy()
    293 {
    294     destroy(mRoot);
    295 }
    296 
    297 /*
    298  * 打印"二叉查找树"
    299  *
    300  * key        -- 节点的键值 
    301  * direction  --  0,表示该节点是根节点;
    302  *               -1,表示该节点是它的父结点的左孩子;
    303  *                1,表示该节点是它的父结点的右孩子。
    304  */
    305 template <class T>
    306 void LeftistHeap<T>::print(LeftistNode<T>* heap, T key, int direction)
    307 {
    308     if(heap != NULL)
    309     {
    310         if(direction==0)    // heap是根节点
    311             cout << setw(2) << heap->key << "(" << heap->npl << ") is root" << endl;
    312         else                // heap是分支节点
    313             cout << setw(2) << heap->key << "(" << heap->npl << ") is " << setw(2) << key << "'s "  << setw(12) << (direction==1?"right child" : "left child") << endl;
    314 
    315         print(heap->left, heap->key, -1);
    316         print(heap->right,heap->key,  1);
    317     }
    318 }
    319 
    320 template <class T>
    321 void LeftistHeap<T>::print()
    322 {
    323     if (mRoot != NULL)
    324         print(mRoot, mRoot->key, 0);
    325 }
    326 #endif
    View Code

    左倾堆的测试程序(LeftistHeapTest.cpp)

     1 /**
     2  * C 语言: 左倾堆
     3  *
     4  * @author skywang
     5  * @date 2013/11/07
     6  */
     7 
     8 #include <iostream>
     9 #include "LeftistHeap.h"
    10 using namespace std;
    11 
    12 int main()
    13 {
    14     int i;
    15     int a[]= {10,40,24,30,36,20,12,16};
    16     int b[]= {17,13,11,15,19,21,23};
    17     int alen=sizeof(a)/sizeof(a[0]);
    18     int blen=sizeof(b)/sizeof(b[0]);
    19     LeftistHeap<int>* ha=new LeftistHeap<int>();
    20     LeftistHeap<int>* hb=new LeftistHeap<int>();
    21 
    22     cout << "== 左倾堆(ha)中依次添加: ";
    23     for(i=0; i<alen; i++)
    24     {
    25         cout << a[i] <<" ";
    26         ha->insert(a[i]);
    27     }
    28     cout << "
    == 左倾堆(ha)的详细信息: " << endl;
    29     ha->print();
    30 
    31 
    32     cout << "
    == 左倾堆(hb)中依次添加: ";
    33     for(i=0; i<blen; i++)
    34     {
    35         cout << b[i] <<" ";
    36         hb->insert(b[i]);
    37     }
    38     cout << "
    == 左倾堆(hb)的详细信息: " << endl;
    39     hb->print();
    40 
    41 
    42     // 将"左倾堆hb"合并到"左倾堆ha"中。
    43     ha->merge(hb);
    44     cout << "
    == 合并ha和hb后的详细信息: " << endl;
    45     ha->print();
    46 
    47 
    48     // 销毁
    49     ha->destroy();
    50 
    51     return 0;
    52 }
    View Code

    左倾堆的C++测试程序

    左倾堆的测试程序已经包含在它的实现文件(LeftistHeapTest.cpp)中了,这里仅给出它的运行结果:

    == 左倾堆(ha)中依次添加: 10 40 24 30 36 20 12 16 
    == 左倾堆(ha)的详细信息: 
    10(2) is root
    24(1) is 10's   left child
    30(0) is 24's   left child
    36(0) is 24's  right child
    12(1) is 10's  right child
    20(0) is 12's   left child
    40(0) is 20's   left child
    16(0) is 12's  right child
    
    == 左倾堆(hb)中依次添加: 17 13 11 15 19 21 23 
    == 左倾堆(hb)的详细信息: 
    11(2) is root
    15(1) is 11's   left child
    19(0) is 15's   left child
    21(0) is 15's  right child
    13(1) is 11's  right child
    17(0) is 13's   left child
    23(0) is 13's  right child
    
    == 合并ha和hb后的详细信息: 
    10(2) is root
    11(2) is 10's   left child
    15(1) is 11's   left child
    19(0) is 15's   left child
    21(0) is 15's  right child
    12(1) is 11's  right child
    13(1) is 12's   left child
    17(0) is 13's   left child
    16(0) is 13's  right child
    23(0) is 16's   left child
    20(0) is 12's  right child
    40(0) is 20's   left child
    24(1) is 10's  right child
    30(0) is 24's   left child
    36(0) is 24's  right child
  • 相关阅读:
    MVC<2:路由映射原理2>
    分支限界>0/1背包 小强斋
    分支限界>装载问题 小强斋
    解空间树搜索 及 最优解 小强斋
    算法>NP难度和NP完全的问题 小强斋
    算法>NP难度和NP完全的问题 小强斋
    分支限界>装载问题 小强斋
    分支限界>0/1背包 小强斋
    分支限界>15谜问题 小强斋
    分支限界>15谜问题 小强斋
  • 原文地址:https://www.cnblogs.com/skywang12345/p/3638342.html
Copyright © 2011-2022 走看看