N
N
nepster-web2015-09-18 15:47:39
PHP
nepster-web, 2015-09-18 15:47:39

How to determine the parent cell in the pyramid?

There is a pyramid of numbers, it looks like this:
994dff5d19a34a6c8c5ebc3ec0798071.png
Each cell has its own number and starts from 0.
Thus, it turns out that the cell coordinates are: level and number.
The question is, how can one rise to the topmost cell through the parents?
That is, for example, we determine the cell from which we will rise:
level 4 (account from 0), number 1.
You need to get the following cells:
- level 3 number 1
- level 2 number 0
- level 1 number 0
- level 0 number 0
Naturally, that the matrix can be any and the number of cells as much as you like. Can you please tell me how to calculate it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2015-09-18
@nepster-web

It's very similar to a binary heap , only it's ternary in your example. In this regard, it may be convenient to store such a pyramid in a one-dimensional array, as is done for a heap. For the case with two children, the formulas for the address of the children are at the link, it is very easy to get the formulas for your case from them.

Level 4 (score from 0), number 1.
You need to get the following cells:
- Level 3 number 1

Like 0 for 1 cell the parent should be?
If this is a typo, then with your storage method, it is enough to take the integer part of dividing the child's number by 3.

A
AlikDex, 2015-09-18
@AlikDex

first you need to master the introductory part on the matrices, and then ask the right questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question