我有一个二叉树程序。而且我试着从该项目得到如下的输出:在数组wuthout打印和数组比较问题中存储方法的值
55, 31, 1 , 49, 39, 47, 64, 65, 98, 97 // (result of print_preorder)
its ok to insertBoth // result of comparison
但即时得到tihs:
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
55 31 1 49 39 47 64 65 98 97
你看如何解决这个问题?如何在“arr”数组中存储二进制树中的实际值 ?以及如何做比较?我尝试使用下面的方法 ,但它不起作用。
实例与问题的工作:
#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 main() {
node *root;
node *tmp;
int i, j;
int arr[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
int arrExp[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
root = NULL;
for (i = 0; i < 10; i++) {
insert(&root, arr[i]);
}
for (j = 0; j < 10; j++) {
arr[j] = print_preorder(root);
printf("\n");
}
for (i = 0; i < 10; i++) {
if (arr[i] == arrExp[i]) {
printf("test");
}
}
return 0;
}
另外对于比较,我尝试使用方法:
bool check(int actual[], int expected[]) {
int i = 0;
while(actual){
if(actual[i++] != expected[i++])
return true;
}
return false;
}
然后使用方法:
if(verify(arr,arrExp)){
printf("test");
}
else{
printf("test no");
}
,但我得到同样的问题。
而且不工作:
bool check(int expected[], node ** tree) {
int i = 0;
if(tree == NULL)
return false;
for(int i =0; i<sizeof(tree)/sizeof(int); i++){
if(expected[i++] != (*tree)->data)
return false;
}
return true;
}
阵列和预购结果之间的比较仍然不工作,它似乎总是“不一样”,我应该有一些错误的,但我不是发现其中的问题是:
int main() {
node *root = NULL;
int i, j;
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 *ap = arrExp;
int arrExpPo[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
int *apo= arr;
// insert 10 nodes in the tree
for (i = 0; i < n; i++)
insert(&root, arr[i]);
// print tree
print_preorder(root);
puts("\n");
// compare array with tree (it works)
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");
// compare array with pre order result (dont works, appears always "not same")
outToArrayPreOrder(root, &apo);
for (i = 0; i < n; i++) {
if (arr[i] != arrExpPo[i]) {
puts("not same");
return -1;
}
}
puts("same");
return 0;
}
嗯,“55 31 1 49 39 47 64 65 98 97”输出中没有'''',当然'printf(“%d,”,tree-> data)''会产生一个逗号。怀疑生成文件问题或错误/缺少代码或错误的输出发布。 – chux
谢谢,你是对的。现在它是正确的,我正在测试,忘了改变。 – Jokl
我在'int print_preorder'中看不到返回值。打开警告并注意它们。 –