The problem is that when writing data from a binary tree to an array, not all data is written. For example, a tree contains the following data:

76 Сидоров 60 Петров 54 Артуров 49 Иванов 41 Денисов 3 Волков 

But in the array are written only:

 49 Иванов 41 Денисов 60 Петров 54 Артуров 76 Сидоров 

Wednesday: Builder c ++ The code itself:

  struct Tree { int info; String name; Tree *left, *right; }*root; String **array; int size = NodeCount(root); /* функция NodeCount подсчитывает количество узлов, для того чтобы знать какой размерности объявлять массив.(Работает корректно)*/ array = new String*[size]; //объявляю двумерный массив for(int i=0;i<size;i++) array[i] = new String[2]; addToArray(root,array,0); // думаю в этой функции проблема.... for(int i=0;i<size;i++) { Memo1->Lines->Add(array[i][0] + ' ' + array[i][1]); // вывод массива } /* --------Сама функция-----*/ void addToArray(Tree *root,String **a,int idx) { a[idx][0] = IntToStr(root->info); a[idx][1] = root->name; if (root->left) addToArray(root->left,a,++idx); if (root->right) addToArray(root->right,a,++idx); } 

    1 answer 1

    The problem is that your values ​​are overwritten, because when saving the values ​​of the right node of the tree, there may already be something in the array at this place. You can solve this by simply passing in instead of int idx, int & idx for example. Just remember to create a variable with an initial value of 0 in the calling code.

    • Thank you very much for the tip. I am very grateful to you! while (true) cout << "thanks"; - Alex. jpeg