C中BST程序的功能之一的问题
Problem in one of the function of the BST program in C
#include <stdlib.h>
struct btNode
{
int data;
struct btNode *right;
struct btNode *left;
} * root, *temp1, *temp2;
void create(int);
void insert(int);
void postorder(struct btNode *);
int main()
{
int choice, item;
do
{
printf("\nChoose one of the options:\n");
printf("1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter any number to insert:");
scanf("%d", &item);
insert(item);
break;
case 4:
postorder(root);
break;
case 6:
break;
default:
printf("\nWRONG INPUT");
}
} while (choice != 6);
return 0;
}
void create(int num)
{
temp1 = (struct btNode *)malloc(sizeof(struct btNode));
temp1->data = num;
temp1->left = NULL;
temp1->right = NULL;
}
void insert(int num)
{
create(num);
if (root == NULL)
{
root = temp1;
printf("%d inserted\n", root->data);
}
else
{
temp2 = root;
while (temp2 != NULL)
{
//printf("inside while");
if (temp2->data >= num)
{
temp2 = temp2->left;
}
else
{
temp2 = temp2->right;
}
}
temp2 = temp1;
printf("%d inserted\n", temp2->data);
}
}
void postorder(struct btNode *r)
{
if (root == NULL)
{
printf("Tree is empty");
return;
}
else
{
postorder(r->left);
postorder(r->right);
printf("%d ", r->data);
}
}
以上是一个不完整的BST菜单驱动程序。我现在已经尝试完成创建节点、插入和后序的功能。但是当我插入几个元素并尝试执行后序时,主要问题出现了,程序突然终止了。我试过调试程序,在所有三个函数处设置断点。在调试期间,create()
和 insert()
功能运行良好,但主要问题出现在 postorder()
功能期间。请帮我解决问题。
请参阅我用 // CHANGE HERE
标记的评论。
测试link:https://onlinegdb.com/FK7SQ02nP
#include <stdlib.h>
#include <stdio.h>
struct btNode
{
int data;
struct btNode *right;
struct btNode *left;
}; // CHANGE HERE: remove globals
struct btNode* create(int); // CHANGE HERE: signature change
struct btNode* insert(struct btNode*, int); // CHANGE HERE: signature change
void postorder(struct btNode *);
int main()
{
int choice, item;
// CHANGE HERE: assign root to NULL
struct btNode* root = NULL;
do
{
printf("\nChoose one of the options:\n");
printf("1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter any number to insert:");
scanf("%d", &item);
root = insert(root, item);
break;
case 4:
postorder(root);
break;
case 6:
break;
default:
printf("\nWRONG INPUT");
}
} while (choice != 6);
return 0;
}
// CHANGE HERE: return the pointer from function
struct btNode* create(int num)
{
struct btNode* temp1 = (struct btNode *)malloc(sizeof(struct btNode));
temp1->data = num;
temp1->left = NULL;
temp1->right = NULL;
return temp1;
}
// CHANGE HERE: accept root node as argument and return the root node from function
struct btNode* insert(struct btNode* root, int num)
{
// CHANGE HERE: store returned pointer
struct btNode* temp1 = create(num);
if (root == NULL)
{
root = temp1;
printf("%d inserted\n", root->data);
}
else
{
// CHANGE HERE: see the while loop
struct btNode* temp2 = root;
while (temp2 != NULL)
{
//printf("inside while");
if (temp2->data >= num)
{
// CHANGE HERE
if (temp2->left)
temp2 = temp2->left;
else
{
temp2->left = temp1;
printf("%d inserted\n", temp2->left->data);
break;
}
}
else
{
// CHANGE HERE
if (temp2->right)
temp2 = temp2->right;
else
{
temp2->right = temp1;
printf("%d inserted\n", temp2->right->data);
break;
}
}
}
// CHANGE HERE: commented these two lines
// temp2 = temp1;
// printf("%d inserted\n", temp2->data);
}
return root;
}
void postorder(struct btNode *r)
{
// CHANGE HERE: replaced root with r
if (r == NULL)
{
printf("Tree is empty");
return;
}
else
{
// CHANGE HERE: call postorder only if children are not null
if (r->left) postorder(r->left);
if (r->right) postorder(r->right);
printf("%d ", r->data);
}
}
示例输出:
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:2
2 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:34
34 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:564
564 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:342
342 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-1000
-1000 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-332
-332 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-987
-987 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-9
-9 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
4
-987 -9 -332 -1000 342 564 34 2
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
#include <stdlib.h>
struct btNode
{
int data;
struct btNode *right;
struct btNode *left;
} * root, *temp1, *temp2;
void create(int);
void insert(int);
void postorder(struct btNode *);
int main()
{
int choice, item;
do
{
printf("\nChoose one of the options:\n");
printf("1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter any number to insert:");
scanf("%d", &item);
insert(item);
break;
case 4:
postorder(root);
break;
case 6:
break;
default:
printf("\nWRONG INPUT");
}
} while (choice != 6);
return 0;
}
void create(int num)
{
temp1 = (struct btNode *)malloc(sizeof(struct btNode));
temp1->data = num;
temp1->left = NULL;
temp1->right = NULL;
}
void insert(int num)
{
create(num);
if (root == NULL)
{
root = temp1;
printf("%d inserted\n", root->data);
}
else
{
temp2 = root;
while (temp2 != NULL)
{
//printf("inside while");
if (temp2->data >= num)
{
temp2 = temp2->left;
}
else
{
temp2 = temp2->right;
}
}
temp2 = temp1;
printf("%d inserted\n", temp2->data);
}
}
void postorder(struct btNode *r)
{
if (root == NULL)
{
printf("Tree is empty");
return;
}
else
{
postorder(r->left);
postorder(r->right);
printf("%d ", r->data);
}
}
以上是一个不完整的BST菜单驱动程序。我现在已经尝试完成创建节点、插入和后序的功能。但是当我插入几个元素并尝试执行后序时,主要问题出现了,程序突然终止了。我试过调试程序,在所有三个函数处设置断点。在调试期间,create()
和 insert()
功能运行良好,但主要问题出现在 postorder()
功能期间。请帮我解决问题。
请参阅我用 // CHANGE HERE
标记的评论。
测试link:https://onlinegdb.com/FK7SQ02nP
#include <stdlib.h>
#include <stdio.h>
struct btNode
{
int data;
struct btNode *right;
struct btNode *left;
}; // CHANGE HERE: remove globals
struct btNode* create(int); // CHANGE HERE: signature change
struct btNode* insert(struct btNode*, int); // CHANGE HERE: signature change
void postorder(struct btNode *);
int main()
{
int choice, item;
// CHANGE HERE: assign root to NULL
struct btNode* root = NULL;
do
{
printf("\nChoose one of the options:\n");
printf("1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter any number to insert:");
scanf("%d", &item);
root = insert(root, item);
break;
case 4:
postorder(root);
break;
case 6:
break;
default:
printf("\nWRONG INPUT");
}
} while (choice != 6);
return 0;
}
// CHANGE HERE: return the pointer from function
struct btNode* create(int num)
{
struct btNode* temp1 = (struct btNode *)malloc(sizeof(struct btNode));
temp1->data = num;
temp1->left = NULL;
temp1->right = NULL;
return temp1;
}
// CHANGE HERE: accept root node as argument and return the root node from function
struct btNode* insert(struct btNode* root, int num)
{
// CHANGE HERE: store returned pointer
struct btNode* temp1 = create(num);
if (root == NULL)
{
root = temp1;
printf("%d inserted\n", root->data);
}
else
{
// CHANGE HERE: see the while loop
struct btNode* temp2 = root;
while (temp2 != NULL)
{
//printf("inside while");
if (temp2->data >= num)
{
// CHANGE HERE
if (temp2->left)
temp2 = temp2->left;
else
{
temp2->left = temp1;
printf("%d inserted\n", temp2->left->data);
break;
}
}
else
{
// CHANGE HERE
if (temp2->right)
temp2 = temp2->right;
else
{
temp2->right = temp1;
printf("%d inserted\n", temp2->right->data);
break;
}
}
}
// CHANGE HERE: commented these two lines
// temp2 = temp1;
// printf("%d inserted\n", temp2->data);
}
return root;
}
void postorder(struct btNode *r)
{
// CHANGE HERE: replaced root with r
if (r == NULL)
{
printf("Tree is empty");
return;
}
else
{
// CHANGE HERE: call postorder only if children are not null
if (r->left) postorder(r->left);
if (r->right) postorder(r->right);
printf("%d ", r->data);
}
}
示例输出:
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:2
2 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:34
34 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:564
564 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:342
342 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-1000
-1000 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-332
-332 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-987
-987 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
1
Enter any number to insert:-9
-9 inserted
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit
4
-987 -9 -332 -1000 342 564 34 2
Choose one of the options:
1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit