B]DATA STRUCTURE IN C

TREES

please help to device an algorithm to compute the size of the sub-tree rooted at each node .

my attemp is

algorithm subtree_size(tree)

if(tree = empty) then
return 0;

else
return 1 +(subtree_size(leftsubtree) +subtree_size(rightsubtree) )
end

B]DATA STRUCTURE IN C

TREES

please help to device an algorithm to compute the size of the sub-tree rooted at each node .

100

commented: -10 -4

What else do you want? It is fairly good. But what you'll get includes the root. So subtract by 1, you'll get the size of the subtree.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.