DS Trees

Is it possible to implement trees using arrays ? If yes, how?

Questions by lavs_mca

Showing Answers 1 - 10 of 10 Answers

Yes, it is possible.
Here is an example of implementing a binary tree:

Consider an attay : a[n];
Place the root in first position.
and let the current position to be i = 0;
place left node at the position: 2i+1
place right node at the position: 2i+2.
Repeat these steps.

Yes it's possible to implement trees using arrays, root node is place in 0th position.
A node in ith position will have its left child at 2*ith position and right child at 2*i + 1th position.
That's it
It was clearly given in Samanta book.

  Was this answer useful?  Yes

hrshksh

  • May 9th, 2010
 

This questions is ambigious. There are a lot of differences between a tree, k ordered tree, binary tree. There is no harm for any implementation to have array or list as the desired mode of implementation. However, for formula based representation, an array implementation is choosen because of a simple observation.


If a node is at position i then it's left child is 2i and right child 2i+1. This formula assumes that the root node of the B Tree starts as 1. The parent is i/2 if i => 2. 

If the root is placed at position 0 of the array then left child is at 2i+1, and right child at 2i+2. 

A usual formula based representation is in the implementation of binary heap (mix or max)


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions