Reputation: 135
I am not hiding this is a part of my homework but I've tried enough before posting here.
So...
I need to prove for a binary tree that a node k have its left child on 2k and right child on 2k + 1 position. I've proved this with induction.
Now I need to prove for a binary tree that a node k have its parent on (floor)(k/2)
position. I took two cases.
Tried it with induction as well. It's true for a tree of 3 nodes.
If it's true for node k I'll prove for node k + 1.
I am trying to make a general binary tree but the types won't help me to use induction assumption. I assume maybe I'll have to use what I proved before for child's position.
Any help?
Upvotes: 3
Views: 567
Reputation: 629
So you've proven that the kth node has children at 2k and 2k+1. Then let's divide the children into two cases, even and odd.
For even children they're located at i=2k for some k. You can see that that means its parent is at k, or i/2, or floor(i/2).
For odd children they're located at i=2k+1 for some k. You can see that this means its parent is at k. floor(i/2) in this case equals floor(k+1/2), which equals floor(k) = k since k is an integer, so here the parent node is at floor(i/2) also.
Since the set of all odd and even children make up all children, the parent of the ith child is floor(i/2)
QED? Sorry if this isn't rigorous or formal enough..
Upvotes: 6