私はバイナリツリープログラムを持っています。 IMはプログラムからこの出力を取得しよう:メソッドの値を配列wuthout printと配列比較の問題に格納する
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"配列に実際の の値をバイナリツリーに格納するにはどうすればいいですか?比較を行う方法は?私は の方法で試したが、うまくいきませんでした。
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");
}
しかし、私は得る私はメソッドを使用しようと、比較のためにも
#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 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;
}
アレイと事前に注文した結果との比較はまだ仕事は、それは、私が間違っている何かを持っている必要がありますが、問題がどこにあるImが見つからない、常に「同じではありません」と表示されますいけません:
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;
}
「5531 1 49 39 47 64 65 98 97出力には何もありませんか?確かに 'printf("%d "、tree-> data);'はカンマを生成します。疑わしいmakefileの問題またはfalse/missing codeまたはfalse outputが出力されました。 – chux
ありがとうございます。その正しい今、私はそのようなテストをしていて、変更することを忘れています。 – Jokl
'int print_preorder'には戻り値がありません。あなたの警告を立て、注意を払う。 –