2016-12-17 41 views
0

我有一个二叉树程序。而且我试着从该项目得到如下的输出:在数组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; 
} 
+1

嗯,“55 31 1 49 39 47 64 65 98 97”输出中没有'''',当然'printf(“%d,”,tree-> data)''会产生一个逗号。怀疑生成文件问题或错误/缺少代码或错误的输出发布。 – chux

+0

谢谢,你是对的。现在它是正确的,我正在测试,忘了改变。 – Jokl

+1

我在'int print_preorder'中看不到返回值。打开警告并注意它们。 –

回答

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

+0

感谢您的帮助。但比较不起作用。看起来它们总是一样的。但我认为你的解决方案不工作,因为我需要拖拽一个数组的一个数组,并在树上插入一些值,然后使用期望值的期望数组。如果树上的节点是icual并且与预期数组的顺序相同,则在我要比较的树上插入节点之后。用你的解决方案,数组看起来总是相同的,所以它总是显示“相同”。 – Jokl

+0

@Jokl'outToArray'用于输出树的全部内容到一个数组。所以树结构之前的数组与输出之后的输出相同。 – BLUEPIXY

+0

@Jokl您的标题似乎想要将数组中的内容存储在数组中。如果要测试数组的内容是否全部出现在树中,则不需要将其保存在另一个数组中。 – BLUEPIXY