Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions data_structures/binary_trees/binary_search_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,27 @@ void inOrder(node *root)
inOrder(root->right);
}
}
/** Recursive utilitary procedure to find number of leaf nodes of the binary tree
* @param root pointer to parent node
* @returns 1 if node is a leaf
* @returns sum of number of leaves in left and right branches
* @returns 0 if tree is empty
*/
int leafcount(node *root)
{
int l,r;
if(root!=NULL)
{
if((root->left==NULL)&&(root->right==NULL))
return 1;
l=leafcount(root->left);
r=leafcount(root->right);
return(l+r);
}
return 0;
}

/** Main funcion */
/** Main function */
int main()
{
// this reference don't change.
Expand All @@ -259,7 +278,7 @@ int main()
{
printf(
"\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get "
"current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
"current Height\n[5] Print Tree in Crescent Order\n[6] Number of leaf nodes\n[0] Quit\n");
scanf("%d", &opt); // reads the choice of the user

// processes the choice
Expand Down Expand Up @@ -298,6 +317,9 @@ int main()
case 5:
inOrder(root);
break;
case 6:
printf("The number of leaf nodes in the tree is: %d\n", leafcount(root));
break;
}
}

Expand Down