CObject
|
Implements the CITree interface with a binary tree/heap. More...
#include <CCBinaryTree.h>
Public Member Functions | |
const struct CCBinaryTree_VTable * | CCBinaryTree_GetVTable () |
CError | CCBinaryTree (struct CCBinaryTree *self, size_t element_size, size_t max_size, signed char(*compare)(const void *, const void *), size_t key_size) |
CITreeError | CITree_Push (struct CITree *self, const void *element, const void *key) |
CITreeError | CITree_Pop (struct CITree *self, void *element) |
CITreeError | CITree_DeleteElement (struct CITree *self, void *element) |
CITreeError | CITree_Peek (struct CITree *self, void *element) |
CITreeError | CITree_Get (struct CITree *self, void *element, size_t index) |
CITreeError | CITree_Delete (struct CITree *self, void *element, size_t index) |
size_t | CITree_Size (struct CITree *self) |
size_t | CITree_MaxSize (struct CITree *self) |
void | CITree_Clear (struct CITree *self) |
Data Fields | |
struct CObject | cobject |
struct CITree | citree |
struct CCArrayList | tree_backend |
unsigned char * | swap_space_1 |
unsigned char * | key_space_1 |
unsigned char * | key_space_2 |
signed char(* | compare )(const void *, const void *) |
size_t | index |
size_t | element_size |
size_t | key_size |
size_t | max_size |
struct CInterface | interface |
Implements the CITree interface with a binaray tree/heap. Data and keys put into the tree are copied by value. This implementation allows arbitrary keys to be used when inserting into the tree. The root of the tree is always the node with the key that evaluates to be smaller than all other keys. The evaluation function for keys is passed into the constructor as a function argument. For example, the tree below uses integers for keys, can have a maximum of 8 nodes, and will save integers in each of its nodes. The code puts an item in with value 0 and key 0, then an item with value 5 and key -3. This will put the -3:5 key:value pair at the root of tree. Next time CITree_Pop() is called, that is the item that gets removed, since its key will evaluate as being the smallest.
CError CCBinaryTree | ( | struct CCBinaryTree * | self, |
size_t | element_size, | ||
size_t | max_size, | ||
signed char(*)(const void *, const void *) | compare, | ||
size_t | key_size | ||
) |
Construct a binary tree. The tree's memory is allocated using CMalloc defined in Class.h. When the tree is no longer needed, CDestroy() must be called on it.
self | The tree. |
element_size | The number of bytes of data per node. This is how many bytes are copied into a node when CITree_Push() is called. This must be greater than or equal to one. |
max_size | The maximum number of nodes in the tree. This must be greater than or equal to 2. |
compare | A function pointer used for comparing keys. The prototype of the function is signed char compare( const void* key1, const void* key2 );
|
key_size | The size of keys in bytes. Keys are copied into the tree when CITree_Push() is called. This must be greater than or equal to 1. |
|
inherited |
Reset the tree to have zero ndoes in it. All data in the tree is lost.
self | The tree. |
|
inherited |
Delete at an arbitrary index in the tree. Indexing is done such that at any index i, left_child(i) = 2i and right_child(i) = 2i + 1.
element | Pointer to location where data at the index will be copied to. Use NULL to ignore and simply delete from the tree. |
index | Location in the tree to delete data from |
|
inherited |
Delete the node in the three who's element matches the input parameter element. This function has O(n) execution time.
self | The tree. |
element | The data pointed to by this is the search critera. The function will search for a node whos element matches the data pointed to by this and remove it. |
|
inherited |
Look at an arbitrary index in the tree. Indexing is done such that at any index i, left_child(i) = 2i + 1 and right_child(i) = 2i + 2.
element | Pointer to location where data at the index will be copied to. |
index | Location in the tree to copy data from |
|
inherited |
The maximum number of nodes which can be added to the tree.
self | The tree. |
|
inherited |
Get the element at the root of the tree without removing it.
self | The tree. |
element | A pointer to the location where data will be copied out of the tree. |
|
inherited |
Get the root of the tree and remove it. The tree will then have its nodes adjusted such that the heap property is maintained.
self | The tree. |
element | Pointer to location where that data at the root of the tree will be copied too. Use NULL to safely ignore. |
|
inherited |
Put an element into the tree by copy and move it up the tree such that the heap property is not violated using the input param key to evaluate the heap property. The heap property requires the root of the tree has the key which evaluates to be less than all other keys in the tree.
self | The tree. |
element | Pointer to data that will be copied into the three. |
key | Pointer to the key for the data added to the tree. |
|
inherited |
Get the current size of tree, ie, how many nodes have been added to the tree.
self | The tree. |