# Trees on the brain

I need to write a procedure to build a binary tree using arrays where the data item is string[10]. I also need a procedure to build a binary tree  using pointers where the data item is string[10]. Any suggestions / algorithms?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
I do not understand the bintree with arrays but with pointers here is the idea:

type
PNode = ^TNode;
TNode = record
data : string[10];
L,R  : PNode; // left and right childs of node
end;

var
Root : TNode; //the root node for our tree

Now, please list what methods you want for this tree. e.g. adding item, removing item, balancing the tree etc...

Igor
0
Commented:
You can use an array if you plan on having a balanced tree.  For example, if you want the tree:

....1
.../.\
..2...3
./\.../\
4.5..6..7

Then you can have an array like this:

1 | 2 | 3 | 4 | 5 | 6 | 7

To find any node's left child, multiply it by 2.  To find it's right child, multiply it by two and add 1.  If you have an unchanging, balaced array that you need to perform a lot of searches on, this way is quicker than using pointers.

0
Commented:
binary tree using pointers :
type node = ^item ; item = record  data : string[10]; l,r : node; end;
the function for insertion is
function insert (root : node, toinsert : string):node ;
begin  if root=nil then begin root^.data := toinsert ; root^.l := nil; root^.r := nil; end
else if root^.data > toinsert then root^.left := insert(root^.left, toinsert)
else root^.right := insert (root^.right, toinsert);
insert := root ; end ;
0

Experts Exchange Solution brought to you by