retagged by
236 views
0 votes
0 votes

A complete binary tree can be stored in an array.

If index starts at $1$, to access the child of  $i^{th}$ node, the  _____$^{th}$ and _____ $^{th}$ index of array needs to be used.
 

  1. $2i - $ and $2i$
  2. $2i$ and $2i + 1$
  3. $2i + 1$ and $2i + 2$
  4. $2i - 1$ and $2i + 1$
retagged by

1 Answer

Best answer
2 votes
2 votes
let a  complete binary tree tree structure is

a(1'st node)

/                \

b(2nd node)   c(3)

/     \

d(4)  e(5)

 

if it is put in an array it can be like in the order as first root,then root's left child,next roots right child ..so on.So the array will be

[a  ---index1(as index starts from 1 as mentioned)

,b, --2

c,--3

d,--4

e--5

].

 

Now  index 2's child(i.2e b' child) are d(index position 4) and e (index position 5)

that means to access node 2's child we need to use index 2*2 and 2*2+1

this is applicable for other nodes.

So if we generalize to access node i's child we need to access nodes 2i and 2i+1
selected by
Answer:

Related questions

1 votes
1 votes
2 answers
1
Bikram asked Feb 9, 2017
289 views
A ternary tree is a tree in which every internal node has exactly three children.The number of leaves in a ternary tree with $’z’$ internal nodes is _______.$2$$\left...
0 votes
0 votes
1 answer
4