因此,此代码搜索树中的字符串之一(按字符串数组输入)。搜索无法正常进行,因为printf(“%p”,treePtr)显示treePtr有一个地址,但由于某种原因立即变为0x0,因此返回null。
Output:
0x7f9c50c02680
0x0
我尝试了不同的组合,例如:&treePtr和* treePtr,但它们不起作用。
/*
(Binary Tree Search)
Write function binaryTreeSearch that attempts to locate a specified
value in a binary search tree. The function should take as arguments a pointer to the root node of
the binary tree and a search key to be located. If the node containing the search key is found, the
function should return a pointer to that node; otherwise, the function should return a NULL pointer.
*/
//ANS:
/* Exercise Solution */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
/* TreeNode structure definition */
struct TreeNode
{
struct TreeNode *leftPtr; /* pointer to left subtree */
char data[9]; /* node data */
struct TreeNode *rightPtr; /* pointer to right subtree */
}; /* end struct TreeNode */
typedef struct TreeNode TreeNode;
typedef TreeNode *TreeNodePtr;
/* function prototypes */
void insertNode (TreeNodePtr * treePtr, char value[]);
TreeNodePtr binaryTreeSearch (TreeNodePtr treePtr, char key[]);
int main (void)
{
int i;
const char * item[] = {"cea", "riz", "mac", "roz", "bee", "lea", "tee", "pee", "see"}; /* loop counter */
char searchKey[3]; /* value to search for */
TreeNodePtr rootPtr = NULL; /* points to the tree root */
TreeNodePtr searchResultPtr; /* pointer to search result */
printf ("The strings being placed in the tree are:\n");
for (i = 0; i <= 8; i++)
{
printf ("%s\t", item[i]);
insertNode (&rootPtr, item[i]);
} /* end for */
/* prompt user and read integer search key */
printf ("\n\nEnter a string to search for: ");
scanf ("%s", searchKey);
printf ("%s", searchKey );
searchResultPtr = binaryTreeSearch (rootPtr, searchKey);
/* if searchKey not found */
if (searchResultPtr == NULL)
{
printf ("\n%s was not found in the tree.\n\n", searchKey);
} /* end if */
else
{ /* if key found */
printf ("\n%s was found in the tree.\n\n", searchResultPtr->data);
} /* end else */
getchar();
getchar();
return 0; /* indicate successful termination */
} /* end main */
/* insert a node into the tree */
void insertNode (TreeNodePtr * treePtr, char value[])
{
/* if treePtr is NULL */
if (*treePtr == NULL)
{
/* dynamically allocate memory */
*treePtr = malloc (sizeof (TreeNode));
/* if memory was allocated, insert node */
if (*treePtr != NULL)
{
strcpy((*treePtr)->data, value);
(*treePtr)->leftPtr = NULL;
(*treePtr)->rightPtr = NULL;
} /* end if */
else
{
printf ("%s not inserted. No memory available.\n", value);
} /* end else */
} /* end if */
else
{ /* recursively call insertNode */
/* insert node in left subtree */
if (value < (*treePtr)->data)
{
insertNode (&((*treePtr)->leftPtr), value);
} /* end if */
else
{
/* insert node in right subtree */
if (value > (*treePtr)->data)
{
insertNode (&((*treePtr)->rightPtr), value);
} /* end if */
else
{ /* duplicate value */
printf ("dup");
} /* end else */
} /* end else */
} /* end else */
} /* end function insertNode */
/* search for key in tree */
TreeNodePtr binaryTreeSearch (TreeNodePtr treePtr, char key[])
{
/* traverse the tree inOrder */
if (treePtr == NULL)
{
printf("%p\n", treePtr);
return NULL; /* key not found */
} /* end if */
else if (treePtr->data == key)
{
return treePtr; /* key found */
} /* end else if */
else if (key < treePtr->data)
{
return binaryTreeSearch (treePtr->leftPtr, key); /* search left */
} /* end else if */
else // (key > treePtr->data)
{
return binaryTreeSearch (treePtr->rightPtr, key); /*search right */
} /* end else if */
} /* end function binaryTreeSearch */
最佳答案
if (value > (*treePtr)->data)
这不是在C中比较字符串的方法。此代码将比较字符串的地址,而不是字符串的实际值。要比较字符串,必须使用strcmp函数。例如,要测试一个字符串是否比另一个大(按字典顺序),请使用
strcmp
像这样:if (strcmp(string1, string2) > 0)
这意味着
string1
在字典上大于string2
。浏览您的代码并更新比较以使用strcmp
。线
char searchKey[3];
可能会导致错误。
searchKey
末尾没有足够的空间容纳空终止符。确保将3更改为4。最后但并非最不重要的一点是,
scanf
不是获取用户输入的安全方法。它可能导致缓冲区溢出。我建议使用fgets。这样,您可以指定所需的输入量。fgets(searchKey, sizeof(searchKey), stdin);