Heap Related Algorithms

Source: Internet
Author: User

Heap is a special binary tree with the following two properties:

1. The value of each node is greater than or equal to the value of each subnode;

2. The tree is completely balanced, and the leaves on the last layer are at the leftmost position.

The maximum heap and the minimum heap are defined as the maximum heap. The minimum heap is defined as follows:

1. The value of each node is smaller than or equal to the value of each subnode;

2. The tree is completely balanced, and the leaves on the last layer are at the leftmost position.


This article implements heap creation, deletion, insertion, and heap sorting.

The example in this article takes the largest heap as an example:

// heap_function.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include using namespace STD; void swap (int * P, int * q) {int temp = * P; * P = * q; * q = temp;} void moveup (INT heap [], int start) // The upward movement operation is used to insert elements into the heap and use // {int I = start; int J = (I-1)/2; while (I> 0) {If (heap [I]> heap [J]) {swap (& heap [I], & heap [J]); I = J; j = (I-1)/2;} elsebreak;} void insert_ele (INT heap [], int value, Int & COUNT) // insert an element into the heap. Count indicates the number of elements in the heap. // {heap [count] = value; moveup (heap, count); count ++ ;} void movedown (INT heap [], int first, int last) {int largest = 2 * First + 1; while (largest <= last) {If (largest heap [first]) {swap (& heap [Largest], & heap [first]); First = largest; Largest = 2 * largest + 1;} else largest = last + 1 ;}} void delete_ele (INT heap [], Int & COUNT) // Delete the heap top element from the heap // {heap [0] = heap [count-1]; count --; movedown (heap, 0, Count-1);} void floyalgorithm (INT heap [], int N) // build a heap from the bottom to the top, n is the number of elements // {for (INT I = n/2-1; I> = 0; I --) {movedown (heap, I, n-1 );}} void William amsalgorithm (INT heap [], int N) // build the heap from the top to the bottom, proposed by John Williams // {for (INT I = 0; I = 0; -- I) movedown (heap, I, n-1); For (INT I = n-1; I >=1; -- I) {swap (& heap [0], & heap [I]); movedown (heap, 0, I-1 );}} int _ tmain (INT argc, _ tchar * argv []) {int data [9] = {2, 8, 6, 1, 10, 15, 3, 12, 11}; // heapsort (data, 9); // floyalgorithm (data, 9); William salgorithm (data, 9); For (INT I = 0; I <9; ++ I) {cout


related heap 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.