Binomial Tree & Heap

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seayoungsjtu/article/details/49665625

Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer

Defination

  • A Binomial Tree is defined recursively :
    1. A tree with only one node is a Binomial Tree, and it’s order is 0
    2. A Binomial Tree of order n has subtrees from order 0 to order n-1, from the most right to the most left
    3. A binomial tree of order k has 2k nodes, height k.
      Defination of Binomial Tree
  • A Binomial Heap is a collection of Binomial Trees with different orders, and each node of a Binomial Tree has a key greater than its parent, except for the root node.
    The number and orders of these trees are uniquely determined by the number of nodes n: each binomial tree corresponds to one digit in the binary representation of number n. For example number 13 is 1101 in binary, 13=2 3 +2 2 +2 0   , and thus a binomial heap with 13 nodes will consist of three binomial trees of orders 3, 2, and 0.
    这里写图片描述
    A Binomial Heap supports quick merge, and as well it’s a priority queue (also called meldable heap).

Operations

As mentioned above, the simplest and most important operation is the merging of two binomial trees of the same order. It’s used for many other operations such as build, insert, delete, etc.

Merge

  • To merge two Binomial Trees with the same order, first compare their root keys; next, the Binomial Tree with smaller root key will become the left most child of the Binomal Tree with greater root key, and the order of the Binomial Tree with greater root key should increase by 1.
    Merge two Binomial Trees
  • To merge two Binomial Heaps, just merge all the Binomial Trees with the same order until all the Binomial Trees in the new Binomial Heap have different orders.
    Merge tow Binomial Heaps

Insert

To insert a new node into a Binomial Heap, just creat a new Binomial Heap with only one Binomial Tree of order 0 and merge the new heap with the original heap.

Build

  • The first way to bulid a Binomial Heap is to insert nodes one by one, and each time we insert a new node, we do merge once.
  • Another more efficient way to build a Binomial Tree is to merge Binomial Trees by order, that is: First merge all the nodes(which is also Binomial Trees of order 0) with another node, and if there’s a node left, it can finish merging and be directly added to the array storing all the Binomial Trees at the end of the build work; Next merge all the Binomial Trees left(which are all order 1) one another and the Binomial Tree left can also finish merging; Do this until all the Binomial Trees have different orders, then finish building.

Decrease

To decrease the key of a given node, just change it with it’s parent until it’s no smaller than it’s parent.

Delete

  • To delete the Min-node, just remove the root node with the smallest key of all the Binomial Trees, and merge it’s sub tees with other Binomial Trees.
  • To delete a given node, just decrease the key of the node to negative infinity, do Decrease and do Delete Min-node.

猜你喜欢

转载自blog.csdn.net/seayoungsjtu/article/details/49665625
今日推荐