我正在编写程序的一部分,在该程序中遍历二叉树,直到在树中找到想要的项目为止(假设我们要查找的项目始终存在于树中)。我在树中搜索的方法是预定的树遍历。
每个节点包含一个问题或一个陈述。如果节点为statement,则它没有子节点。但是,如果该节点是一个问题,则它恰好有两个孩子。在下面的内容中,您可以看到用于创建节点的头文件。
// two type of branch in the tree
enum response{
YES,
NO
};
// content of a node
union objInfoOrQInfo {
char * object;
char * question;
};
struct node{
union objInfoOrQInfo container;
// enum response existLeftChild;
// enum response existRightChild;
struct node * rightChild;
struct node * leftChild;
};
void nodePrint(char * string);
在下面的代码中,我填充了一棵树并实现了预定的树遍历
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "node.h"
struct node * traverse(struct node * searchingNode, char * searchContent);
void nodePrint(char * string)
{
struct node root;
struct node left;
struct node right;
//second level
struct node leftLeft;
struct node leftRight;
struct node rightLeft;
struct node rightRight;
// Populating the tree
root.container.question = "Does it have a tail?";
root.leftChild = &left;
root.rightChild = &right;
left.container.question = "Does it like a chase mice?";
left.leftChild = &leftLeft;
left.leftChild = &leftRight;
right.container.question = "Is it flat, round and edible?";
right.leftChild = &rightLeft;
right.rightChild = &rightRight;
leftLeft.container.object = "A Cat";
leftLeft.leftChild = NULL;
leftLeft.rightChild = NULL;
leftRight.container.object = "A Pangolin";
leftRight.leftChild = NULL;
leftRight.rightChild = NULL;
rightLeft.container.object = "A Pizza";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
rightRight.container.object = "Pete";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
// It needs to traverse from the root until it finds the content you are asking for
struct node * result = traverse(&root, string);
if(result->leftChild != NULL || result->rightChild != NULL)
{
printf("Object: %s\n","[NOTHING]");
printf("Question: %s\n",result->container.question);
if(result->leftChild->container.object == NULL)
printf("Yes: %s\n",result->leftChild->container.question);
else
printf("Yes: %s\n",result->leftChild->container.object);
if(result->leftChild->container.object == NULL)
printf("Yes: %s\n",result->rightChild->container.question);
else
printf("Yes: %s\n",result->rightChild->container.object);
}
else
{
printf("Object: %s\n","[NOTHING]");
printf("Question: %s\n",result->container.question);
}
}
// This function traverse from the root until it finds the desired content (return 1), otherwise (return -1)
struct node * traverse(struct node * searchingNode, char * searchContent) {
// If the node is found, just return it.
if (strcmp(searchingNode->container.question, searchContent) == 0)
return searchingNode;
// If the node is not found and we are at a leaf, return NIL
if (searchingNode->leftChild == NULL && searchingNode->rightChild == NULL)
return NULL;
// Searching in the left and right subtrees, respectively.
struct node *leftSubTree = traverse(searchingNode->leftChild, searchContent);
struct node *rightSubTree;
if(leftSubTree == NULL)
rightSubTree = traverse(searchingNode->rightChild, searchContent);
// Calculating the final result
if (leftSubTree == NULL && rightSubTree == NULL)
return NULL;
else if (leftSubTree == NULL)
return rightSubTree;
return leftSubTree;
}
为了搜索节点,我需要调用nodePrint()并在主函数中将适当的参数传递给它,如下所示:
int main() {
nodePrint("A Pizza");
return 0;
}
如果节点数据是一个问题,则预期结果应为:
Object: [NOTHING]
Question: Is it flat, round and edible?
Yes: A Pizza
Yes: Pete
如果数据是语句,则输出应为:
Object: [NOTHING]
Question: A Pizza
我的程序显示了适当的结果,但是有一个SEGFAULT,我无法弄清楚是什么原因引起的。我真的很感谢有人能帮我调试它。