2015-12-31 161 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