Update 'a3/binary_search_tree.c'
This commit is contained in:
		
							parent
							
								
									52e154db28
								
							
						
					
					
						commit
						1d07f03672
					
				@ -1,64 +1,66 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdbool.h>
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
#include "binary_search_tree.h"
 | 
					#include "binary_search_tree.h"
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
node_t *create_node(int data) ;
 | 
					node_t *create_node(int data) ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
node_t *create_node(int data) {
 | 
					node_t *create_node(int data) {
 | 
				
			||||||
    node_t *new_node = (node_t *)malloc(sizeof(node_t));
 | 
					    node_t *new_node = (node_t *)malloc(sizeof(node_t));
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
    new_node->data = data;
 | 
					    new_node->data = data;
 | 
				
			||||||
    new_node->left = NULL;
 | 
					    new_node->left = NULL;
 | 
				
			||||||
    new_node->right = NULL;
 | 
					    new_node->right = NULL;
 | 
				
			||||||
    return new_node;
 | 
					    return new_node;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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 = create_node(tree_data[0]);
 | 
					    node_t *root = create_node(tree_data[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    size_t i = 1;
 | 
					    size_t i = 1;
 | 
				
			||||||
while (i < tree_data_len) {
 | 
					while (i < tree_data_len) {
 | 
				
			||||||
    node_t *new_node = create_node(tree_data[i]);
 | 
					    node_t *new_node = create_node(tree_data[i]);
 | 
				
			||||||
    node_t *current = root;
 | 
					    node_t *current = root;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (1) {
 | 
					    while (1) {
 | 
				
			||||||
        if (new_node->data <= current->data) {  //porovnanie novy uzol a aktualny
 | 
					        if (new_node->data <= current->data) {  //porovnanie novy uzol a aktualny
 | 
				
			||||||
            if (current->left == NULL) {  
 | 
					            if (current->left == NULL) {  
 | 
				
			||||||
                current->left = new_node;
 | 
					                current->left = new_node;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            current = current->left;
 | 
					            current = current->left;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            if (current->right == NULL) {
 | 
					            if (current->right == NULL) {
 | 
				
			||||||
                current->right = new_node;
 | 
					                current->right = new_node;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            current = current->right;
 | 
					            current = current->right;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    i++;
 | 
					    i++;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
    return root;
 | 
					    return root;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Function to free the memory allocated for the binary search tree
 | 
					// Function to free the memory allocated for the binary search tree
 | 
				
			||||||
void free_tree(node_t *tree) {
 | 
					void free_tree(node_t *tree) {
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
   free(tree); 
 | 
					   free(tree); 
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Function to return sorted data from the binary search tree
 | 
					// Function to return sorted data from the binary search tree
 | 
				
			||||||
int *sorted_data(node_t *tree) {
 | 
					int *sorted_data(node_t *tree) {
 | 
				
			||||||
 | 
					if(tree==NULL){
 | 
				
			||||||
    return NULL;
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user