Data Structures (Weiss) Chapter 6: Leftist Heap

本文介绍了一种特殊的二叉堆——左偏堆的基本概念、关键属性及其在C++中的具体实现方法。左偏堆是一种自平衡的二叉堆结构,能够高效地支持合并操作。文章详细展示了左偏堆中节点的定义、主要操作如插入、删除最小元素及合并等,并通过实例代码演示了其使用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Definition of null path length, npl(X), of any node X to be the length of the shortest path from X to a node without two children. Thus, the npl of a node with zero or one child is 0, while npl(NULL)=-1.


The leftist heap is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child. The key of parent is always no more than the key of its children. The leftist heap is very unbalanced. 


//

//  main.cpp

//  Data Structure TRY1

//

//  Created by zr9558 on 6/7/13.

//  Copyright (c) 2013 zr9558. All rights reserved.

//



// Data Structure C++, Weiss, P.229 Section 6.6 Leftist Heaps

#include<iostream>

using namespace std;



template<typename Comparable>

class LeftistHeap

{

public:

    LeftistHeap() {root=NULL;}

    LeftistHeap(constLeftistHeap &rhs)

    {

       root=NULL;

       operator=(rhs);

    }

    ~LeftistHeap()

    {

       makeEmpty();

    }

    

   bool isEmpty()const

    {

       returnroot==NULL;

    }

    

   const Comparable &findMin()const

    {

       returnroot->element;

    }

    

   void insert(const Comparable &x) ;

   void deleteMin();

   void deleteMin( Comparable & minItem);

   void makeEmpty();

   void merge(LeftistHeap &rhs);

    

    constLeftistHeap &operator =(constLeftistHeap &rhs)

    {

       if(this != &rhs)

        {

           makeEmpty();

           root=clone(rhs.root);

        }

       return *this;

    }

    

private:

   struct LeftistNode

    {

        Comparable element;

       LeftistNode *left;

       LeftistNode *right;

       int npl;

        

        LeftistNode(const Comparable & theElement,LeftistNode *lt=NULL,

                   LeftistNode *rt=NULL,int np=0)

        :element(theElement),left(lt), right(rt),npl(np){}

    };

    

   LeftistNode *root;

    

    LeftistNode *merge(LeftistNode *h1,LeftistNode *h2)

    {

       if( h1==NULL)return h2;

       if( h2==NULL)return h1;

       if( h1->element<h2->element)returnmerge1(h1,h2);

       elsereturnmerge1(h2,h1);

    }

    

    LeftistNode *merge1(LeftistNode *h1,LeftistNode *h2)

    {

       if( h1->left==NULL)//Single Node

            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;

    }

    

   void swapChildren(LeftistNode *t)

    {

       LeftistNode *temp=t->left;

        t->left=t->right;

        t->right=temp;

        

        

    }

    

   void reclaimMemory(LeftistNode *t);

    

    LeftistNode *clone(LeftistNode *t)const

    {

       if( t==NULL)returnNULL;

        

       returnnew LeftistNode(t->element,clone(t->left),clone(t->right),t->npl);

    }

    

};



template<typename Comparable>

void LeftistHeap<Comparable>::insert(const Comparable &x)

{

   root = merge(newLeftistNode(x),root);

}


template<typename Comparable>

void LeftistHeap<Comparable>::deleteMin()

{

   LeftistNode *oldRoot=root;

    root=merge(root->left,root->right);

   delete oldRoot;

}


template<typename Comparable>

void LeftistHeap<Comparable>::deleteMin( Comparable & minItem)

{

    minItem=findMin();

    deleteMin();

}


template<typename Comparable>

void LeftistHeap<Comparable>::merge(LeftistHeap &rhs)

{

   if(this==&rhs)return;

    

   root=merge(root,rhs.root);

    rhs.root=NULL;

}


template<typename Comparable>

void LeftistHeap<Comparable>::makeEmpty()

{

   while( !isEmpty())

        deleteMin();

}




int main()

{

    LeftistHeap<int> H;

    

   for(int i=4; i<20; ++i)

        H.insert(i%11);

    

    LeftistHeap<int> H1;

    H1=H;

    

   while( !H.isEmpty())

    {

       cout<<H.findMin()<<" ";

        H.deleteMin();

    }

    cout<<endl;

    

   while( !H1.isEmpty())

    {

       cout<<H1.findMin()<<" ";

        H1.deleteMin();

    }

    cout<<endl;

    

    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值