A $$\texttt{Generic Tree}$$ can be represented as follows:
The tree node declaration for general trees can be given as:
typedef ... element;
typedef struct node
{
element data;
struct node *firstChild;
struct node *nextSibling;
} *GenericTree;
- Write a function that returns the number of nodes in a $$\texttt{Generic Tree}$$.
Difficulty level
Video recording
This exercise is mostly suitable for students
int CountNodes(GenericTree GT)
{
if(!GT) return 0;
return 1 + CountNodes(GT->firstChild) + CountNodes(GT->nextSibling);
}
int SumNodes(GenericTree GT)
{
if(!GT) return 0;
return GT->data + CountNodes(GT->firstChild) + CountNodes(GT->nextSibling);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Picking up sticks