bool compareTrees(Node *r1, Node *r2) { if(r1->n != r2->n) { std::cout << "error on nodes " << r1->n << " and " << r2->n << std::endl; return false; } if(r1->children.size() != r2->children.size()) return false; for (int i = 0; i < r1->children.size(); i++) { return compareTrees(r1->children[i], r2->children[i]); } return true; }
//return a tree resulted of applying resolution rule tree resolution(tree t1, tree t2, tree model, int axiom_id) { tree result = malloc(sizeof(tree_node)); int i; bool temp = model -> childs[0] -> negative; n_resolutions++; //change to match if trees are equal model -> childs[0] -> negative = !model -> childs[0] -> negative; //head of the tree strcpy(result -> label, "@"); result -> n_childs = 0; result -> axiom_id = axiom_id + 1; //copy to the result tree all the clauses that dont start with the label for(i = 0; i < t1 -> n_childs; i++) { if(!compareTrees(t1 -> childs[i], model)) { result -> childs[result -> n_childs] = copyTree(t1 -> childs[i], NULL); result -> childs[result -> n_childs] -> axiom_id = axiom_id + 1; result -> n_childs++; } } for(i = 0; i < t2 -> n_childs; i++) { if(!compareTrees(t2 -> childs[i], model)) { result -> childs[result -> n_childs] = copyTree(t2 -> childs[i], NULL); result -> childs[result -> n_childs] -> axiom_id = axiom_id + 1; result -> n_childs++; } } model -> childs[0] -> negative = temp; return result; }
// some comparison function for these trees int compareTrees(void*p1,void*p2){ Tree*t1 = p1; Tree*t2 = p2; int max_n_children; int i; int ret_val; if(t1->value > t2->value){ return 1; }else if(t2->value > t1->value){ return -1; } max_n_children = MAX(t1->num_children,t2->num_children); for(i = 0; i < max_n_children;i++){ ret_val = compareTrees(t1->children[i],t2->children[i]); if(ret_val != 0) return ret_val; } return (t1->num_children < t2->num_children) ? -1 : ((t1->num_children > t2->num_children) ? 1 : 0); }