我有一个二叉树程序。我试图测试程序的某些部分,以检查输入是否是预期输出的结果。但我有两个问题,如下所述。
例如,我试图在下面执行此测试,并在测试通过时显示“相同”,并在测试通过时显示“不相同”。
INPUT OUTPUT
Insert 10 nodes(55, 31, 49, 64, 65, 39, 47, 98, 97, 1) 55, 31, 49, 64, 65, 39, 47, 98, 97, 1
Insert node 4 55, 31, 49, 64, 65, 39, 47, 98, 97, 1, 4
print_preorder() 55, 31, 1, 4, 49, 39, 47, 64, 65, 98, 97
search(55) 55
问题在于我提供了正确的输入,我总是得到下面的结果,最后一个测试显示“不相同”,当测试显示“不相同”时,代码的其他部分不会被执行。
same
same
not same
我希望:
same
same
same
55
另一个问题是,当我收到此消息“不相同”时,下面的其他代码不会被执行。
实施例
int main(void) {
node *root = NULL;
int i, j;
node *tmp;
int arr[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
int n = sizeof(arr)/sizeof(*arr);
int arrExp[sizeof(arr)/sizeof(*arr)] = {0};
int arrExp2[] = {55, 31, 1, 4, 49, 39, 47, 64, 65, 98, 97};
int *aporder = arr;
int *ap = arrExp;
// insert 10 nodes on tree
for (i = 0; i < n; i++)
insert(&root, arr[i]);
// check if the array elements are the same of the tree
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
puts("not same");
return -1;
}
}
puts("same");
// insert node 4 on tree
insert(&root, 4);
// check if tree nodes are now icual to arrExp that has now the 4 node
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
puts("not same");
return -1;
}
}
puts("same");
// check if pre order display is icual to ArrExp2
outToArrayPorder(root, &aporder);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp2[i]) {
puts("not same");
return -1;
}
}
puts("same");
tmp = search(&root, 55);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
return 0;
}
int cmp(const void *a, const void *b){
int x = *(int *)a;
int y = *(int *)b;
return x < y ? -1 : x > y;
}
void outToArrayPorder(node *tree, int **arr){
//pre-order output
if(tree){
*(*arr)++ = tree->data;
outToArrayPorder(tree->left, arr);
outToArrayPorder(tree->right, arr);
}
}
void outToArray(node *tree, int **arr){
//Write elements of tree to the array.
if(tree){
outToArray(tree->left, arr);
*(*arr)++ = tree->data;
outToArray(tree->right, arr);
}
}
int print_preorder(node * tree)
{
if (tree)
{
printf("%d ",tree->data);
int left = print_preorder(tree->left);
int right = print_preorder(tree->right);
return left+right+1;
} else{
return 0;
}
}
完成计划:
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
int print_preorder(node * tree)
{
if (tree)
{
printf("%d ",tree->data);
int left = print_preorder(tree->left);
int right = print_preorder(tree->right);
return left+right+1;
} else{
return 0;
}
}
int print_inorder(node * tree)
{
if (tree)
{
int left = print_inorder(tree->left);
printf("%d ",tree->data);
int right = print_inorder(tree->right);
return left+right+1;
}else{
return 0;
}
}
int print_postorder(node * tree)
{
if (tree) {
int left = print_postorder(tree->left);
int right = print_postorder(tree->right);
printf("%d ",tree->data);
return left + right + 1;
} else {
return 0;
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
int cmp(const void *a, const void *b){
int x = *(int *)a;
int y = *(int *)b;
return x < y ? -1 : x > y;
}
void outToArrayPorder(node *tree, int **arr){
//pre-order output
if(tree){
*(*arr)++ = tree->data;
outToArrayPorder(tree->left, arr);
outToArrayPorder(tree->right, arr);
}
}
void outToArray(node *tree, int **arr){
//Write elements of tree to the array.
if(tree){
outToArray(tree->left, arr);
*(*arr)++ = tree->data;
outToArray(tree->right, arr);
}
}
int main(void) {
node *root = NULL;
int i, j;
node *tmp;
int arr[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
int n = sizeof(arr)/sizeof(*arr);
int arrExp[sizeof(arr)/sizeof(*arr)] = {0};
int arrExp2[] = {55, 31, 1, 4, 49, 39, 47, 64, 65, 98, 97};
int *aporder = arr;
int *ap = arrExp;
// insert 10 nodes on tree
for (i = 0; i < n; i++)
insert(&root, arr[i]);
// check if the array elements are the same of the tree
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
puts("not same");
return -1;
}
}
puts("same");
// insert node 4 on tree
insert(&root, 4);
// check if tree nodes are now icual to arrExp that has now the 4 node
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
puts("not same");
return -1;
}
}
puts("same");
// check if pre order display is icual to ArrExp2
outToArrayPorder(root, &aporder);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp2[i]) {
puts("not same");
return -1;
}
}
puts("same");
tmp = search(&root, 55);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
return 0;
}
尝试解决有关代码不能执行的问题,当它看起来“不相同”时,我尝试如下,没有返回-1,但同样的问题发生。当它说“不相同”时,搜索=“55”不会出现,因为该部分未执行。
// insert node 4 on tree
insert(&root, 4);
// check if tree nodes are now icual to arrExp2 that has now the 4 node
int out2[++n];
ap = out2;
bool test = true;
outToArray(root, &ap);
for (i = 0; i < n; i++) {
if (out2[i] != arrExp2[i]) {
puts("not same");
test=false;
}
}
if(test == true){
puts("same");
}
答案 0 :(得分:1)
目前,如果代码显然正常工作,您的代码将设计为停止。你可能已经反过来写了测试。
通过返回自身的结果(返回搜索(...))修复search
函数以进行迭代后,此代码执行:
// insert node 4 on tree
insert(&root, 4);
// check if tree nodes are now equal to arrExp that has now the 4 node
现在:您正在向ap
输出11个节点,这些节点指向为10个数字保留的区域。很明显,无法工作。在这种情况下,出现工作(即,它不会崩溃),因为你要覆盖堆栈上的下一个变量。
但即便如此,您现在正在将11元素树与10元素向量(arr)进行比较。他们根本不可能是平等的。
(顺便说一下,你不需要再次点击arr):
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
puts("not same");
return -1;
}
}
puts("same");
上面的代码当然会找到&#34; 4&#34;元素,不在arr,输出&#34;不相同&#34;并停止。你可能想要:
outToArray(root, &ap);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp[i]) {
break;
}
}
if (i == n) {
puts("same. This should not happen");
exit(1);
} else {
puts("not same, as expected.");
}
您需要修复数组以保留足够的空间以便工作:
int arrExp[sizeof(arr)/sizeof(*arr) + 2];
然后你不能让aporder
指向arr
,因为你要在其中放入11个元素,这意味着将11个元素写入arr,这会覆盖堆栈(和所有事情的根的价值!)。
int *aporder = arrExp;
最后,由于您不需要修改指向的值,因此可以简化search()
函数。我添加了一些调试代码:
node* search(node *tree, int val)
{
printf("Start %08lx\n", tree);
if (tree) {
if (val < tree->data)
{
printf("%d less than %d, going left\n", val, tree->data);
return search(tree->left, val);
}
if(val > tree->data)
{
printf("%d more than %d, going right\n", val, tree->data);
return search(tree->right, val);
}
}
return tree;
}
答案 1 :(得分:1)
您的代码不考虑添加元素 改变如下。
int main(void) {
node *root = NULL;
int i;
int arr[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
int n = sizeof(arr)/sizeof(*arr);
int arrExp1[sizeof(arr)/sizeof(*arr)] = {0};
int arrExp2[] = {1, 4, 31, 39, 47, 49, 55, 64, 65, 97, 98};
int arrExp3[] = {55, 31, 1, 4, 49, 39, 47, 64, 65, 98, 97};
int *ap = arrExp1;
// insert nodes of arr on tree
for (i = 0; i < n; i++)
insert(&root, arr[i]);
// check if the array elements are the same of the tree
outToArray(root, &ap);
qsort(arr, n, sizeof(*arr), cmp);
for (i = 0; i < n; i++) {
if (arr[i] != arrExp1[i]) {
puts("not same");
return -1;
}
}
puts("same");
// insert node 4 on tree
insert(&root, 4);
// check if tree nodes are now icual to arrExp2 that has now the 4 node
int out2[++n];
ap = out2;
outToArray(root, &ap);
for (i = 0; i < n; i++) {
if (out2[i] != arrExp2[i]) {
puts("not same");
return -1;
}
}
puts("same");
// check if pre order display is icual to ArrExp3
ap = out2;
outToArrayPorder(root, &ap);
for (i = 0; i < n; i++) {
if (out2[i] != arrExp3[i]) {
puts("not same");
return -1;
}
}
puts("same");
node *tmp = search(&root, 55);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
return 0;
}
征求意见。
示例代码:
n = sizeof(arr2)/sizeof(*arr2);//length of arr2
for (i = 0; i < n; i++)
insert(&root, arr2[i]);//values of arr2 add to tree
n = print_preorder(root);puts("");//number of elements of tree
int out4[n];//new array for store tree to array
ap = out4;
outToArrayPorder(root, &ap);
if(sizeof(out4) == sizeof(arrExp4) && memcmp(out4, arrExp4, sizeof(out4))==0)//Each size may not be equal.
puts("same4");
else
puts("not same");