Left-leaning heap

Source: Internet
Author: User

The left-leaning heap (or leftist tree), like the two-fork heap previously recorded, is a heap; unlike a normal two-fork heap, it is a kind of consolidated heap. The consolidated heap has great advantages over the consolidation of two heaps compared to the normal two-fork heap: For basic two-fork heap merging, the time complexity is O (n), and for the consolidated heap, the time complexity is O (log2N ).

Left- leaning heap properties

A left-leaning heap (also called a Leftist tree) is a kind of consolidated heap. It has the following properties:

  1. Each node contains the left dial hand node pointer, right child node pointer, key value key, NPL(Null Path length, which indicates the current node to the nearest child node, the node is less than two child nodes)
  2. The key value of each node is less than the key value of its left and right child nodes (the nature of the minimum heap)
  3. NPL of the left child node of each node is greater than or equal to the right child node
  4. The NPL value of the node equals the NPL value of its right child node + 1
  5. The left and right sub-nodes and their lower nodes are also stacked
  6. The NPL value of the leaf node equals 0, and the NPL value of the empty node equals-1
Left- leaning heap merging

The consolidated heap has a great advantage over the consolidation of the heap, and for the left-leaning heap, the merge operation is the core operation, such as INSERT, delete operation can be completed on the basis of the merge operation: insert operation can treat the newly inserted element as a heap and then merge two left-leaning heaps Delete operation (the deletion of a heap always takes an element from the top of the heap) removes the top element of the heap and merges the Zoko of the top element of the heap with the right child heap.
The merge process for the left-leaning heap is:

  1. If an empty left-leaning heap merges with a non-empty left-leaning heap, the non-empty left-leaning heap is returned directly
  2. If two left-leaning heaps are non-empty, compare the root node of the two heap, removing the root node of the smaller heap as the new root node. Then merge the right child node of the "smaller heap" root node (and the heap underneath it) and the "larger heap" (recursive)
  3. If the NPL of the right child node of the new heap is greater than the NPL of the left Dial hand node, then the left child node is exchanged
  4. Set the root node of the new heap to NPL = right child node of NPL + 1

For the concrete process of heap merging, we can refer to the left -leaning heap-graphic parsing

Implementation (c + +)

The binary heap and the consolidated heap are often used in the priority queue, and the following code merges the heap, the elements are queued, and the teams are

#include <iostream>using namespace std;struct treenode{int key;int NPL; Treenode* left; treenode* right; TreeNode (int k) {key = K;NPL = 0;left = right = NULL;}}; void Swap (treenode* node1, treenode* node2) {treenode* tmp = NODE1;NODE1 = NODE2;NODE2 = tmp;} The merge operation of the left-leaning heap treenode* merge (treenode* heap1, treenode* heap2) {///if one of them is an empty heap, return another if (!HEAP1) return heap2;if (!HEAP2) Return heap1;//Select "Smaller heap" and its root node as the root node of the new heap if (Heap1->key > Heap2->key) {Swap (HEAP1, HEAP2);} Merge the right child heap of the smaller heap with the larger heap, and place the right child heap of the smaller heap heap1->right = merge (Heap1->right, HEAP2); if (Heap1->left = = NULL | | (Heap1->left && heap1->right && HEAP1-&GT;LEFT-&GT;NPL < HEAP1-&GT;RIGHT-&GT;NPL)) {//If the NPL of the left Dial hand node of the root node of the heap is the NPL of the less right operand child node, then swap the left and right child node swap (heap1->left, heap1->right);} if (heap1->right = = NULL) HEAP1-&GT;NPL = 1;ELSEHEAP1-&GT;NPL = HEAP1-&GT;RIGHT-&GT;NPL + 1; The new heap of NPL is set to the right child node of NPL +1return Heap1;} Insert element k into the heap heap, that is, the element is enqueued. Note here the reference to the pointer is void Enqueue (treenode*& heap, int k) {if (!heap) {heap = new TreeNode (k); RetuRN;} treenode* new_heap = new TreeNode (k); heap = Merge (heap, new_heap);} Element out of the team, note here using pointers to reference int Dequeue (treenode*& heap) {int result = Heap->key;heap = Merge (Heap->left, heap-> right); return result;}

Left-leaning heap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.