- #1
Of Mike and Men
- 54
- 3
Hey all,
So, I'm working on heaps, and looking at various implementations of heaps, I find that a lot of people us bool returning function for return (2*i+1 > n), where i is an index of a node stored in an array, and n is the size of the array. (2*i+1 is the formula to get the left child's position in the array). The function returns true if it is a leaf, false if it is not a leaf.
However, doesn't using 2*i+1 >= n make more sense? The case of 2*i+1 > n doesn't cover if i is your root node (IE 0), and n is only 1. Since 2(0)+1 = 1 > 1 returns false. Also by definition a node is a leaf if it has no children. I know this is kind of a special case, but it seems to be more consistent with definition of a leaf.
I guess I'm a little confused on what the proper definition would be symbolically.
So, I'm working on heaps, and looking at various implementations of heaps, I find that a lot of people us bool returning function for return (2*i+1 > n), where i is an index of a node stored in an array, and n is the size of the array. (2*i+1 is the formula to get the left child's position in the array). The function returns true if it is a leaf, false if it is not a leaf.
However, doesn't using 2*i+1 >= n make more sense? The case of 2*i+1 > n doesn't cover if i is your root node (IE 0), and n is only 1. Since 2(0)+1 = 1 > 1 returns false. Also by definition a node is a leaf if it has no children. I know this is kind of a special case, but it seems to be more consistent with definition of a leaf.
I guess I'm a little confused on what the proper definition would be symbolically.