The heap of data structure detailed _c language

Source: Internet
Author: User

1. Overview

The heap (also called the precedence queue) is a complete binary tree, which is characterized by the value of the parent node being greater than (less than) two child nodes (called a large top heap and a small top heap, respectively). It is often used to manage the information during the execution of the algorithm, including heap ordering, priority queue, etc.

2. Basic operation of the heap

The heap is a complete binary tree with a height of O (LG N), whose basic operation is proportional to the height of the tree. Before introducing the basics of the heap, let's introduce a few basic terms:

A: The array to represent the heap, with the subscript starting at 1 and all the time to n
Parent (T): node T's parents node, that is, floor (T/2)
Right (T): node T's left child node, that is: 2*t
Left (T): node T's right child node, that is: 2*t+1
Heap_size (a): Heap A current number of elements
The main four operations are given below (for example, in the case of a large top heap):
2.1 Heapify (A,N,T)
This operation is mainly used to maintain the basic properties of the heap. A subtree with the root of right (T) and left (T) is assumed to be a heap, and then the subtree with T as the root is adjusted to make it a heap.

Copy Code code as follows:

void heapify (int a[], int n, int t)

{

int left = left (t);

int right = right (t);

int max = t;

if (left <= n) max = A[left] > A[max]? Left:max;

if (right <= n) max = A[right] > A[max]? Right:max;

if (Max!= a[t])

{

Swap (A, Max, T);

Heapify (A, N, max);

}

}

2.2 Buildheap (A,n)
The main operation is to convert array A to a large top heap. The idea is to first find the last non-leaf node of the heap (that is, the first N/2 node), then start from the node, adjust each subtree from back to front, make it known as a heap, and eventually the entire array is a heap.
Copy Code code as follows:

void buildheap (int a[], int n)

{

int i;

for (i = N/2; i<=n; i++)

Heapify (A, n, i);

}

2.3 Getmaximum (A,n)
This operation is primarily to get the largest element in the heap while preserving the basic properties of the heap. The largest element of the heap is the first element, save it, place the last element in the a[1] position, and then adjust a from top to bottom to make it a heap.
Copy Code code as follows:

void Getmaximum (int a[], int n)

{

int max = a[1];

A[1] = A[n];

n--;

Heapify (A, N, 1);

return Max;

}

2.4 Insert (A, N, t)
Adds an element t to the heap while preserving the nature of the heap. The idea is to put T at the end of a, and then start with the element, and then adjust it up until a is a big top heap.
Copy Code code as follows:

void Insert (int a[], int n, int t)

{

n++;

A[n] = t;

int p = n;

while (P >1 && a[parent (p)] < T)

{

A[P] = A[parent (p)];

p = PARENT (p);

}

A[p] = t;

return Max;

}

3. The application of the heap

3.1 Heap Sort
The most common application of the heap is heap ordering, with time complexity of O (n lg N). If it is small to large sort, with a large top heap, from large to small sort, with a little top heap.

3.2 in O (n LG K) time, the K-sorted table is merged into a sorted table, and N is the number of elements in all ordered tables.

"Parse" takes the first 1 million integers, constructs an array to store with a small top heap, and then then takes down an integer, if it is greater than the smallest element or heap top element, then assigns it to the top of the heap, and then adjusts the entire heap with heapify, and so on, The last 1 million integers left in the heap are 1 million digits. This method can save memory greatly.
3.3 A file contains 100 million random integers, how to quickly find the largest (small) 1 million digits? (Time complexity: O (n LG K))

4. Summary

Heap is a very basic but very useful data structure, many complex algorithms or data structures are based on the heap, so it is very important to understand and grasp the data structure of the heap.

5. Reference materials

(1) Classic algorithm Tutorial "Introduction to Algorithms"

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.