2015-12-27 8 views
6
        8 
           / \ 
           4   12 
          /\  /\ 
          3 6  2 1 
         /\ /\ //\ 
         7 10 13 15 5 9 11 
              /
              14 

Ho bisogno di trovare i nonni di un albero, in questo exemple ho un solo nonno, numero 12 (ho bisogno che lui ha solo due o tre nipoti).In Albero binario, trovare quanti nonni hanno solo due o tre grandchildrens

Questo è ciò che ho provato finora:

int T(struct node * tree){ 
    int t = 0; 
    if (tree == NULL) 
     return 0; 
    if (tree->left && tree->right) 
    { //In this case i check if we NOT have all the four grandchildrens. 
     if (!((tree->left->left) && (tree->left->right) && (tree->right->left) && (tree->right->right))) 
     { 
      t = 1 + T(tree->left) + T(tree->right); 
      T(tree->left); 
      T(tree->right); 

     } 
     else 
     { 
      T(tree->left); 
      T(tree->right); 
     } 

    } 

    return t; 

} 

Purtroppo non funziona ... Qualcuno mi può aiutare con questo?

+0

Stai facendo alcune chiamate ricorsive in cui ignori il valore restituito. – molbdnilo

+0

Come posso risolvere il problema? – gogo

+0

Quanto è equilibrato l'albero? È possibile avere un solo figlio ma due nipoti? Se è così, hai bisogno di un codice aggiuntivo per quel caso (oltre a correggere il bug in quello che hai). – JSF

risposta

3

Un approccio efficace consiste nel restituire ricorsivamente un paio di risultati. Ci sono più eleganti modi per restituire un paio in C++, ma io uso il vecchio modo kludgy C di tornare un uscita tramite un ingresso da puntatore:

int T2(struct node * tree, int* child_count) 
{ 
    int t = 0; // Count the things we are supposed to count 
    int g = 0; // Count grandchildren of the current node 
    if (tree == NULL) 
     return 0; 
    if (tree->left) 
    { 
     ++ *child_count; 
     t += T2(tree->left, &g); 
    } 
    if (tree->right) 
    { 
     ++ *child_count; 
     t += T2(tree->right, &g); 
    } 
    if (g==2 || g==3) 
     ++t; 
    return t; 
} 

int T(struct node * tree) {int dummy; return T2(tree, &dummy); } 

La funzione fa due cose insieme. Il lavoro semplice è che aiuta a contare i nipoti dei suoi genitori incrementando *child_count e anche ricorsivamente fa il lavoro principale accumulando in t.


seguente modo potrebbe essere più facile da capire, ma è meno elegante:

int T(struct node * tree) 
{ 
    struct node *c; 
    int t = 0; // Count the things we are supposed to count 
    int g = 0; // Count grandchildren of the current node 
    if (tree == NULL) 
     return 0; 
    if ((c=tree->left) != NULL) 
    { 
     g += (c->left != NULL) + (c->right != NULL); 
     t += T(c); 
    } 
    if ((c=tree->right) != NULL) 
    { 
     g += (c->left != NULL) + (c->right != NULL); 
     t += T(c); 
    } 
    if (g==2 || g==3) 
     ++t; 
    return t; 
} 
+0

Puoi mostrarmi per favore come faccio a fare ciò che pubblichi senza puntatore int * child_count e just int child_count – gogo

+0

Perché? Stai programmando in C o in C++ e quale regola hai che proibisci usando un 'int *'? – JSF

+0

Oh ok scrivi + t dopo if (g == 2 || g == 3) invece di ++ t – gogo

1

Questo diventa più facile se si introduce un paio di funzioni bambino di conteggio, quella che conta i bambini e uno che conta nipoti:

int children(node* tree) 
{ 
    if (!tree) 
    { 
     return 0; 
    } 
    int count = 0; 
    if (tree->left) 
    { 
     count += 1; 
    } 
    if (tree->right) 
    { 
     count += 1; 
    } 
    return count; 
} 

int grandchildren(node* tree) 
{ 
    if (!tree) 
    { 
     return 0; 
    } 
    return children(tree->left) + children(tree->right); 
} 

int grandparents(node* tree) 
{ 
    if (!tree) 
    { 
     return 0; 
    } 
    int count = grandchildren(tree); 
    if (count == 2 || count == 3) 
    { 
     return 1 + grandparents(tree->left) + grandparents(tree->right); 
    } 
    else 
    { 
     return grandparents(tree->left) + grandparents(tree->right); 
    } 
}