2016-12-17 4 views
0

私はバイナリツリープログラムを持っています。 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; 
} 
+1

「5531 1 49 39 47 64 65 98 97出力には何もありませんか?確かに 'printf("%d "、tree-> data);'はカンマを生成します。疑わしいmakefileの問題またはfalse/missing codeまたはfalse outputが出力されました。 – 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; 
} 

01を試してみてくださいFYI
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}ツリー内

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

プリオーダー徒歩BSTへ
55→31→1→49→39→47→64→65→98→97

+0

ご協力ありがとうございます。しかし、比較は機能していません。彼らは常に同じであるように見えます。しかし、私はあなたの解決策は、私はツリーに挿入するいくつかの値を持つ配列を1つの配列を持っている必要があるので、あなたのソリューションは考えていないし、期待された値を持つ期待された配列を持っています。そして、ツリー上のノードを挿入した後、ツリー上のノードが予想される配列と同じ順序で似ているかどうかを比較したいと思います。あなたのソリューションでは、配列は常に同じであるため、常に「同じ」であるように見えます。 – Jokl

+0

@ Jokl 'outToArray'は、ツリーの内容全体を配列に出力することです。したがって、ツリー構造の前の配列は出力後の出力と同じです。 – BLUEPIXY

+0

@Joklあなたのタイトルは、ツリーの内容を配列に格納したいと思うようです。配列の内容がすべてツリーに存在することをテストする場合は、別の配列に保存する必要はありません。 – BLUEPIXY

関連する問題