Initializtion

This commit is contained in:
Kozar 2024-04-25 21:33:23 +02:00
parent a2f279f2e3
commit bcf92ad070

View File

@ -2,6 +2,9 @@
#include <stdlib.h>
#include <string.h>
int count_nodes(node_t *tree);
void in_order_traversal(node_t *tree, int **ptr);
node_t *build_tree(int *tree_data, size_t tree_data_len) {
if (tree_data_len == 0) {
return NULL;
@ -56,3 +59,22 @@ int *sorted_data(node_t *tree) {
in_order_traversal(tree, &ptr);
return data;
}
int count_nodes(node_t *tree) {
if (tree == NULL) {
return 0;
}
return 1 + count_nodes(tree->left) + count_nodes(tree->right);
}
void in_order_traversal(node_t *tree, int **ptr) {
if (tree == NULL) {
return;
}
in_order_traversal(tree->left, ptr);
**ptr = tree->data;
(*ptr)++;
in_order_traversal(tree->right, ptr);
}