0

Hey guys,
The question on the homework is
"Write a function that returns a count of the nodes that contain a value less than the parameter value."

I was wondering if someone could point me in the right direction maybe with some pseudo code or something of the like. ive got

int lessNodeCount(int value) const
{
	int count = 0;//count for the number of nodes to be updated

Im just confused about whether it should be done recursively or not. I know i have to find the value in the tree, move to the left child and count every node stemming from that left child. Im just not sure how to accomplish that.

1
Contributor
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Howdydoody
0

I figured out how to count the nodes recursively. Im still not sure how to find the node i want. The contains function only returns a true false value and not the pointer to the node for me to be able to use as node1 in the following code.

int lessNodeCount(int value) const
{
	if(node1 == NULL)
		return 0;
	return lessNodeCount(node1->left) + lessNodeCount(node1->right) + 1;
}
0

i could modify the contains method of the binary search tree class to return the pointer to the node instead of a true false value so i could perform the recursive statement on that node specifically.

BinaryNode *node1 = sTree.contains(value);
int lessNodeCount(BinaryNode *node1->left) const
{
	if(node1 == NULL)
		return 0;
	return lessNodeCount(node1->left) + lessNodeCount(node1->right) + 1;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.