So habe ich ein Array von Strukturen, die ich in einen binären Suchbaum verwandeln möchte. Hier ist, wie mein Code aussieht.Verwandeln Sie ein Array von Strukturen in einen binären Suchbaum
typedef struct Student{
char name[25];
char surname[25];
char Id[8];
double grade;
}Student;
struct TNode
{
struct TNode* data;
struct TNode* left;
struct TNode* right;
};
struct TNode* newNode(struct TNode* data);
/* A function that constructs Balanced Binary Search Tree from a sorted array
*/
struct TNode* sortedArrayToBST(struct Student** students, int start, int end)
{
/* Base Case */
if (start > end)
return NULL;
/* Get the middle element and make it root */
int mid = (start + end)/2;
struct TNode *root = newNode(students[mid]);
/* Recursively construct the left subtree and make it
left child of root */
root->left = sortedArrayToBST(students, start, mid-1);
/* Recursively construct the right subtree and make it
right child of root */
root->right = sortedArrayToBST(students, mid+1, end);
return root;
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct TNode* newNode(struct TNode * data)
{
struct TNode* node = (struct TNode*)
malloc(sizeof(struct TNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
/* A utility function to print preorder traversal of BST */
void preOrder(struct TNode* node)
{
if (node == NULL)
return;
printf("%s %s %s %.2f ", node->data);
preOrder(node->left);
preOrder(node->right);
}
Und hier ist, wie ich die Funktionen in meinem Haupt aufrufen.
struct TNode *root = sortedArrayToBST(&students, 0, n-1);
scheinen diese nicht aus irgendeinem Grund zu arbeiten, obwohl das Array von Strukturen innerhalb meiner Hauptfunktion funktioniert ok. Ich sortiere immer mein Array von Strukturen innerhalb meines Hauptprogramms, bevor ich die Funktion sortedArraytoBST aufruft. Bitte hilf mir.
nicht Ihren Compiler geben Sie einige * * Warnung auf 'struct TNODE * root = newNode (Studenten [Mitte]); '? –
Ja, aber ich kann nicht verstehen warum. –