I have been using ruby for a few months now and am writing a program
that duplicates portions of a tree structure.
For example, take the following tree:
Where ( represents a node, each letter represents a leaf. I am
currently using a very clunky set of arrays to hold all the
leaf = Array.new
node = Array.new
left = Array.new #represents the child to the left
right = Array.new #represents the child to the right
parent = Array.new
I number each element in the array based on moving up the tree and
reading either a left or right node or leaf.
left = i2
right = i2+1
thus node represents the node subtending the edges leading to the
left child A and the right child B.
So to traverse this tree
( = node 1
( = move up a node to 2
A = move up to left leaf 4
, = move down to node 2
B = move up to the right leaf 5
) = move down to node 2
, = move down to node 1
C = move up to right child 3
) = move down to node 1 and end of tree
Using the above tree and information in the array elements, I want to
be able to duplicate portions of the tree, such that I could get
So moving up from node 1 to node 2 I have a duplication in A and B. I
can indicate where I have duplications but ultimately need to write
this back out into the parenthetical notation that I am using to
describe a tree. I am afraid that I should be using struct or they may
be a better way to describe my tree structures rather than using
numbered and linked lists. Any information on using struct in ruby or
pointer equivalents would be appreciated. I am trying to come up with
a way of doing this such that I could use it on trees of any size.
Right now I am using arrays and the bookkeeping is getting tricky.
Thanks for any help!