My Kernel v0.1.0
avl.h
Go to the documentation of this file.
1
29#ifndef LIBALGO_AVL_H
30#define LIBALGO_AVL_H
31
32#include <kernel/types.h>
33
34#include <utils/compiler.h>
35
36#include <stdbool.h>
37#include <stddef.h>
38
39typedef struct avl avl_t;
40
52struct avl {
56 ssize_t height;
57};
58
60static inline ssize_t avl_height(const avl_t *avl)
61{
62 return (avl == NULL) ? -1 : avl->height;
63}
64
66static inline bool avl_is_root(const avl_t *avl)
67{
68 return avl->parent == NULL;
69}
70
72#define AVL_EMPTY_NODE ((avl_t){0})
73
79typedef int (*avl_compare_t)(const avl_t *left, const avl_t *right);
80
96const avl_t *avl_search(avl_t *root, avl_t *value, avl_compare_t);
97
111
123avl_t *avl_remove(avl_t **root, avl_t *value, avl_compare_t);
124
137void avl_print(avl_t *root, void (*print)(const avl_t *));
138
144const avl_t *avl_min(const avl_t *root);
145
151const avl_t *avl_max(const avl_t *root);
152
155#endif /* LIBALGO_AVL_H */
static bool avl_is_root(const avl_t *avl)
Returns hether the given AVL node is the root of a tree.
Definition: avl.h:66
static ssize_t avl_height(const avl_t *avl)
Returns the height of an AVL tree.
Definition: avl.h:60
avl_t * avl_insert(avl_t **root, avl_t *new, avl_compare_t)
Insert a new node inside an AVL tree.
Definition: avl.c:263
int(* avl_compare_t)(const avl_t *left, const avl_t *right)
Comparison function used during AVL tree operations.
Definition: avl.h:79
avl_t * avl_remove(avl_t **root, avl_t *value, avl_compare_t)
Remove a value from an AVL tree.
Definition: avl.c:296
void avl_print(avl_t *root, void(*print)(const avl_t *))
Print the content of an AVL tree.
Definition: avl.c:359
const avl_t * avl_min(const avl_t *root)
Find the minimum value inside an AVL tree.
Definition: avl.c:370
const avl_t * avl_max(const avl_t *root)
Find the maximum value inside an AVL tree.
Definition: avl.c:382
const avl_t * avl_search(avl_t *root, avl_t *value, avl_compare_t)
Search a value inside an AVL tree.
Definition: avl.c:49
A single node of an AVL tree.
Definition: avl.h:52
avl_t * left
The left child.
Definition: avl.h:53
avl_t * right
The right child.
Definition: avl.h:54
ssize_t height
Height of the tree.
Definition: avl.h:56
avl_t * parent
The parent, NULL if this is the root.
Definition: avl.h:55