1
0
Fork 0

Header implementation for AVL trees.

This commit is contained in:
Wael Karram 2021-10-17 13:42:21 +03:00
parent 03bb02d7ac
commit c976c052b3
2 changed files with 51 additions and 4 deletions

View File

@ -17,10 +17,10 @@ Tree node.
Linked list.
Double linked list.
Stack.
Safe string copy (not tested - not a priority).
Bubble Sort (not tested - fix tests).
Quicksort (not tested - fix tests, and fix comparison function!).
BST - Binary Search Tree (initial implementation, needs testing).
Safe string copy.
Bubble Sort.
Quicksort.
BST - Binary Search Tree.
To be implemented:
Queue (Header written, just write the implementation).

47
trees/AVL_tree/AVL_tree.h Normal file
View File

@ -0,0 +1,47 @@
/* This file defines a simple binary tree and related functions thereof. */
#ifndef AVL_TREE_H
#define AVL_TREE_H
#include <math.h>
#include <stdlib.h>
#include "../binary_tree/binary_tree.h"
/* Define constants. */
/* Return code for success. */
#define AVL_TREE_SUCCESS_CODE 0
/* Return code for failure. */
#define AVL_TREE_FAILURE_CODE 1
/* Return code for comparison, smaller. */
#define AVL_TREE_COMPARISON_SMALLER_THAN -1
/* Return code for comparison, equal. */
#define AVL_TREE_COMPARISON_EQUAL_TO 0
/* Return code for comparison, larger. */
#define AVL_TREE_COMPARISON_LARGER_THAN 1
/* Define all the functions. */
/* This function initializes a single node. */
tree_node_t* initialize_avl_tree_node();
/* This function initializes a single node and attempts to store the given data within. */
tree_node_t* initialize_avl_tree_node_store(void *data);
/* This helper function is used to recursively fill an array into a BST. */
tree_node_t* initialize_avl_tree_recursive(void **array, int index_start, int index_end);
/* This function creates a BST for a sorted array. */
tree_node_t* initialize_avl_tree(void **array, int length);
/* This function links a node to the right. */
int link_avl_tree_node_right(tree_node_t *parent, tree_node_t *child);
/* This function links a node to the left. */
int link_avl_tree_node_left(tree_node_t *parent, tree_node_t *child);
/* This function frees a whole tree - not including data. */
void free_avl_tree(tree_node_t *root);
/* This function frees a whole tree, including data. */
void free_avl_tree_data(tree_node_t *root, void (*free_function)(const void*));
/* This function inserts data to the tree. */
int insert_node_into_avl_tree(tree_node_t *root, tree_node_t *to_insert, int (*comparison_function)(const void*, const void*));
/* This function searches for data in the tree. */
tree_node_t* search_in_avl_tree(tree_node_t *root, void *data, int (*comparison_function)(const void*, const void*));
/* This function finds the tree's depth (at the deepest leaf). */
size_t find_avl_tree_depth(tree_node_t *root);
/* This function fills an AVL tree into an allocated array. */
int fill_array_avl_tree(tree_node_t *root, void **array, size_t *index, size_t length);
/* This function creates a sorted array, from an AVL tree. */
void **sorted_array_from_avl_tree(tree_node_t *root, size_t *length);
#endif /* AVL_TREE_H */