123
This commit is contained in:
parent
b955cf1824
commit
0cd0b96b00
@ -1,11 +1,5 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include "binary_search_tree.h"
|
#include "binary_search_tree.h"
|
||||||
|
#include <stdlib.h>
|
||||||
typedef struct node {
|
|
||||||
struct node *right;
|
|
||||||
struct node *left;
|
|
||||||
int data;
|
|
||||||
} node_t;
|
|
||||||
|
|
||||||
node_t* insert(node_t *node, int data) {
|
node_t* insert(node_t *node, int data) {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
@ -22,6 +16,13 @@ node_t* insert(node_t *node, int data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void inorder_traversal(node_t *node, int *sorted) {
|
||||||
|
if (node == NULL) return;
|
||||||
|
inorder_traversal(node->left, sorted);
|
||||||
|
sorted[sorted_index++] = node->data;
|
||||||
|
inorder_traversal(node->right, sorted);
|
||||||
|
}
|
||||||
|
|
||||||
node_t* build_tree(int *tree_data, size_t tree_data_len) {
|
node_t* build_tree(int *tree_data, size_t tree_data_len) {
|
||||||
node_t *root = NULL;
|
node_t *root = NULL;
|
||||||
for (size_t i = 0; i < tree_data_len; i++) {
|
for (size_t i = 0; i < tree_data_len; i++) {
|
||||||
@ -37,15 +38,6 @@ void free_tree(node_t *tree) {
|
|||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sorted_index = 0;
|
|
||||||
|
|
||||||
void inorder_traversal(node_t *node, int *sorted) {
|
|
||||||
if (node == NULL) return;
|
|
||||||
inorder_traversal(node->left, sorted);
|
|
||||||
sorted[sorted_index++] = node->data;
|
|
||||||
inorder_traversal(node->right, sorted);
|
|
||||||
}
|
|
||||||
|
|
||||||
int *sorted_data(node_t *tree) {
|
int *sorted_data(node_t *tree) {
|
||||||
int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků
|
int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků
|
||||||
sorted_index = 0;
|
sorted_index = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user