You speak for findTree but you give insertTree. Insert a tree where? The insertTreeNode makes sense, but insertTree not that much. It would make sense creating a Tree, but that is just declaring a variable TreeT.
If you want to insert a node you could do:
Code:
insertNode(TreenNodeT pos, TreeInfoT info)
{
pos = malloc(sizeof(*pos)); //best way to use malloc
pos->R = pos->L = NULL;
pos->info = info;
}
or use any logic in order to insert a node
this
Code:
TreeInfoT findTree(TreeT, TreeInfoT);
needs a bit clarification. Why would you return a TreeInfoT? What is that info exactly? Maybe you mean
Code:
TreeNodeT findTree(TreeT, TreeInfoT);
In this case you can simply search the whole tree recursively. Like:
Code:
TreeNodeT findTree(TreeT tree, TreeInfoT info)
{
TreeNodeT root= tree->root;
return findNode(root, info);
}
and
Code:
TreeNodeT findNode(TreeNodeT node, TreeInfoT info)
{
TreeNodeT found;
if (node->info == info)
return node;
if (node->L) {
found = findNode(node->L);
if (found) return found;
}
if (node->R) {
found = findNode(node->R);
if (found) return found;
}
return NULL;
}
or sth like this (maybe not correct). It will search the node itself for info. If it exists it returns. If note it repeats the process for L and R nodes. At any point, if a findNode succeds, it returns the node.
Think findTree as the initialization function for findNode. Instead of doing
Code:
findNode(tree->root, info);
you can do
Code:
findTree(tree, info);
since it is a requirement