将方法的值存储在数组wuthout print和数组比较问题中

时间:2016-12-17 00:06:50

标签: c arrays

我有一个二叉树程序。我试图从程序中获得此输出:

window.opener

但是我得到了:

55, 31, 1 , 49, 39, 47, 64, 65, 98, 97         // (result of print_preorder)
its ok to insertBoth                           // result of comparison

你看到如何解决这个问题吗?如何存储在" arr"数组实际 二叉树中的值?以及如何进行比较?我试过下面的方法 但它不起作用。

处理问题的示例:

 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

同样为了比较,我尝试使用一种方法:

        #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");
    }

数组和预订结果之间的比较仍然不起作用,它似乎总是&#34;不一样&#34;,我应该有一些错误,但我找不到问题所在:

 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;
    }

1 个答案:

答案 0 :(得分:0)

试试这个

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 cmp(const void *a, const void *b){
    int x = *(int *)a;
    int y = *(int *)b;
    return x < y ? -1 : x > y;
}

int main(void) {
    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;

    for (i = 0; i < n; i++)
        insert(&root, arr[i]);

    print_preorder(root);
    puts("\n");

    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");

    return 0;
}
void outToArray(node *tree, int **arr){
//pre-order output 
    if(tree){
        *(*arr)++ = tree->data;
        outToArray(tree->left, arr);
        outToArray(tree->right, arr);
    }
}

int main(void) {
    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[] = {55, 31, 49, 64, 65, 39, 47, 98, 97, 1};
    int *ap = arr;

    for (i = 0; i < n; i++)
        insert(&root, arr[i]);

    print_preorder(root);
    puts("\n");

    outToArray(root, &ap);

    for (i = 0; i < n; i++) {
        if (arr[i] != arrExp[i]) {
            puts("not same");
            return -1;
        }
    }
    puts("same");

    return 0;
}

供参考:

{55,31,49,64,65,39,47,98,97,1}到BST

    55
   / \
  31   64
 / \   \
1   49   65
   /     \
  39       98
   \     /
    47   97

预订在树上行走
55→31→1→49→39→47→64→65→98→97