2016-03-29 29 views
-1

Ich machte einen Code ist die Eingabe von Werten zu einem Binary-Tree durch eine Insert-Funktion nodeT * insertT (nodeT ** pp, int iValue). Ohne Bedeutung der Reihenfolge der Werte fügt meine Funktion die Werte der Reihe nach in den Baum ein, vermeidet Duplikate und druckt korrekt.Binär-Baum-Code wird nicht richtig funktionieren

Insert Values for Binary Tree(-1 to stop)4 6 8 10 12 14 16 -1 
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest 
5 
the smallest 5th element is: 12 
count of node: 7 

Allerdings, wenn ich Werte Eingang nicht bestellt, die Funktionen CountNode und KTH kleinste Element wird der Wunsch Ausgang nicht zurück. Außerdem ist die Anzeige des Binärbaums korrekt. Für das Beispiel

Insert Values for Binary Tree(-1 to stop)10 14 6 8 12 4 16 -1 
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest 
5 
the smallest 5th element is: 0 
count of node: 3 

ich die folgenden Funktionen verwendet, insert, k_smallest

nodeT *insertT(nodeT **pp, int iValue) 
{ 

    if(*pp == NULL) 
    { 
     *pp = allocateNodeT(iValue); 
     return *pp; 
    } 
    if(iValue == (*pp)->iValue) 
    { 
     return *pp; 
    } 

    if(iValue < (*pp)->iValue) 
    { 
     return insertT(&(*pp)->pLeft, iValue); 
    } 
    else //(iValue > (*pp)->iValue) 
    { 
     return insertT(&(*pp)->pRight, iValue); 
    } 

} 
int k_smallest(nodeT *pRoot, int iKey) 
{ 
    int iSave; 
    if(pRoot) 
    { 
     nodeT* pTraverse; 
     pTraverse =pRoot; 
     while(pTraverse) 
     { 
      if((pTraverse->iCount +1) == iKey) 
      { 
       iSave = pTraverse->iValue; 
       break; 
      } 
      else if(iKey > pTraverse->iCount) 
      { 
       iKey = iKey-(pTraverse->iCount +1); 
       pTraverse = pTraverse->pRight; 
      } 
      else 
      { 
       pTraverse = pTraverse->pLeft; 
      } 
     } 
    } 
    return iSave; 
} 
+0

Wäre es möglich, dass Sie uns den Code für den Binärbaum und die Knoten zeigen? – jrhee17

+0

@ jrhee17 Ich habe den Rest des Codes eingereicht –

Antwort

0

Dieser Code unten ist die Lösung, die ich ein BST zu schaffen gemacht. Es läuft ohne Fehler und ich glaube, dass der Code den Speicher richtig zuweist. Allerdings, wie ich in der Frage sagte, wenn ich nicht in Reihenfolge Werte eingeben und rufen Sie die Funktion k_smallest und height die Ausgabe ist anders als wenn ich die Werte in Reihenfolge eingeben (ich habe ein Beispiel in der ersten Post).

int main(int argc, char*argv[]) 
    { 
     char szKey; 
     int iValue, iStop, iKey, iKth, iCount, iSumPath, iK; 
     iStop = -1; 
     nodeT *t1, **t2; 
     t1 = NULL; 
     t2 = &t1; 
    Tree tree = newTree(); 

    //Ask for values for binary tree and stop with -1 
    printf("Insert Values for Binary Tree(-1 to stop)"); 
    do 
    { 
     scanf("%d", &iValue); 
     if(iValue > 0) 
     { 
      insertT(t2,iValue); 
     } 
    }while(iValue != iStop); 

    // Show contents of the tree callinf function DisplayTree 
    printf("The contents of the tree are: \n"); 
    prettyPrintT(t1, 0); 

    //input a number within range of tree for kth smallest 
    printf("\nInput a number whitin the range of the tree to receive the kth Smallest\n"); 
    scanf("%d", &iKey); 
    iKth = k_smallest(t2, iKey); 
    //Print the kth smallest element 
    printf("the smallest %dth element is: %d \n", iKey, iKth); 

    //Count the nodes 
    iCount = iheight(t2); 
    printf("count of node: %d\n", iCount); 

    printf("\n"); 
    freeNode(t2); 
} 
    /******************** allocateNodeT ******************************************** 
     NodeT *allocateNodeT(Element value)) 
     Purpose: 
      This function allocates a binary tree node, assigns the element value, 
      and initializes the pointers to NULL. 
      It returns a pointer to the newly allocated node.. 
     Parameters: 
      I Element value value where address is search. 
     Notes: 

     Return Value: 
      pNew 
     **********************************************************************/ 
     nodeT *allocateNodeT(int iValue) 
     { 
      nodeT *pNew = (nodeT*) malloc(sizeof(nodeT)); 
      pNew->iValue = iValue; 
      pNew->pLeft = NULL; 
      pNew->pRight =NULL; 
      return pNew; 
     } 
     /******************** insertT ******************************************** 
     nodeT *insertT(nodeT **pp, int iValue)) 
     Purpose: 
      insert iValue into the tree recursively 
     Parameters: 
      I nodeT *pp     Specifies the Root 
      I int iValue    value to be inserted on the tree. 
     Notes: 

     **********************************************************************/ 
     nodeT *insertT(nodeT **pp, int iValue) 
     { 

      if(*pp == NULL) 
      { 
       *pp = allocateNodeT(iValue); 
       return *pp; 
      } 
      if(iValue == (*pp)->iValue) 
      { 
       return *pp; 
      } 

      if(iValue < (*pp)->iValue) 
      { 
       return insertT(&(*pp)->pLeft, iValue); 
      } 
      else //(iValue > (*pp)->iValue) 
      { 
       return insertT(&(*pp)->pRight, iValue); 
      } 

     } 
+1

Bitte fügen Sie Text hinzu, um zu klären, ob dies eine Lösung für die Frage ist, die Sie gestellt haben, oder geben Sie mehr Code als Teil der Frage ein? Wenn Sie als Antwort auf einen Kommentar mehr Code posten, bearbeiten Sie die ursprüngliche Frage und fügen Sie sie hinzu, anstatt sie in einer Antwort zu posten. – gariepy

+0

@gariepy ist dieser Text es besser erklären? –

Verwandte Themen