This community-built FAQ covers the “Translating Parent and Child Elements Into Indices” exercise from the lesson “Heaps: Python”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Complex Data Structures
FAQs on the exercise Translating Parent and Child Elements Into Indices
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
How would we a represent a heap that, somewhere in the middle of it, a node only has one child but that child has two children itself? For example, if we had the following heap:
…3…4 |… 5…
How would we represent it as a list? I tried but I got the following [0, 1, 2, 3, 4, 5, ?, 6, 7]
What goes in the ‘?’
This also makes it tough to use the parent, left_child, and right_child methods. Please help.
Unlike a tree, a heap is not defined by designated parent-child relationships, but is purposely constructed top-to-bottom, and left-to-right, so that the indexing scheme holds and the remove or add algorithm maintains the min or max structure of the heap.
In other words, as far as I can see, the situation you describe would never arise with a heap. After 0, 1, 2, 3, 4, and 5 were added to your heap, 6 would be forced to occupy the position next to 5 as the rightmost child of 2, and 7 would be the leftmost child of 3.
Could anyone explain to me why the parent index of index 4 is 2?
This is only the case when your root index is 1. When the root index is 0, the parent of index 4 is 1:
3 4 5 6
And according to the exercise they are using root index 0:
[None, 10, 13, 21, 61, 22, 23, 99]
Indices: [0, 1, 2, 3, 4, 5, 6, 7]
Yet with the arithmetic for a root index 1 heap:
index // 2
- Left Child:
index * 2
- Right Child:
(index * 2) + 1
Or am I missing something? Am I completely off base here?!?
I am wondering the same thing. Can anyone shed light on this please?
The tree is actually
4 5 6
The first index is None and should actually be ignored. In other websites it seems conventional to not even have None in the index. The root is at index 1 with the value 10.
So yes, you were literally “off base” lol.