Alright I have seen a bunch of algorithms now, that I don't fully understand. I am trying to count leafs and non-leafs of a binary tree
I have this header struct for a binary tree where pRoot is the root
Code:
typedef struct BST
{
char city[50];
char zipcode[8];
char state[2];
double latitude;
double longitude;
long population;
struct BST *left;
struct BST *right;
struct BST *parent;
}Tree_node;
typedef struct
{
struct BST *pRoot;
int itree_node_count;
int idepth;
int leaves[500];
int non_leaves[500];
}Tree_Header;
I am declaring my fiunction like this
Code:
int Traverse(Tree_Header *psTree, int);
calling it like this
CODE]Traverse(psTree, 0);[/CODE]
and then the function looks like this
Code:
Traverse(Tree_Header *psTree, int ilevel)
{
//visit
if(psTree->pRoot->left == NULL && psTree->pRoot->right == NULL)
{
//increment leaves
psTree->leaves[ilevel]++;
}
else
{
//increment non_leaves
psTree->non_leaves[ilevel]++;
}
Traverse(psTree->pRoot->left, ilevel+1);
Traverse(psTree->pRoot->right, ilevel+1);
return 0;
}
I wish I understood recursion better but I don't . If there is anyone that can help me with this, I would be very thankful because I am tired of beating my head on this keyboard