forked from fbuihuu/libtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.h
85 lines (70 loc) · 2.83 KB
/
avl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* avl.h - this file is part of Anytree.
*
* Copyright (C) 2010 Franck Bui-Huu <[email protected]>
* Copyright (C) 2013 Kuzma Shapran <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2 of the
* License.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#ifndef ANYTREE__AVL__INCLUDED
#define ANYTREE__AVL__INCLUDED
#include <stdint.h>
#include <stddef.h>
/*
* The definition has been stolen from the Linux kernel.
*/
#ifdef __GNUC__
# define avltree_container_of(node, type, member) ({ \
const struct avltree_node *__mptr = (node); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#else
# define avltree_container_of(node, type, member) \
((type *)((char *)(node) - offsetof(type, member)))
#endif /* __GNUC__ */
/*
* AVL tree
*/
struct avltree;
struct avltree_node {
struct avltree *tree;
struct avltree_node *left, *right;
struct avltree_node *parent;
signed balance:3; /* balance factor [-2:+2] */
};
typedef int (*avltree_cmp_fn_t)(const struct avltree_node *, const struct avltree_node *);
struct avltree {
avltree_cmp_fn_t cmp_fn;
unsigned size;
struct avltree_node *root;
struct avltree_node *first, *last;
int height;
};
struct avltree_node *avltree_first(const struct avltree *tree);
struct avltree_node *avltree_last(const struct avltree *tree);
struct avltree_node *avltree_next(const struct avltree_node *node);
struct avltree_node *avltree_prev(const struct avltree_node *node);
struct avltree_node *avltree_lookup(const struct avltree_node *key, const struct avltree *tree);
struct avltree_node *avltree_insert(struct avltree_node *node, struct avltree *tree);
void avltree_remove(struct avltree_node *node, struct avltree *tree);
void avltree_replace(struct avltree_node *old, struct avltree_node *node, struct avltree *tree);
#define avltree_is_empty(TREE) (TREE->size == 0)
#define avltree_size(TREE) (TREE->size)
int avltree_init(struct avltree *tree, avltree_cmp_fn_t cmp);
void avltree_clean(struct avltree *tree);
typedef void (*avltree_call_fn_t)(const struct avltree_node *);
void avltree_foreach(struct avltree *tree, avltree_call_fn_t call);
void avltree_foreach_backward(struct avltree *tree, avltree_call_fn_t call);
#endif /* ANYTREE__AVL__INCLUDED */