From: Shashank Agarwal Date: 2008-06-30T08:28:38+09:00 Subject: Re: data structure Vandana wrote: > Hello All, > > I would like to implement a tree with the following properties. > > 1. The tree is balanced. > 2. Each node has a max of 5 sub nodes and min of ceil(5/2) sub nodes. > 3. The tree remains static. Number of nodes known from the beginning. > > How would I implement this in ruby? > > Thanks, > Vandana. If the number of nodes are known, then an array based implementation would be better. So basically, arr[0] is the root. Since it has maximum 5 sub nodes, index 1-5 are the roots children, 6-10 are array[1]'s children and so on. The function to find node x's (five) children then would be x*5 + 1, x*5 + 2, ..., x*5 + 5. Similarly, floor((x - 1) /5) will be the parent's node. Do check the function coz I made them as I typed. -- Posted via http://www.ruby-forum.com/.