此代码是用 C 编写的。Node_reset() 函数的第一行出现错误。(结构数组在主函数中动态分配。)我认为Node_reset()的成员访问有问题,Create()也可能出现同样的错误。Input.txt,提供如下格式:
2
3 dfs(3)
786 368 603
5 bfs(4)
825 162 768 724 635
第一行中的数字(即 2)表示要处理的测试用例的数量。从第二行开始,给出了测试用例,每个用例的信息分两行提供。
在第一行(上例中的第 2 行)中,第一个数字(即 3)表示该情况下的整数个数,下一个字符串(即 dfs(3))表示目标数字的位置你需要寻找的。在本例中,目标编号在 DFS 的第三个节点上。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_QUEUE_SIZE 100
#define FALSE 1;
#define TRUE 0;
typedef struct BST_node{
int data;
int visited;
struct BST_node * leftChild;
struct BST_node * rightChild;
}node;
typedef node* TreeNode;
typedef struct {
TreeNode data[MAX_QUEUE_SIZE];
int front;
int rear;
}Queue;
void Node_reset(TreeNode* input_tree, int BST_size)
{
for(int i=0;i<BST_size;i++)
{
input_tree[i]->data = 0; //"Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"occurs
input_tree[i]->leftChild=NULL;
input_tree[i]->rightChild=NULL;
input_tree[i]->visited=FALSE;
}
}
void Create(TreeNode* input_tree, int data[], int BST_size)
{
for(int i=0;i<BST_size;i++)
{
input_tree[i]->data=data[i];
}
for(int i=0;i<BST_size-1;i++)
{
if(input_tree[i]->data > input_tree[i+1]->data) input_tree[i]->leftChild=input_tree[i+1];
else input_tree[i]->rightChild=input_tree[i+1];
}
}
void init_Q(Queue* q)
{
q->front = q->rear =0;
}
int IsEmpty(Queue* q)
{
return (q->front == q->rear);
}
int IsFull(Queue* q)
{
return ((q->rear+1)%MAX_QUEUE_SIZE==q->front);
}
void enqueue(Queue* q,TreeNode data)
{
if(IsFull(q))
{
printf("Queue is Full");
}
q->rear = (q->rear+1)%MAX_QUEUE_SIZE;
q->data[q->rear]=data;
}
TreeNode dequeue(Queue* q)
{
if(IsEmpty(q))
{
printf("Queue is empty");
return NULL;
}
q->front = (q->front+1)%MAX_QUEUE_SIZE;
return q->data[q->front];
}
int DFS_search(TreeNode Node, int *searching_times)
{
while(*searching_times==0){
*searching_times--;
if(Node->leftChild!=NULL) DFS_search(Node->leftChild, searching_times);
if(Node->rightChild!=NULL) DFS_search(Node->rightChild, searching_times);
}
return Node->data;
}
int BFS_search(TreeNode* Node, int searching_times)
{
Queue q;
init_Q(&q);
for(int i=0;i<searching_times;i++)
{
enqueue(&q, Node[i]);
if(Node[i]->leftChild)
enqueue(&q, Node[i]->leftChild);
if(Node[i]->rightChild)
enqueue(&q, Node[i]->rightChild);
}
for(int i=0;i<searching_times;i++)
{
if(i==searching_times-1) return dequeue(&q)->data;
else dequeue(&q);
}
return 0;
}
void pass_result(int result_num,int *result,int result_arr[])
{
result_arr[result_num]=(*result);
}
void return_result(int result_arr[],int how_many)
{
FILE *of = fopen("output.txt","w");
for(int i=0;i<how_many;i++)
{
fprintf(of, "%d\n",result_arr[i]);
}
fclose(of);
}
int main()
{
int how_many_times; //the number of input expression
int BST_size; //input the number of element here
char string[7]={0}; //input string data here
char temp[2]={0}; //input searching num
int searching_times; //the number of input expression
int result; //result of expression
FILE *fp = fopen("input.txt","r");
fscanf(fp, "%d", &how_many_times);
int *result_arr=(int*)malloc(sizeof(int)*how_many_times);
for(int i=0;i<how_many_times;i++)
{
memset(string, 0, sizeof(char)*7); //reset string[]
memset(temp, 0, sizeof(char)*2); //reset temp[]
fscanf(fp, "%d", &BST_size); //pass BST size
int* input=(int*)malloc(sizeof(int)*BST_size); //allocate heep memory as long as input BST size
fscanf(fp, "%s", string); //pass string input data to string[]
TreeNode* tree=(TreeNode*)malloc(sizeof(TreeNode)*BST_size);
Node_reset(tree, BST_size);
for(int i=0;i<BST_size;i++) //input elements in array
{
fscanf(fp, "%d",&input[i]);
}
Create(tree, input, BST_size);
for(int i=0;i<2;i++)
{
temp[i]=string[i+4];
}
searching_times=atoi(temp); //input data about searching times becomes integer type
if(strcmp(&string[0],"d")) result=DFS_search(tree[0], &searching_times);
if(strcmp(&string[0],"b")) result=BFS_search(tree, searching_times);
pass_result(i, &result, result_arr); //pass the result
free(input);
}
return_result(result_arr, how_many_times); //return result in txt file
free(result_arr); //free result heep data
}
这是发生错误的部分。如果需要,请参考完整代码或告诉我我还能做些什么。
#define MAX_QUEUE_SIZE 100
#define FALSE 1;
#define TRUE 0;
typedef struct BST_node{
int data;
int visited;
struct BST_node * leftChild;
struct BST_node * rightChild;
}node;
typedef node* TreeNode;
typedef struct {
TreeNode data[MAX_QUEUE_SIZE];
int front;
int rear;
}Queue;
void Node_reset(TreeNode* input_tree, int BST_size)
{
for(int i=0;i<BST_size;i++)
{
input_tree[i]->data = 0;
input_tree[i]->leftChild=NULL;
input_tree[i]->rightChild=NULL;
input_tree[i]->visited=FALSE;
}
}
void Create(TreeNode* input_tree, int data[], int BST_size)
{
for(int i=0;i<BST_size;i++)
{
input_tree[i]->data=data[i];
}
for(int i=0;i<BST_size-1;i++)
{
if(input_tree[i]->data > input_tree[i+1]->data) input_tree[i]->leftChild=input_tree[i+1];
else input_tree[i]->rightChild=input_tree[i+1];
}
}
int main()
{
int how_many_times; //the number of input expression
int BST_size; //input the number of element here
char string[7]={0}; //input string data here
char temp[2]={0}; //input searching num
int searching_times; //the number of input expression
int result; //result of expression
FILE *fp = fopen("input.txt","r");
fscanf(fp, "%d", &how_many_times);
int *result_arr=(int*)malloc(sizeof(int)*how_many_times);
for(int i=0;i<how_many_times;i++)
{
fscanf(fp, "%d", &BST_size); //pass BST size
int* input=(int*)malloc(sizeof(int)*BST_size); //allocate heep memory as long as input BST size
fscanf(fp, "%s", string); //pass string input data to string[]
TreeNode* tree=(TreeNode*)malloc(sizeof(TreeNode)*BST_size);
Node_reset(tree, BST_size);
for(int i=0;i<BST_size;i++) //input elements in array
{
fscanf(fp, "%d",&input[i]);
}
Create(tree, input, BST_size);
}
}