/**
* This element is a member of a avl-tree. It must be contained in all
* larger structs that should be put into a tree.
*/
struct avl_node {
/**
* Linked list node for supporting easy iteration and multiple
* elments with the same key.
*
* this must be the first element of an avl_node to
* make casting for lists easier
*/
struct list_head list;
/**
* Pointer to parent node in tree, NULL if root node
*/
struct avl_node *parent;
/**
* Pointer to left child
*/
struct avl_node *left;
/**
* Pointer to right child
*/
struct avl_node *right;
/**
* pointer to key of node
*/
const void *key;
/**
* balance state of AVL tree (0,-1,+1)
*/
signed char balance;
/**
* true if first of a series of nodes with same key
*/
bool leader;
};
/**
* This struct is the central management part of an avl tree.
* One of them is necessary for each avl_tree.
*/
struct avl_tree {
/**
* Head of linked list node for supporting easy iteration
* and multiple elments with the same key.
*/
struct list_head list_head;
/**
* pointer to the root node of the avl tree, NULL if tree is empty
*/
struct avl_node *root;
/**
* number of nodes in the avl tree
*/
unsigned int count;
/**
* true if multiple nodes with the same key are
* allowed in the tree, false otherwise
*/
bool allow_dups;
/**
* pointer to the tree comparator
*
* First two parameters are keys to compare,
* third parameter is a copy of cmp_ptr
*/
avl_tree_comp comp;
/**
* custom pointer delivered to the tree comparator
*/
void *cmp_ptr;
};
初始化
#define AVL_TREE(_name, _comp, _allow_dups, _cmp_ptr)
/**
* Initialize a new avl_tree struct
* @param tree pointer to avl-tree
* @param comp pointer to comparator for the tree
* @param allow_dups true if the tree allows multiple elements with the same
* @param ptr custom parameter for comparator
*/
void avl_init(struct avl_tree *tree, avl_tree_comp comp,
bool allow_dups, void *ptr)
基本操作
加入
/**
* Inserts an avl_node into a tree
* @param tree pointer to tree
* @param new pointer to node
* @return 0 if node was inserted successfully, -1 if it was not inserted
* because of a key collision
*/
int avl_insert(struct avl_tree *tree, struct avl_node *new)
删除
/**
* Inserts an avl_node into a tree
* @param tree pointer to tree
* @param new pointer to node
* @return 0 if node was inserted successfully, -1 if it was not inserted
* because of a key collision
*/
int avl_insert(struct avl_tree *tree, struct avl_node *new)
查找
/**
* Finds a node in an avl-tree with a certain key
* @param tree pointer to avl-tree
* @param key pointer to key
* @return pointer to avl-node with key, NULL if no node with
* this key exists.
*/
struct avl_node * avl_find(const struct avl_tree *tree, const void *key)
/**
* Finds the first node in an avl-tree with a key greater or equal
* than the specified key
* @param tree poin
平衡二叉树(avl.c/h)数据结构/** * This element is a member of a avl-tree. It must be contained in all * larger structs that should be put into a tree. */struct avl_node { /** * Linked list no