IF i have this function...

node* get_lowest(node* root)
{
		node* min = root->child[0];

		for(int i = 0; i<root->child.size();i++)
		{
			if(min > root->child[i])
				min = root->child[i];
		}
		cout<<"Here it is:"<<min->data<<endl;
		return min;
}

how do I get it to return the smallest indexed pointer?
for example if root->child[0] and root->child[1] exist how do I get root->child[0] to return?

return_object = get_lowest(root) ?

Recommended Answers

All 6 Replies

Probably you want a pointer which points to the minimal data value. In actual fact it's (as usually) senseless operation to compare pointer values. Moreover, the result of pointers comparison is well-defined only if two pointers points to the same array.

If so, compare data values, not pointers. Save pointer value which refers to the minimal data value. Return this pointer - that's all.

Can you post node type definition? It's hard to say more without this info...

Next time use code tag with the language specifier:
[code=cplusplus] source text

[/code]

Yeah the node type def is this

struct node
{
	vector <node*> childrent;
	int item;
};

I dont want the data inside i want the pointer...

>I dont want the data inside i want the pointer
Which pointer do you want (and why) - that's a question ;)

If you want the minimal pointer value ( why?! ) - you have it. What's a problem? Well, there are two defects in your function:
1. if child (or childrent) size == 0 then root->child[0] raises memory access exception.
2. Start the loop from i = 1.

I want the minimal pointer because it is the left most node for my tree. If i have it how do I get it in a variable like this-

root->children[0] = get_lowest(root) ?

>I want the minimal pointer because it is the left most node for my tree.
The leftmost node in the tree is not the same as the minimal pointer!
Moreover, a pointer value does not bear a relation to the node position in the tree.
Make a loop from the root via child[0] pointers until you have found a leaf - that's a node with an empty child vector. That's the leftmost node of the tree (probably ;))...

Just a hunch, but are you trying to find, among all the non-null pointers in children , the one with the lowest index?

For example assume that the children vector has three elements:

children[0] is null
children[1] is null
children[2] is null

Function should return null?

children[0] is null
children[1] is not null
children[2] is not null

Function should return children[1]?

children[0] is not null
children[1] is not null
children[2] is null

Function should return children[0]?

Is that the idea? I agree with ArkM. Comparing two pointers and deciding which is "less than" the other (presumably by comparing the addresses?) doesn't make a lot of sense.

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.