Click here to Skip to main content
16,010,351 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionClosest Ancestor Pin
NGInd7-Jun-13 4:14
NGInd7-Jun-13 4:14 
Hi all,

I am writing a program to find the closest ancestor in a binary tree (not BST). I found a sample working code:

C#
mynode *closestAncestor(mynode* root, mynode* p, mynode* q)
{
   mynode *l, *r, *tmp;

   if(root == NULL)
   {
      return(NULL);
   }

 if(root->left==p || root->right==p || root->left==q || root->right==q)

   {
     return(root);
   }
   else
   {
      l = closestAncestor(root->left, p, q);
      r = closestAncestor(root->right, p, q);

      if(l!=NULL && r!=NULL)
      {
        return(root);
      }
      else
      {
         tmp = (l!=NULL) ? l : r;
         return(tmp);
      }
   }
}



I am trying to do something like the following (passing only the data values and finding only the data value of ancestor, not concerned with its pointer)

C#
int closestanc(node * root, int n1, int n2)
{
    int l, r;
    if(root == NULL)
    return -1;
    if(root->right->data == n1 || root->right->data == n2 || root->left->data == n1 || root->left->data == n2)
    return root->data;
    else
    {
        l = closestanc(root->left, n1, n2);
        r = closestanc(root->right, n1, n2);
        if(l!= -1 && r!= -1)
        return root->data;
        else
        return (l != -1 ? l : r);
    }
}


But this thing doesn't work. Can you please point out where I am doing it wrong ?
AnswerRe: Closest Ancestor Pin
Roy Heil14-Jun-13 9:58
professionalRoy Heil14-Jun-13 9:58 
QuestionUseage about OPCODE collision detection Pin
Henry Hong4-Jun-13 15:23
Henry Hong4-Jun-13 15:23 
AnswerRe: Useage about OPCODE collision detection Pin
dusty_dex4-Jun-13 18:36
dusty_dex4-Jun-13 18:36 
GeneralRe: Useage about OPCODE collision detection Pin
Henry Hong4-Jun-13 20:03
Henry Hong4-Jun-13 20:03 
GeneralRe: Useage about OPCODE collision detection Pin
Richard MacCutchan4-Jun-13 21:52
mveRichard MacCutchan4-Jun-13 21:52 
GeneralRe: Useage about OPCODE collision detection Pin
SoMad4-Jun-13 22:52
professionalSoMad4-Jun-13 22:52 
GeneralRe: Useage about OPCODE collision detection Pin
Richard MacCutchan4-Jun-13 23:32
mveRichard MacCutchan4-Jun-13 23:32 
GeneralRe: Useage about OPCODE collision detection Pin
dusty_dex4-Jun-13 23:46
dusty_dex4-Jun-13 23:46 
GeneralRe: Useage about OPCODE collision detection Pin
Richard MacCutchan4-Jun-13 23:53
mveRichard MacCutchan4-Jun-13 23:53 
QuestionWeather Prediction using minimal data Pin
nootanghimire16-May-13 23:06
professionalnootanghimire16-May-13 23:06 
AnswerRe: Weather Prediction using minimal data Pin
dusty_dex16-May-13 23:58
dusty_dex16-May-13 23:58 
GeneralRe: Weather Prediction using minimal data Pin
nootanghimire17-May-13 23:50
professionalnootanghimire17-May-13 23:50 
GeneralRe: Weather Prediction using minimal data Pin
dusty_dex18-May-13 2:49
dusty_dex18-May-13 2:49 
GeneralRe: Weather Prediction using minimal data Pin
Dave Kreskowiak29-May-13 18:40
mveDave Kreskowiak29-May-13 18:40 
AnswerRe: Weather Prediction using minimal data Pin
Richard MacCutchan18-May-13 5:42
mveRichard MacCutchan18-May-13 5:42 
AnswerRe: Weather Prediction using minimal data Pin
Joezer BH16-Jun-13 23:29
professionalJoezer BH16-Jun-13 23:29 
QuestionBest autorrelation function Pin
Russell'7-May-13 0:36
Russell'7-May-13 0:36 
QuestionPattern matching in trees, lists and strings alike Pin
bjongejan11-Apr-13 9:19
bjongejan11-Apr-13 9:19 
AnswerRe: Pattern matching in trees, lists and strings alike Pin
dusty_dex11-Apr-13 11:44
dusty_dex11-Apr-13 11:44 
GeneralRe: Pattern matching in trees, lists and strings alike Pin
bjongejan11-Apr-13 22:49
bjongejan11-Apr-13 22:49 
GeneralRe: Pattern matching in trees, lists and strings alike Pin
dusty_dex11-Apr-13 23:04
dusty_dex11-Apr-13 23:04 
GeneralRe: Pattern matching in trees, lists and strings alike Pin
bjongejan12-Apr-13 9:25
bjongejan12-Apr-13 9:25 
GeneralRe: Pattern matching in trees, lists and strings alike Pin
Kosta Cherry5-Jun-13 20:04
Kosta Cherry5-Jun-13 20:04 
QuestionAlgorithm Pin
Member 99636023-Apr-13 15:11
Member 99636023-Apr-13 15:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.