//checking common ancestor of two nodes a and b struct btnode* common_ancestor(struct btnode * tree,struct btnode * a, struct btnode * b) { struct btnode * temp; if(IsNodeInTree(tree,a) ==1 && IsNodeInTree(tree,b)==1) { if((IsNodeInTree(tree->l,a) ==1 && IsNodeInTree(tree->r,b)==1)||IsNodeInTree(tree->r,a)==1 && IsNodeInTree(tree->l,b)==1) { return tree; } else if(IsNodeInTree(tree->l,a) ==1 && IsNodeInTree(tree->l,b)==1) { temp=common_ancestor(tree->l,a,b); return temp; } else if(IsNodeInTree(tree->r,a) ==1 && IsNodeInTree(tree->r,b)==1) { temp=common_ancestor(tree->r,a,b); return temp; } } else { return NULL; } }
void test_tree() { //中序遍历为:123654 boost::shared_ptr<tree> node1 = boost::shared_ptr<tree>(new tree(1)); boost::shared_ptr<tree> node3 = boost::shared_ptr<tree>(new tree(3)); boost::shared_ptr<tree> node4 = boost::shared_ptr<tree>(new tree(4)); boost::shared_ptr<tree> node2 = boost::shared_ptr<tree>(new tree(2)); node2->left = node1; node2->right = node3; boost::shared_ptr<tree> node6 = boost::shared_ptr<tree>(new tree(6)); node6->left = node2; boost::shared_ptr<tree> node5 = boost::shared_ptr<tree>(new tree(5)); node6->right = node5; node5->right = node4; mid_travel(node6); boost::shared_ptr<tree> test1=common_ancestor(node6, node1, node3); cout << test1->data << endl; boost::shared_ptr<tree> test2=common_ancestor(node6, node3, node4); cout << test2->data << endl; boost::shared_ptr<tree> test3 = common_ancestor(node6, node4, node5); cout << test3->data << endl; boost::shared_ptr<tree> test4 = common_ancestor(node6, node2, node3); cout << test4->data << endl; }
int main() { struct btnode *one,*two,*three,*four,*five,*six,*seven,*temp; one=initNode(1); two=initNode(2); three=initNode(3); four=initNode(4); five=initNode(5); six=initNode(6); root=one; //one node outside tree seven=initNode(7); //set the tree one->l=two; one->r=three; three->l=four; three->r=six; four->l=five; //Checking if node belongs to the tree printf("node four in tree:%d\n",IsNodeInTree(root,four)); printf("node six in tree:%d\n",IsNodeInTree(root,six)); //checking what is common ancestor of node 5 and 6 temp=common_ancestor(root,five,six); if(temp != NULL) { printf("Common ancestor of 5 and 6 is:%d\n",temp->val); } else printf("Common ancestor does not exist!\n"); //checking what is common ancestor of node 3 and 7 temp=common_ancestor(root,three,seven); if(temp != NULL) { printf("Common ancestor of 3 and 7 is:%d\n",temp->val); } else printf("Common ancestor does not exist!\n"); }
static void primitive_invoke_continuation (rep_continuation *c, repv ret) { char water_mark; rep_barrier **dest_hist = 0, *dest_root = 0, *anc, *ptr; int depth; /* try to find a route from the current root barrier to the root barrier of the continuation, without crossing any closed barriers */ dest_root = FIXUP (rep_barrier *, c, c->barriers); dest_hist = alloca (sizeof (rep_barrier *) * dest_root->depth); depth = trace_barriers (c, dest_hist); anc = common_ancestor (barriers, dest_hist, depth); if (anc == 0) { DEFSTRING (unreachable, "unreachable continuation"); Fsignal (Qerror, rep_LIST_1 (rep_VAL (&unreachable))); return; } /* Handle any `out' barrier functions */ for (ptr = barriers; ptr != anc; ptr = ptr->next) { DB (("invoke: outwards through %p (%d)\n", ptr, ptr->depth)); if (ptr->out != 0) { repv cont = rep_VAL (c); rep_GC_root gc_cont, gc_ret; rep_PUSHGC (gc_cont, cont); rep_PUSHGC (gc_ret, ret); ptr->out (ptr->data); rep_POPGC; rep_POPGC; } } /* save the return value and recurse up the stack until there's room to invoke the continuation. Note that invoking this subr will already have restored the original environment since the call to Fmake_closure () will have saved its old state.. */ invoked_continuation = c; invoked_continuation_ret = ret; invoked_continuation_ancestor = anc; DB (("invoke: calling continuation %p\n", c)); grow_stack_and_invoke (c, &water_mark); }