Data Structure - heap

stack

"Heap" (Heap), is actually a special kind of tree.

What kind of tree is the heap? We need to meet two conditions

 

First, the heap is a complete binary tree;

Second, the value of the stack are each node must be greater than (or less) its subtree value for each node (which can also be said about the value of the node).

 

Large 顶堆

Value for each node is greater than or equal stacks for each node in the subtree value, we called "big top heap."

Top small heap

For each node the value is less than equal to each node in the subtree stack value, we call "minor vertex heap."

 

As shown in FIG, 1, and 2 is a large pile top, the top of stack 3 is small, not a stack 4, because 4 is not a complete binary tree

 

How to store a heap

Because the heap is a complete binary tree, complete binary tree is suitable for use arrays to store.

Because we do not need to store pointers about child node, simply through the array index, you can find around the child and parent nodes of a node.

An array to store the complete binary tree is very save storage space. 

An array index to the left child node of the i, that is, the labeled node i * 2, the right child is the subscript i node * 2 + 1, the parent node is the subscript i / 2 in.

 

 

 

Heap of

When we insert or delete elements in the heap, no longer meets the characteristics of an inverted pile heap, need to be adjusted again allowed to meet the characteristics of the heap, the heap of the process is.

Heap of divided into two, down and from the bottom up from.

 

Reactor core operating

 

To insert a stack element

We let the newly inserted node parent node size comparison. If not less than the size of the child node of the parent node equal relationship, we swap the two nodes.

This process is repeated until the stack of completed. This is a typical stack-up from the method of.

 

 

Delete the top of the heap element

Top of the stack elements are special, according to the definition of the stack, the top of stack memory elements is maximum or minimum data stack.

Assumption is that big pile top, remove the largest element, the second largest element will be placed in the top of the heap.

If you like to insert elements as comparative exchange, the problem occurs.

The last problem is that out of the heap of the heap does not meet the characteristics of the complete binary tree.

To solve this problem, we need to change the thinking.


We last node into the top of the stack, and then by the same method of Comparative parent and child nodes.

Parent-child relationship is not satisfied for the size, interchanged, two nodes, and this process is repeated, until satisfied until the magnitude relation between parent and child nodes.

 

To avoid this problem empty array, so we remove the last element in the array. After the results of this method of heap, be sure to meet the characteristics of the complete binary tree.

Using this method, the stack of the process, the exchange operation is, the array of "holes" will not occur. (It will be appreciated as the hole is placed at the end of the array)

This is a typical down from the heap of the process.

 

Code for heap operations implemented

public class Heap {
  private int[] a; // 数组,从下标1开始存储数据
  private int n;  // 堆可以存储的最大数据个数
  private int count; // 堆中已经存储的数据个数

  public Heap(int capacity) {
    a = new int[capacity + 1];
    n = capacity;
    count = 0;
  }

  public void insert(int data) {
    if (count >= n) return; // 堆满了
    ++count;
    a[count] = data;
    int i = count;
    while (i/2 > 0 && a[i] > a[i/2]) { // 自下往上堆化
      swap(a, i, i/2); // swap()函数作用:交换下标为i和i/2的两个元素
      i = i/2;
    }
  }
 }


public void removeMax() {
  if (count == 0) return -1; // 堆中没有数据
  a[1] = a[count];
  --count;
  heapify(a, count, 1);
}

private void heapify(int[] a, int n, int i) { // 自上往下堆化
  while (true) {
    int maxPos = i;
    if (i*2 <= n && a[i] < a[i*2]) maxPos = i*2;
    if (i*2+1 <= n && a[maxPos] < a[i*2+1]) maxPos = i*2+1;
    if (maxPos == i) break;
    swap(a, i, maxPos);
    i = maxPos;
  }
}


Heap of nodes along the path where the process is relatively exchange, so heap of time with complexity proportional to the height of the tree.

Contains a complete binary tree of n nodes, the tree does not exceed the height of log n.

Insert and delete data elements in the top of the stack is the main logic of the stack, so the stack is inserted into a top of the stack element and delete elements of the time complexity is O (logn).

 

 

 

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104683008