bloodhound  0.2.0
AVL self-balancing binary search tree with intrusive nodes.
Public Attributes | List of all members
AvlNode Struct Reference

Intrusive AVL tree node. More...

#include <bloodhound.h>

Public Attributes

AvlNodeleft
 
AvlNoderight
 
signed char balance_factor
 

Detailed Description

Intrusive AVL tree node.

Users should create custom structs that have AvlNode as a member, such as by composition or inheritance (in C++). Although it may be tempting, users should not modify the contents of AvlNode in their own types. For example, the following is a node that holds a const char* key and an int value:

typedef struct Node {
AvlNode node;
const char *key;
int value;
} Node;
AvlTree map;
Node n = {{NULL, NULL, 0}, "Hello, world!", 42};
Node *const previous = (Node*) AvlTree_insert(&map, &n.node);

In C, I recommend placing the AvlNode as the first member of your struct to ensure that casting between your own node types and AvlNode is valid. You can put the AvlNode member at a different spot if you want to, but you'll have to use offsetof() to convert between pointer types.

This is a similar example using inheritance in C++ to allow for easy use of static_cast:

struct Node : AvlNode {
Node(std::string k, int v) : key(std::move(k)), value(v) { }
std::string key;
int value;
}
AvlTree map;
Node n("Hello, world!", 42);
const auto previous = static_cast<Node*>(AvlTree_insert(&map, &n));

The documentation for this struct was generated from the following file: