aboutsummaryrefslogtreecommitdiff
path: root/src/tree.h
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-06-23 07:39:29 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-06-23 07:39:29 +0200
commitc84c3f459919aa8db2a24c8461aab9e547be961f (patch)
tree58a71c95096f386f0b043f64cecbd382ec978348 /src/tree.h
parentca37489b388764b3a8f2d558a9e9a24e3e0d4105 (diff)
downloaddns-c84c3f459919aa8db2a24c8461aab9e547be961f.tar.gz
Comments, white space errors
Diffstat (limited to 'src/tree.h')
-rw-r--r--src/tree.h48
1 files changed, 37 insertions, 11 deletions
diff --git a/src/tree.h b/src/tree.h
index 38b4b38..98019c8 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -4,11 +4,43 @@
* License: MIT
* */
+/**
+ * Binary tree designed to serve as a static lookup table.
+ * On the fly rebalancing is not intended.
+ *
+ * The char* _key is used as a primary key, the tree can
+ * carry any generic payload in void* _data.
+ *
+ * The root does not need special initialization.
+ *
+ * A query with tree_get() returns _data on an exact match,
+ * NULL if no matching key is found.
+ *
+ * To free the memory, do not use free() on root, but
+ * tree_destroy() instead. _key and _data can be freed
+ * by providing _TREE_FREE_DATA | _TREE_FREE_KEY.
+ *
+ * Example:
+ *
+ * struct tree_node* root = NULL;
+ *
+ * // A String is used as data
+ * tree_insert ( &root, "one", "data one" );
+ * tree_insert ( &root, "two", "data two" );
+ *
+ * printf( "%s\n", tree_get( &root, "two" ) );
+ *
+ * tree_destroy ( &root, 0 );
+ *
+ **/
+
#pragma once
#include <stdlib.h>
#include <stdint.h>
+#include <math.h>
+//TODO remove
#include <stdio.h>
#define _TREE_FREE_DATA 0x01
@@ -23,7 +55,11 @@ struct tree_node {
int tree_insert ( struct tree_node** _root, char* _key, void* _data );
-int tree_balance( struct tree_node** _root );
+/**
+ * Inserts the given list into the tree, achieving optimal depth.
+ * Expects a sorted list.
+ * */
+int tree_balanced_insert( struct tree_node** _root, void* _data[], char* _key[], unsigned int _len);
/**
* Returns (void*)node->data on success, NULL on failure
@@ -31,13 +67,3 @@ int tree_balance( struct tree_node** _root );
void* tree_get ( struct tree_node** _root, char* _query );
int tree_destroy( struct tree_node** _root, uint8_t _options );
-
-/**
- * ignore-case alphabetical string compare
- * returns:
- * 0 :: _1 == _2
- * -1 :: _1 < _2
- * +1 :: _1 > _2
- * */
-int string_compare ( char* _1, char* _2 );
-