aboutsummaryrefslogtreecommitdiff
path: root/include/lib/algo/avl_tree.h
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-05 16:14:18 -0800
committerChristian C <cc@localhost>2025-03-05 16:14:18 -0800
commit74e46ee7709785f91e04ac42619e4a7e9f90bc18 (patch)
tree82f5140c42cff58d9a980e2737eea808e1820560 /include/lib/algo/avl_tree.h
parenta3684826417bf9cea4c24e8a82e9574feb64576d (diff)
Modularize
Diffstat (limited to 'include/lib/algo/avl_tree.h')
-rw-r--r--include/lib/algo/avl_tree.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/include/lib/algo/avl_tree.h b/include/lib/algo/avl_tree.h
new file mode 100644
index 0000000..80f0c44
--- /dev/null
+++ b/include/lib/algo/avl_tree.h
@@ -0,0 +1,40 @@
+#ifndef INC_LIB_ALGO_AVL_TREE_H
+#define INC_LIB_ALGO_AVL_TREE_H
+
+#include <lib/bool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+struct AVLNode {
+ void* data;
+ bool_t (*compare)(void*, void*);
+ struct AVLNode* left;
+ struct AVLNode* right;
+ uint8_t height;
+};
+
+// Get the height of an AVL node
+uint8_t get_height(struct AVLNode* node);
+
+// Get the Maximum Height between two
+uint8_t max_height(uint8_t a, uint8_t b);
+
+// Get the balance factor of a node
+ssize_t get_balance_factor(struct AVLNode* node);
+
+// Rotate an AVL node right
+struct AVLNode* right_rotate(struct AVLNode* parent);
+
+// Rotate an AVL node left
+struct AVLNode* left_rotate(struct AVLNode* parent);
+
+// In-order traversal print pointer
+void print_in_order(struct AVLNode* root);
+
+// Free avl tree nodes starting at root
+void free_avl_tree(struct AVLNode* root);
+
+// Free avl tree and their data starting at root
+void free_avl_tree_nodes(struct AVLNode* root);
+
+#endif