plibsys 0.0.5
|
Binary tree data structure. More...
Go to the source code of this file.
Typedefs | |
typedef struct PTree_ | PTree |
Tree opaque data structure. | |
typedef enum PTreeType_ | PTreeType |
Internal data organization algorithm for PTree. | |
Enumerations | |
enum | PTreeType_ { P_TREE_TYPE_BINARY = 0 , P_TREE_TYPE_RB = 1 , P_TREE_TYPE_AVL = 2 } |
Internal data organization algorithm for PTree. More... | |
Functions | |
P_LIB_API PTree * | p_tree_new (PTreeType type, PCompareFunc func) |
Initializes new PTree. | |
P_LIB_API PTree * | p_tree_new_with_data (PTreeType type, PCompareDataFunc func, ppointer data) |
Initializes new PTree with additional data. | |
P_LIB_API PTree * | p_tree_new_full (PTreeType type, PCompareDataFunc func, ppointer data, PDestroyFunc key_destroy, PDestroyFunc value_destroy) |
Initializes new PTree with additional data and memory management. | |
P_LIB_API void | p_tree_insert (PTree *tree, ppointer key, ppointer value) |
Inserts a new key-value pair into a tree. | |
P_LIB_API pboolean | p_tree_remove (PTree *tree, pconstpointer key) |
Removes a key from a tree. | |
P_LIB_API ppointer | p_tree_lookup (PTree *tree, pconstpointer key) |
Lookups a value by a given key. | |
P_LIB_API void | p_tree_foreach (PTree *tree, PTraverseFunc traverse_func, ppointer user_data) |
Iterates in-order through the tree nodes. | |
P_LIB_API void | p_tree_clear (PTree *tree) |
Clears a tree. | |
P_LIB_API PTreeType | p_tree_get_type (const PTree *tree) |
Gets a tree algorithm type. | |
P_LIB_API pint | p_tree_get_nnodes (const PTree *tree) |
Gets node count. | |
P_LIB_API void | p_tree_free (PTree *tree) |
Frees a previously initialized tree object. | |
Binary tree data structure.
PTree represents a binary search tree structure for faster lookup than a plain array or a list. It has average O(logN) time complexity to search a key-value pair, and O(N) in the worst case (when a tree is degenerated into the list).
Currently PTree supports the following tree types:
Use p_tree_new(), or its detailed variations like p_tree_new_with_data() and p_tree_new_full() to create a tree structure. Take attention that a caller owns the key and the value data passed when inserting new nodes, so you should manually free the memory after the tree usage. Or you can provide destroy notification functions for the keys and the values separately.
New key-value pairs can be inserted with p_tree_insert() and removed with p_tree_remove().
Use p_tree_lookup() to find the value by a given key. You can also traverse the tree in-order with p_tree_foreach().
Release memory with p_tree_free() or clear a tree with p_tree_clear(). Keys and values would be destroyed only if the corresponding notification functions were provided.
Note: all operations with the tree are non-recursive, only iterative calls are used.
Definition in file ptree.h.
enum PTreeType_ |
Clears a tree.
tree | PTree to clear. |
All the keys will be deleted. Key and value destroy functions would be called on every node if any of them was provided.
P_LIB_API void p_tree_foreach | ( | PTree * | tree, |
PTraverseFunc | traverse_func, | ||
ppointer | user_data ) |
Iterates in-order through the tree nodes.
tree | A tree to traverse. |
traverse_func | Function for traversing. |
user_data | Additional (maybe NULL) user-provided data for the traverse_func. |
The tree should not be modified while traversing. The internal tree structure can be modified along the traversing process, so keep it in mind for concurrent access.
Frees a previously initialized tree object.
tree | PTree object to free. |
All the keys will be deleted. Key and value destroy functions would be called on every node if any of them was provided.
Gets node count.
tree | PTree to get node count for. |
If the tree is empty or an invalid pointer is given it returns 0.
Gets a tree algorithm type.
tree | PTree object to get the type for. |
Inserts a new key-value pair into a tree.
tree | PTree to insert a node in. |
key | Key to insert. |
value | Value corresponding to the given key. |
If the key already exists in the tree then it will be replaced with the new one. If a key destroy function was provided it would be called on the old key. If a value destroy function was provided it would be called on the old value.
P_LIB_API ppointer p_tree_lookup | ( | PTree * | tree, |
pconstpointer | key ) |
Lookups a value by a given key.
tree | PTree to lookup in. |
key | Key to lookup. |
P_LIB_API PTree * p_tree_new | ( | PTreeType | type, |
PCompareFunc | func ) |
P_LIB_API PTree * p_tree_new_full | ( | PTreeType | type, |
PCompareDataFunc | func, | ||
ppointer | data, | ||
PDestroyFunc | key_destroy, | ||
PDestroyFunc | value_destroy ) |
Initializes new PTree with additional data and memory management.
type | Tree algorithm type to use, can't be changed later. |
func | Key compare function. |
data | Data to be passed to func along with the keys. |
key_destroy | Function to call on every key before the node destruction, maybe NULL. |
value_destroy | Function to call on every value before the node destruction, maybe NULL. |
Upon every node destruction the corresponding key and value functions would be called.
P_LIB_API PTree * p_tree_new_with_data | ( | PTreeType | type, |
PCompareDataFunc | func, | ||
ppointer | data ) |
Initializes new PTree with additional data.
type | Tree algorithm type to use, can't be changed later. |
func | Key compare function. |
data | Data to be passed to func along with the keys. |
The caller takes ownership of all the keys and the values passed to the tree.
P_LIB_API pboolean p_tree_remove | ( | PTree * | tree, |
pconstpointer | key ) |
Removes a key from a tree.
tree | PTree to remove a key from. |
key | A key to lookup. |
If a key destroy function was provided it would be called on the key. If a value destroy function was provided it would be called on the old value.