📜  左派树/左派堆

📅  最后修改于: 2021-04-17 11:58:39             🧑  作者: Mango

左派树或左派堆是使用二进制堆的变体实现的优先级队列。每个节点都有一个s值(或等级或距离) ,它是到最近的叶子的距离。与二叉堆(始终是完整的二叉树)相反,左派树可能非常不平衡。

以下是左派树/堆的时间复杂性。

Function       Complexity              Comparison
1) Get Min:       O(1)      [same as both Binary and Binomial]
2) Delete Min:    O(Log n)  [same as both Binary and Binomial]
3) Insert:        O(Log n)  [O(Log n) in Binary and O(1) in 
                            Binomial and O(Log n) for worst case]                                                                  
4) Merge:         O(Log n)  [O(Log n) in Binomial]

左派树是具有属性的二叉树:

  1. 普通最小堆属性: key(i)> = key(parent(i))
  2. 左侧较重: dist(right(i))<= dist(left(i))。在这里,dist(i)是扩展二叉树表示形式中从节点i到叶节点的最短路径上的边数(在此表示中,空子节点被视为外部节点或叶节点)。到达后代外部节点的最短路径是通过正确的子节点。每个子树也是左派树,dist(i)= 1 + dist(right(i))。

示例:下面的左手树显示了通过上述过程为每个节点计算出的距离。最右边的节点的等级为0,因为该节点的右边子树为null,并且其父节点的距离为1 x dist(i)= 1 + dist(right(i))。每个节点都遵循相同的规则,并计算其s值(或等级)。

lt1

从上面的第二个属性,我们可以得出两个结论:

  1. 从根到最右叶的路径是从根到叶的最短路径。
  2. 如果到最右边的叶子的路径有x个节点,则左边的堆至少有2个x – 1个节点。这意味着对于具有n个节点的左派堆,最右叶的路径长度为O(log n)。

