/* Ancestor Tree by Raymond Le Grand */ #include #include using namespace std; const int LEFT=1; const int RIGHT=2; //tree nodes struct node{ int number; struct node *left; struct node *right; struct node *parent; }; node *base; node * create_node(int v) { node *n = (node *)malloc(sizeof(node)); n->left = n->right = NULL; n->number = v; return (n); } void insert_node(node * &ptr, int v){ if (ptr==NULL){ ptr=create_node(v); if (base==NULL){ base=ptr; cout<<"base is "<number; } }else if (ptr->number>v){ insert_node(ptr->right,v); }else{ insert_node(ptr->left,v); } } int search_node(node * ptr, int v){ if (ptr==NULL) return -1; if (ptr->number==v){ return 0; }else if (ptr->number>v){ return(RIGHT); }else{ return(LEFT); } } int find_ancestor(node * ptr, int num1, int num2){ int temp; //at each step,store point at which they diverge //case 1 where they are both the same temp=search_node(ptr,num1); if (temp==search_node(ptr,num2)){ //if both=#, then proceed down tree if (temp==RIGHT){ return find_ancestor(ptr->right,num1,num2); } if (temp==LEFT){ return find_ancestor(ptr->left,num1,num2); } } //case 2 both=0, then return that number //case 3 where num(1 or 2) is @ that node, num2 keeps going, so return //case 4 where num1 and num2 branch in different directions, so return if (ptr==NULL){ cout<<"error"; return 0; } return ptr->number ; } void delete_tree(node * &ptr){ if (ptr->left!=NULL) delete_tree(ptr->left); if (ptr->right!=NULL) delete_tree(ptr->right); free (ptr); ptr=NULL; return; } int main(){ int count,num,num2; int i=0; while(true){ cin>>count; if (count==(-1)){ break; }else if (i!=0){ cout<>num; insert_node(base,num); } //get pairs and find lowest common ancestor cin>>count; for (i=0;i>num>>num2; cout<