"Algorithm Design and Analysis Basics" 17, heap

Source: Internet
Author: User

Storing heap data in arrays

Package Cn.xf.algorithm.ch06changerule;import Java.util.arraylist;import Java.util.list;import org.junit.Test;/** * * Function: Construction of heap * 1, the heap can be defined as a binary tree, the node of the tree contains the key, and satisfies the condition * 1) The shape requirements of the tree: This binary tree is a basic complete (full binary tree), each layer of the tree is full, except the last layer of the rightmost element may be absent * 2) parental advantage, heap characteristics, Each node has a key greater than or equal to the key of his or her child (for any leaf we think it is automatically satisfied) * for the heap: * There is only one N-node complete binary tree his height: the logarithm of N of the log2 of the removed bounds * The root of the heap always contains the largest element of the heap * A node of the heap and the child of that node Sun is a heap. * You can use arrays to implement heaps by recording the elements of the heap from top to bottom, left to right.  * @author Xiaofeng * @date July 9, 2017 * @fileName Heap.java * */public class Heap {/** * heap data storage structure */private List<double> heap;/** * Build a heap from the bottom up */private list<double> createheaddowntoup (list<double> heap) {if ( Heap = = NULL | | Heap.size () <= 0) return heap;//number of data int nums = Heap.size ();//bar The whole array is moved back one bit to facilitate the calculation of data, since 0, then 2*0 or 0, does not reflect the n is the basic setting for the left child of N heap.add (0, 0d);//Build a heap, starting at the middle of the array, because twice times the mid-seat mid is almost the end of the tree, and in the vicinity of this 2*mid is the child node for the mid node for (int i = NUMS/2 + 1; i > 0; i) {//Get the address of the datum node int baseindex = i;//Get the value of this node double vbasevalue = Heap.get (BaseIndex); Boolean isheap = FalSe This is used to determine whether the current traversal of these three numbers satisfies the concept of a heap//heap transformation, Exchange tree node and child node values, so that the current tree satisfies the concept of the heap//2 * BaseIndex <= nums This tree is used to judge the subtree also satisfies the definition of the heap while (!). Isheap && 2 * baseindex <= nums) {//Gets the position of the left child node of the currently traversed data int maxchildindex = 2 * baseindex;//get the large position from the two child nodes if (m Axchildindex < Nums) {//If the left child's position is smaller than the total length, due to the full binary tree attribute, then there must be a right child node//judge the child node's data is relatively large, so that Max is the big if (Heap.get ( Maxchildindex) < Heap.get (Maxchildindex + 1)) {//If the right child is larger Maxchildindex + + 1;}} Again, the value of the current node is not greater than the value of the child node, if so, the current subtree is the property that satisfies the heap//maxchildindex = = Nums That's still a lie, can be compared to the left sub-tree if (Maxchildindex > Nums | | Vbasevalue >= Heap.get (maxchildindex)) {isheap = true;} else {//if not, then swap, the big data is exchanged to the node, the data on the node is switched to the child node Heap.set ( BaseIndex, Heap.get (Maxchildindex)); BaseIndex = Maxchildindex;heap.set (BaseIndex, Vbasevalue);}}}    Remove the first 0 and then return to Heap.remove (0); return heap;}                private void Shifheaddowntoup (int i) {if (heap = = NULL | | heap.size () <= 0) return;        Number of data int nums = Heap.size (); The whole array is moved back one bit to facilitate the calculation of the data, because starting from 0, then 2*0 or 0, does not reflect then is the basic setting of the left child of N heap.add (0, 0d);        Boolean isheap = false;        int baseindex = i;        Double vbasevalue = Heap.get (i);  while (!isheap && 2 * baseindex <= nums) {//Gets the position of the left child node of the currently traversed data int maxchildindex = 2 *            BaseIndex;                Get the big one from the two child nodes if (Maxchildindex < nums) {//If the left child's position is smaller than the total length, there must be a right child node due to the full binary tree attribute                    Judge the child's node data to be large, so that Max is the big if (Heap.get (Maxchildindex) < Heap.get (Maxchildindex + 1)) {                If the right child compares larger maxchildindex + = 1; }}//Again, the value of the current node is not greater than the value of the child node, if so, the current subtree is the property that satisfies the heap//Maxchildindex = = Nums that or Hide the condition, you can make a comparison of the left subtree if (Maxchildindex > Nums | | vbasevalue >= heap.get (maxchildindex)) {Ishea            P = true; } else {//if dissatisfied, then swap, the big data is exchanged on the node, the data of the node is swapped to the child node Heap.set (BaseIndex, Heap.get (Maxchildindex) );                BaseIndex = Maxchildindex;            Heap.set (BaseIndex, Vbasevalue);    }}//Remove the first 0 and return to Heap.remove (0);    }//Create heap public heap () {heap = new arraylist<double> ();    Createheaddowntoup (heap); }public Heap (list<double> data) {if (data = = NULL | | data.size () <= 0) {data = new arraylist<double    > ();        } heap = data; Createheaddowntoup (heap);} @Overridepublic String toString () {return heap.tostring ();}    public void Add (Double value) {if (value = = null) return;    Heap.add (value);//int insertinedx = Heap.size ();    Bottom-up build heap for (int i = Heap.size ()/2; I >= 0; i.) {Shifheaddowntoup (i + 1); }}/** * Delete an element, get the index position of this element to delete * 1, the root of the key "and" the last key of the heap K do exchange * 2, the scale of the heap minus one * 3, strictly according to the bottom-up of the algorithm of the practice, bar k down filtering, heap data heap * @param index */publi    c void Delete (int index) {//This is a bottom-up heap of data//bar The last data is populated with the data to be deleted Double Lastvalue = Heap.get (Heap.size ()-1); Remove the last element, the last element to replace theAn element Heap.set (index, lastvalue) that needs to be deleted;    Heap.remove (Heap.size ()-1); Start from bottom up to heap for (int i = index; I >= 0; i) shifheaddowntoup (i + 1);}}

  

"Algorithm Design and Analysis Basics" 17, heap

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.