Last active
August 26, 2017 19:22
-
-
Save lironsade/985a03694f5ddc50e73e10cc6824ee8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* implement with DFS | |
*/ | |
pNode getBest(pNode head, getNodeChildrenFunc getChildren, \ | |
getNodeValFunc getVal, freeNodeFunc freeNode, copyNodeFunc copy, unsigned int best) | |
{ | |
/* head */ | |
pNode* childs; | |
unsigned num_of_childs = getChildren(head, childs); | |
/* node */ | |
pNode node = NULL; | |
unsigned int node_val; | |
/* best */ | |
pNode* best = NULL; | |
unsigned int best_p_node_val = 0; | |
for(int i = 0; i < num_of_childs; i++) | |
{ | |
node = getBest(head[i], getChildren, getVal, freeNode, copy, best); | |
node_val = getVal(node); | |
if(node_val == best) | |
{ | |
best = node; | |
i++; | |
break; | |
} | |
else if(node_val > best_p_node_val) | |
{ | |
freeNode(best); | |
best = node; | |
best_p_node_val = node_val; | |
} | |
else | |
{ | |
freeNode(node); | |
} | |
} | |
if(getVal(head) == best) | |
{ | |
freeNode(best); | |
best = head; | |
} | |
// free rest of children if broke mid-way | |
for (; i < num_of_childs ; ++i) | |
{ | |
freeNode(childs[i]); | |
} | |
free(childs); | |
return best; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment