2015-12-31 168 views
0

我在添加節點到搜索樹時遇到問題。我有很多錯誤,比如「指針和整數之間的比較」和「期望的char,但參數的類型是char *」。這裏是我的代碼:將節點添加到樹問題

void addNode(WordTreeNode ** tree, char tok) 
{ 
    WordTreeNode *temp = NULL; 
    if(!(*tree)) 
    { 
     temp = (WordTreeNode *)malloc(sizeof(WordTreeNode)); 
     temp->leftChild = temp->rightChild = NULL; 
     temp->name = tok; 
     *tree = temp; 
     return; 
    } 

    if(tok < (*tree)->name) 
    { 
     addNode(&(*tree)->leftChild, tok); 
    } 
    else if(tok > (*tree)->name) 
    { 
     addNode(&(*tree)->rightChild, tok); 
    } 

} 
+1

你能告訴我你的成員'name'的類型嗎? – Rabbid76

+0

'if(tok < (*tree)-> name)'看起來很腥......'if(tok < *((*tree)-> name))',也許? –

+0

也許'char tok' - >'char * tok',並改爲使用'strcmp'進行比較。 – BLUEPIXY

回答

1

我覺得你meber namechar *類型。使用strcmp而不是<>char*而不是char。將您的代碼更改爲:

void addNode(WordTreeNode ** tree, char * tok) 
            //^
{ 
    WordTreeNode *temp = NULL; 
    if(!(*tree)) 
    { 
     temp = (WordTreeNode *)malloc(sizeof(WordTreeNode)); 
     temp->leftChild = temp->rightChild = NULL; 

     temp->name = malloc(strlen(tok)+1); 
     strcpy(temp->name, tok); 
     // don't forget to free the memory when youe destroy the node 

     *tree = temp; 
     return; 
    } 

    if(strcmp(tok, (*tree)->name) < 0) 
    // ^^^^^^ 
    { 
     addNode(&(*tree)->leftChild, tok); 
    } 
    else if (strcmp(tok, (*tree)->name) > 0) 
      // ^^^^^^ 
    { 
     addNode(&(*tree)->rightChild, tok); 
    } 
} 
+0

成員名稱是char類型。謝謝! – Trex00