运作方式:

  1. 主要操作是merge()。
  2. deleteMin()(或extractMin()可以通过删除根并为左右子树调用merge()来完成。
  3. 可以通过使用单个键(要插入的键)创建左派树并为给定树和具有单个节点的树调用merge()来完成insert()。

合并背后的想法:
由于右子树较小,因此其思想是将一棵树的右子树与其他树合并。以下是抽象步骤。

  1. 将值较小的根作为新根。
  2. 将其左子树挂在左侧。
  3. 递归合并其右子树和另一棵树。
  4. 从递归返回之前:
    –更新合并根目录的dist()。
    –如果需要,在根目录下交换左右子树,以保持合并的leftist属性
    结果

资料来源:http://courses.cs.washington.edu/courses/cse326/08sp/lectures/05-leftist-heaps.pdf

合并的详细步骤:

  1. 比较两个堆的根。
  2. 将较小的键推入一个空的堆栈,然后移至较小键的右子项。
  3. 递归比较两个键,然后继续将较小的键推入堆栈,然后移至其正确的子级。
  4. 重复直到到达空节点。
  5. 以最后处理的节点为准,使其成为堆栈顶部节点的右子节点,如果违反了leftist堆的属性,则将其转换为leftist堆。
  6. 递归地继续从堆栈中弹出元素,并使它们成为新堆栈顶部的正确子元素。

例子:
考虑下面给出的两个左派堆:
2个

将它们合并到单个左派堆中

3

节点7的子树侵犯了左派堆的属性,因此我们将其与左子节点交换,并保留了左派堆的属性。
4

转换为左派堆。重复这个过程

5
6

该算法在最坏情况下的时间复杂度在最坏情况下为O(log n),其中n是左派堆中的节点数。

合并两个左派堆的另一个示例:
lt9

左派树/左派堆的实现:

//C++ program for leftist heap / leftist tree
#include 
using namespace std;
  
// Node Class Declaration
class LeftistNode
{
public:
    int element;
    LeftistNode *left;
    LeftistNode *right;
    int dist;
    LeftistNode(int & element, LeftistNode *lt = NULL,
                LeftistNode *rt = NULL, int np = 0)
    {
        this->element = element;
        right = rt;
        left = lt,
        dist = np;
    }
};
  
//Class Declaration
class LeftistHeap
{
public:
    LeftistHeap();
    LeftistHeap(LeftistHeap &rhs);
    ~LeftistHeap();
    bool isEmpty();
    bool isFull();
    int &findMin();
    void Insert(int &x);
    void deleteMin();
    void deleteMin(int &minItem);
    void makeEmpty();
    void Merge(LeftistHeap &rhs);
    LeftistHeap & operator =(LeftistHeap &rhs);
private:
    LeftistNode *root;
    LeftistNode *Merge(LeftistNode *h1,
                       LeftistNode *h2);
    LeftistNode *Merge1(LeftistNode *h1,
                        LeftistNode *h2);
    void swapChildren(LeftistNode * t);
    void reclaimMemory(LeftistNode * t);
    LeftistNode *clone(LeftistNode *t);
};
  
// Construct the leftist heap
LeftistHeap::LeftistHeap()
{
    root = NULL;
}
  
// Copy constructor.
LeftistHeap::LeftistHeap(LeftistHeap &rhs)
{
    root = NULL;
    *this = rhs;
}
  
// Destruct the leftist heap
LeftistHeap::~LeftistHeap()
{
    makeEmpty( );
}
  
/* Merge rhs into the priority queue.
rhs becomes empty. rhs must be different
from this.*/
void LeftistHeap::Merge(LeftistHeap &rhs)
{
    if (this == &rhs)
        return;
    root = Merge(root, rhs.root);
    rhs.root = NULL;
}
  
/* Internal method to merge two roots.
 Deals with deviant cases and calls recursive Merge1.*/
LeftistNode *LeftistHeap::Merge(LeftistNode * h1,
                                LeftistNode * h2)
{
    if (h1 == NULL)
        return h2;
    if (h2 == NULL)
        return h1;
    if (h1->element < h2->element)
        return Merge1(h1, h2);
    else
        return Merge1(h2, h1);
}
  
/* Internal method to merge two roots.
 Assumes trees are not empty, and h1's root contains
  smallest item.*/
LeftistNode *LeftistHeap::Merge1(LeftistNode * h1,
                                 LeftistNode * h2)
{
    if (h1->left == NULL)
        h1->left = h2;
    else
    {
        h1->right = Merge(h1->right, h2);
        if (h1->left->dist < h1->right->dist)
            swapChildren(h1);
        h1->dist = h1->right->dist + 1;
    }
    return h1;
}
  
// Swaps t's two children.
void LeftistHeap::swapChildren(LeftistNode * t)
{
    LeftistNode *tmp = t->left;
    t->left = t->right;
    t->right = tmp;
}
  
/* Insert item x into the priority queue, maintaining
  heap order.*/
void LeftistHeap::Insert(int &x)
{
    root = Merge(new LeftistNode(x), root);
}
  
/* Find the smallest item in the priority queue.
Return the smallest item, or throw Underflow if empty.*/
int &LeftistHeap::findMin()
{
    return root->element;
}
  
/* Remove the smallest item from the priority queue.
Throws Underflow if empty.*/
void LeftistHeap::deleteMin()
{
    LeftistNode *oldRoot = root;
    root = Merge(root->left, root->right);
    delete oldRoot;
}
  
/* Remove the smallest item from the priority queue.
Pass back the smallest item, or throw Underflow if empty.*/
void LeftistHeap::deleteMin(int &minItem)
{
    if (isEmpty())
    {
        cout<<"Heap is Empty"<left);
        reclaimMemory(t->right);
        delete t;
    }
}
  
// Internal method to clone subtree.
LeftistNode *LeftistHeap::clone(LeftistNode * t)
{
    if (t == NULL)
        return NULL;
    else
        return new LeftistNode(t->element, clone(t->left),
                               clone(t->right), t->dist);
}
  
//Driver program
int main()
{
    LeftistHeap h;
    LeftistHeap h1;
    LeftistHeap h2;
    int x;
    int arr[]= {1, 5, 7, 10, 15};
    int arr1[]= {22, 75};
  
    h.Insert(arr[0]);
    h.Insert(arr[1]);
    h.Insert(arr[2]);
    h.Insert(arr[3]);
    h.Insert(arr[4]);
    h1.Insert(arr1[0]);
    h1.Insert(arr1[1]);
  
    h.deleteMin(x);
    cout<< x <

输出:

1
22
5

参考:
维基百科-左派树
CSC378:左派树