Path in the heap Java description

Source: Internet
Author: User

Inserts a series of given numbers into a small, initially empty heap H[] . iIt then prints the path from the root node to any given subscript H[i] .

Input format:

The 1th row of each group of tests contains 2 positive integersN andM (≤1000), which are the number of inserted elements, and the number of path bars that need to be printed. The next line gives the number of n in the interval [-10000, 10000] to be inserted into an integer that is initially empty for the small top heap. The last line gives a m subscript.

Output format:

For each subscript given in the input i , output data from the H[i] path to the root node in a row. The numbers are separated by 1 spaces, with no extra spaces at the end of the line.

Input Sample:
5 346 23 26 24 105 4 3
Sample output:
24 23 1046 23 1026 10
Idea: A heap is a fully binary tree, with arrays of data stored in the heap, with a corner labeled 1 representing the top of the heap.     If the current angle is labeled I, the left son is 2i, the right son is 2i+1, and the parent node is I/2. Each insertion of an element, put him in the last position of the complete binary tree, so that it compared with the parent node, if the parent node is smaller than the value of the interchange, continue to cycle comparison, because Sentinel arr[0]=-10001 is set, so the insertion of the data angle is only a minimum of 1, can not be 0.
ImportJava.util.Scanner;//declaring a heap classclassHeap {//the data content in the heap has an array and a size that represents the heap sizes    int[] arr; intsize; //Initialize the heap, because N is given a value of 10000 to 10000, and 10001 is for the array to meet the problem, arr[0]=-10001 as the Sentinel, so that the array from the angle Mark 1 begins to store the data//at first the heap was empty, so size=0     Publicheap () {arr=New int[10001]; arr[0]=-10001; Size=0; }    //to insert data into the heap, the structure of the heap is a fully binary tree     Public voidInsertintnum) {        if(size==0) {arr[1]=num; Size++; }        Else{arr[++size]=num; //Recovery T            intt=size;  while(NUM&LT;ARR[T/2])            {                intTemp=arr[t/2]; Arr[t/2]=num; Arr[t]=temp; T=t/2; }                }    }    //gets the path from the given corner mark to the top of the heap     Public voidRoadintnum) {         while(num!=0) {System.out.print (Arr[num]+" "); Num=num/2; }        if(num==0) {System.out.println (); }    }}//Test Classclasstest{ Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); Heap h=NewHeap (); intn=Sc.nextint (); intm=Sc.nextint ();  for(intI=0;i<n; i++ )        {            intnum=Sc.nextint ();        H.insert (num); }                 for(intI=0;i<m; i++ )        {            intR=Sc.nextint ();        H.road (R); }    }}

Printing results:

Path in the heap Java description

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.