errewrewrwr

This commit is contained in:
Michal Utľák 2024-04-23 19:49:45 +02:00
parent a38b9a5579
commit 6eef05da23

View File

@ -14,7 +14,7 @@ void free_tree(node_t *tree) {
free(tree); free(tree);
} }
int *sorted_data(node_t *tree) { int *sorted_data(node_t *tree) {
if (tree == NULL) { if (tree == NULL) {
return NULL; return NULL;
} }
@ -30,9 +30,30 @@ int *sorted_data(node_t *tree) {
return NULL; return NULL;
} }
size_t index = 0; size_t index = 0;
inorder_traversal(tree, sorted_array, &index); node_t *current_node = tree;
while (current_node != NULL) {
if (current_node->left == NULL) {
sorted_array[index++] = current_node->data;
current_node = current_node->right;
} else {
node_t *predecessor = current_node->left;
while (predecessor->right != NULL && predecessor->right != current_node) {
predecessor = predecessor->right;
}
if (predecessor->right == NULL) {
predecessor->right = current_node;
current_node = current_node->left;
} else {
predecessor->right = NULL;
sorted_array[index++] = current_node->data;
current_node = current_node->right;
}
}
}
return sorted_array; return sorted_array;
} }