Data Structure BASICS (19) and data structure basics 19

Source: Internet
Author: User

Data Structure BASICS (19) and data structure basics 19
Full Binary Tree

 

First, let's review the two properties of the Complete Binary Tree:

Property 1: the depth of a Complete Binary Tree with n nodes is [logn] (rounded down) + 1.

Property 2: If a Complete Binary Tree Containing n nodes is numbered 1 to n from top to bottom and from left to right, any node numbered I in the Complete Binary Tree is:

(1) If I = 1, the node is the root of the binary tree and has no parent. Otherwise, the node numbered [I/2] (rounded down) is the parent node;
(2) If 2i> n, the node has no left child. Otherwise, the node number 2i is the left child node;
(3) If 2i + 1> n, the node has no right child node. Otherwise, the node numbered 2i + 1 is the right child node.

 

Array and full Binary Tree

 

It can be seen that if the Complete Binary Tree is numbered from top to bottom and from left to right from 0 to n-1, the nature of the Complete Binary Tree needs to be modified as follows:

(1) If I = 0, then the node is the root of the binary tree, no parent, otherwise, number for [(I-1)/2] (down to take an integer) is its parent node;
(2) If 2i + 1> n, the node has no left child. Otherwise, the node number 2i + 1 is the left child node;
(3) If 2i + 2> n, the node has no right child node. Otherwise, the node number 2i + 2 is the right child node.


Large and small top heaps

 

The heap is a series of {r1, r2 ,..., Rn }:


(Small top heap)


(Big top stack)

 

Design of the big top stack

Template <typename Type> class MaxHeap {public: MaxHeap (int _ maxSize = 10); virtual ~ MaxHeap (); bool isEmpty () const; void push (const Type & item); void pop (); const Type & top () const; private: // increase the heap capacity by two times void resize (); // penetrate void trickUp (int index) upwards; // penetrate void trickDown (int index) downward; private: type * heapArray; int maxSize; // currentSize has two meanings: // 1. number of elements stored in the current heap // 2. represents the first vacant int currentSize of the current full binary tree ;};

Implementation of big top stack

// Construct template <typename Type> MaxHeap <Type>: MaxHeap (int _ maxSize): maxSize (_ maxSize), currentSize (0) {if (maxSize <1) throw std: length_error ("heap size must> = 1"); heapArray = new Type [maxSize] ;}// destructor template <typename Type> MaxHeap <Type> :: ~ MaxHeap () {delete [] heapArray; heapArray = NULL; currentSize = 0;} // empty template <typename Type> bool MaxHeap <Type >:: isEmpty () const {return currentSize = 0 ;}

Heap top Element

// View the heap top element template <typename Type> const Type & MaxHeap <Type >:: top () const {if (isEmpty () throw std :: underflow_error ("heap is empty"); return heapArray [0];}

Insert element

// Insert template <typename Type> void MaxHeap <Type>: push (const Type & item) {// If the heap is full, expand the heap if (currentSize = maxSize) {resize () ;}// Insert the element to the first empty position of the heap. heapArray [currentSize] = item; // maintain the heap nature: trickUp (currentSize); ++ currentSize ;}
// Penetrate up and move the inserted element to the appropriate position. template <typename Type> void MaxHeap <Type >:: trickUp (int index) {// locate the parent node int parent = (index-1)/2; Type bottom = heapArray [index] based on the nature of the full binary tree; // The cyclic termination condition // 1. index = 0 // Insert the bottom element to the root node // 2. heapArray [parent]> = bottom // insert bottom to a subnode of the parent node while (index> 0) & (heapArray [parent] <bottom )) {// move the parent node down heapArray [index] = heapArray [parent]; index = parent; parent = (parent-1)/2 ;} // insert heapArray [index] = bottom ;}
// Double the heap size. template <typename Type> void MaxHeap <Type >:: resize () {int newSize = std: max (maxSize * 2, 1 ); type * newHeap = new Type [newSize]; for (int I = 0; I <currentSize; ++ I) {newHeap [I] = heapArray [I];} delete [] heapArray; heapArray = newHeap; maxSize = newSize ;}

Delete heap top Element

// Delete template <typename Type> void MaxHeap <Type >:: pop () {if (isEmpty () throw std: underflow_error ("heap is empty "); // only one element, if (currentSize = 1) {heapArray [0]. ~ Type (); currentSize = 0; return;} // display the heapArray [0]. ~ Type (); // place the most element directly to the top of the heap, // and currentSize-1 heapArray [0] = heapArray [-- currentSize]; // If the heap is damaged at this time: trickDown (0 );}
// Downstream penetration template <typename Type> void MaxHeap <Type >:: trickDown (int index) {int largeChild; Type top = heapArray [index]; // you only need to reach the last layer of the Complete Binary Tree // The termination condition of the Loop: // 1. index has reached the last layer (index> = currentSize/2: Find a suitable position) // 2. A suitable position (top> = heapArray [largeChild]) while (index <currentSize/2) {int leftChild = (index * 2) + 1 is found in the middle of the heap; int rightChild = leftChild + 1; // if there is a right son node, in addition, the right son node is larger than the left son node if (rightChild <currentSize & heapArray [rightChild]> heapArray [leftChild]) largeChild = rightChild; else largeChild = leftChild; if (top> = heapArray [largeChild]) break; // otherwise, the heapArray [index] = heapArray [largeChild] needs to be further penetrated. // index needs to be searched down for index = largeChild;} // insert heapArray [index] = top ;}

Heap sorting

Heap sorting is to insert elements to the heap one by one and then extract them one by one;

// Heap sorting template <typename Type> void heapSort (Type * begin, Type * end) {int size = end-begin; MaxHeap <Type> maxHeap (size ); // save it to the heap for (Type * current = begin; current <end; ++ current) maxHeap. push (* current); // retrieve the while (begin! = End) {* begin = maxHeap. top (); ++ begin; maxHeap. pop () ;}} template <typename Type> void heapSort (Type * array, int arraySize) {return heapSort (array, array + arraySize );}

Appendix-test code:

int main(){    int array[20];    for (int i = 0; i < 20; ++i)    {        array[i] = rand()%100;        cout << array[i] << ' ';    }    heapSort(array, 20);    cout << endl;    for (int i = 0; i < 20; ++i)    {        cout << array[i] << ' ';    }    cout << endl;    return 0;}

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.