Build Balanced Tree

A

alexbarham

Hello,

I am working through Mastering Algorithms with PERL. I am reading about
binary trees and how to keep them balanced. The example code shows tree
manipulation but doesn't show how to build the tree. I would like to
try out the sample code. However, I don't quite understand how to build
a binary tree. If I wanted to build a tree with one left node having a
value of 1 and a right node with a value of 2, how would I do it? Is it
a hash of hashes as in?:

my %tree = {
{left=>0,
right=>1,
val=>1}
left=>1,
right=>0,
val=>2}
};
Is it an array of array?
my @tree = [[0,0,1],[1,0,2]];

Or is it more flexible than this. Any help would be greatly appreciated!
 
X

xhoster

Hello,

I am working through Mastering Algorithms with PERL. I am reading about
binary trees and how to keep them balanced. The example code shows tree
manipulation but doesn't show how to build the tree. I would like to
try out the sample code.

What sample code? From what I can tell through Google, that book appears
to have several pieces of sample code.
However, I don't quite understand how to build
a binary tree.

You create an empty tree, and then you insert thing into it. Do they
provide the code for doing these two thing?

If I wanted to build a tree with one left node having a
value of 1 and a right node with a value of 2, how would I do it?

Generally, you wouldn't. You insert a 1 and 2 into the tree, and they
get placed where the algorithm places them. (Aside from which, you have a
3 node tree but only specified the values of 2 of the nodes.)
Is it
a hash of hashes as in?:

It could be a hash of hashes.
my %tree = {
{left=>0,
right=>1,
val=>1}
left=>1,
right=>0,
val=>2}
};

That is wrong on so many levels. It has syntax errors, and you are
confusing {} with ().

Somehting more like this:
my $tree_ref = {
val => 'unknown',
left => {
val => '1',
},
right => {
val => '2',
}
};



Is it an array of array?

It could also be that.
my @tree = [[0,0,1],[1,0,2]];

This is wrong on less levels, but still wrong. You need three nodes (root,
left, right), but you only have two.

Anyway, if you look at the specific piece of sample code that you are
interested in, it should be obvious what the representation is.
Or is it more flexible than this.

If they are to be balanced, they probably have to be more flexible than
this.

Xho
 
A

Andrew McGregor

I am working through Mastering Algorithms with PERL. I am reading about
binary trees and how to keep them balanced. The example code shows tree
manipulation but doesn't show how to build the tree. I would like to
try out the sample code. However, I don't quite understand how to build
a binary tree. If I wanted to build a tree with one left node having a
value of 1 and a right node with a value of 2, how would I do it? Is it
a hash of hashes as in?:

Use sub basic_tree_add to build the tree, page 77.
my %tree = {

If you want to pre-declare the tree, again build it as above, then use
Data::Dumper to see the structure.

die Data::Dumper $tree;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top