私はバイナリツリープログラムを持っています。そして、プログラムのいくつかの部分をテストして、入力が期待される出力にicualであるかどうかを確認しようとしています。しかし、下記の2つの問題があります。プログラムの出力が予想と異なっています
たとえば、このテストを以下のように実行しようとすると、テストパスに「同じ」、失敗したときに「同じではない」と表示されます。
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を返しますが、同じ問題が発生します。 「同じではない」と言うと、その部分は実行されないので、search = "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");
}
さて、 ''印刷され、次の行を返す '-1 "は、同じではない";'、-1' 'のメイン()' 'return'sa値'そうプログラムは終了する。そのため、これ以上のコードは実行されません。 –
あなたは[mcve]を提供する必要があります。このコードはそのままコンパイルされず、 'insert()'のような重要な機能がありません。 –
質問を更新します。これは私がこの瞬間に持っているものであり、問題の2つの問題があります。 – Jokl