Hi again guys,

I'm back with another question, this time concerning tree structures.

My tree is a binary tree and the nodes are struct as follows:

Code:

struct Node {
int info;
struct Node *parent;
struct Node *right;
struct Node *left;
};

So, I need to write this function Search that return the node required.

The strategys that I have to use are:

-Postorder visit

-Preorder visit

-Symmetrical

For now I'm trying to use the first strategy.

Then, I have already written this two codes both working:

Code:

int main(int argc, char **argv) {
...
}
//first node is tree root
struct Node *Search(struct Node *node, int i) {
if (!node)
return NULL;
if (node->info == i)
return node;
struct Node *l = Search(node->left, i);
struct Node *r = Search(node->right, i);
if (l)
return l;
else
return r;
}

Code:

bool found = false;
struct Node *s;
int main(int argc, char **argv) {
...
}
//first node is tree root
void Search(struct Node *node, int i) {
if (!node)
return NULL;
if (node->info == i) {
found = true;
s = node;
}
if (!found) {
Search(node->left, i);
Search(node->right, i);
}
found =false;
}

The first code is my first implementation, works fine but, is not efficient because even if it find the node, the procedure seems continue until the end of tree.

So, I've written the 2nd code, that can stops when find the node but it, istead, looks really horrible for me!

My question is:

There are other ways more intelligent and more beautiful to see, to implement my 2nd code? Or maybe there are other approaches that I have not considerate?