algorithm - Implementing a 4-heap using an array -


When using an array to store all the elements, what type of math do you cross the 4-heap To use? In particular, how do you find the indicator of a parent node for a specific leaf?

Let's say I have the following array:

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... e.t.c.

It is made with a heap of which 1 is root, 2.5 children, children of 6. 9 2 etc.

Why do I need to do math 6 Need to find the parents (for example)?

to find the parents of any child (except 1, in which there is no parent To find the first and last child of a parent:

  basic = int ((child + 2) / 4)  

/ P>

  child_first = parent * 4 - 2 child_last = parent * 4 + 1  

You can see it in every operation, because you have four times at each level As you did at the previous level:

  1 (1) 2 to 5 4) 6 to 21 (16) 22 to 85 (64) 86 Area of ​​341 (256) 342 1365 (1024) level 1: 1 - & gt; 2 3 4 5 Level 2: 2 - & gt; 6 7 8 9 3 - & gt; 10 11 12 13 4 - & gt; 14 15 16 17 5 - & gt; 18 19 20 21 Level 3: 6 - & gt; 22 23 24 25 7 - & gt; 26 27 28 29 8 - & gt; 30 31 32 33 9 - & gt; 34 35 36 37 10 - & gt; 38 39 40 41 11 - & gt; 42 43 44 45 12 - & gt; 46 47 48 49 13 - & gt; 50 51 52 53 14 - & gt; 54 55 56 57 15 - & gt; 58 59 60 61 16 - & gt; 62 63 64 65 17 - & gt; 66 67 68 69 18 - & gt; 70 71 72 73 1 9 - & gt; 74 75 76 77 20 - & gt; 78 79 80 81 21 - & gt; 82 83 84 85  

& nbsp;

  Level 4: 22 - & gt; 86 87 88 89 23 - & gt; 90 91 92 93 24 - & gt; 94 95 96 97 25 - & gt; 98 99 100 101 :::: 82 - & gt; 326 327 328 32 9 83 - & gt; 330 331 332 333 84 - & gt; 334 335 336 337 85 - & gt; Examples are:  

Examples are:

  342 = parent (344/4) = 86 (for 343,344,345) 346 = Parents of parents (348/4) = 87 (for 347,348,349) 21 = 21 * 4 - 2 = Last child of 82 = 21 * 4 + 1 = 85  < / Pre> 

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -