AI Horizon: Intelligence and Reason in Computers
Introduction Home Essays Resources Contact Info
Essays
  Basic CS
General AI
Chess AI
Go AI

Resources
  Source Code
AI Tips and Tricks
Books
Links

Other
  Code Journal
Site Updates

An Affiliate of Cprogramming.com
 

Heap Tree and Heap Sort Source Code
(Up to Resources : Source Code Repository : Trees)

This source file is an implementation of the Heap Tree class and the Heap Sort algorithm. The class is implemented with templates.

For the templated class, the elements must have the operators >, =, and < defined.

To use the Heap sort that is built into the class, two seperate steps must be taken. The first is to call the constructor, which organizes the array into a heap:

HeapTree<TYPE> HeapName(Array, Num, MaxNum);

TYPE is the data type of the elements, Array is the actual array to be sorted, and Num is the number of elements that are to be sorted. MaxNum normally sets the limit on the number of data nodes that the Heap can have. If you are only using the heap for sorting, you can set MaxNum to Num. (However, MaxNum should not be set to anything less than Num).

When the constructor is called, the Heap copies the Array. Thus, neither the Array variable nor what it points to will be modified by the Heap.

The second step is to call the actual sort, which will organize the heap into a sorted array:

NewArray *Sort();

This Sort() function will return a pointer to another array which is sorted. Any modifications done to NewArray or its contents will not affect the heap.

Download the Source: heap.h

All content is written and published by the people at or affiliated with AI Horizon <http://www.aihorizon.com/>.
Send any comments and suggestions to [email protected].

Please report any errors to [email protected].