我在向搜索树添加节点时遇到问题。我有很多错误,如"指针和整数之间的比较"和"期望的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);
}
}
答案 0 :(得分:1)
我认为您的会员name
属于char *
类型。使用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);
}
}