2 Generic C code for red-black trees.
3 Copyright (C) 2000 James S. Plank,
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
21 /* Revision 1.2. Jim Plank */
23 /* Original code by Jim Plank (plank@cs.utk.edu) */
24 /* modified for THINK C 6.0 for Macintosh by Chris Bartley */
29 typedef struct rb_status
{
31 unsigned internal
: 1 ;
37 /* Main rb_node. You only ever use the fields
47 typedef struct rb_node
{
50 struct rb_node
*flink
;
51 struct rb_node
*blink
;
55 struct rb_node
*right
;
59 struct rb_node
*parent
;
76 typedef int Rb_compfn(const void *, const void *);
79 extern Rb_node
make_rb(); /* Creates a new rb-tree */
81 /* Creates a node with key key and val val and inserts it into the tree.
82 rb_insert uses strcmp() as comparison funcion. rb_inserti uses <>=,
83 rb_insertg uses func() */
85 extern Rb_node
rb_insert(Rb_node tree
, const char *key
, void *val
);
86 extern Rb_node
rb_inserti(Rb_node tree
, int ikey
, void *val
);
87 extern Rb_node
rb_insertp(Rb_node tree
, const void *key
, void *val
);
88 extern Rb_node
rb_insertg(Rb_node tree
, const void *key
, void *val
,
92 /* returns an external node in t whose value is equal
93 k or whose value is the smallest value greater than k. */
95 extern Rb_node
rb_find_key(Rb_node root
, const char *key
);
96 extern Rb_node
rb_find_ikey(Rb_node root
, int ikey
);
97 extern Rb_node
rb_find_pkey(Rb_node root
, const void *key
);
98 extern Rb_node
rb_find_gkey(Rb_node root
, const void *key
, Rb_compfn
*func
);
101 /* Works just like the find_key versions only it returns whether or not
102 it found the key in the integer variable found */
104 extern Rb_node
rb_find_key_n(Rb_node root
, const char *key
, int *found
);
105 extern Rb_node
rb_find_ikey_n(Rb_node root
, int ikey
, int *found
);
106 extern Rb_node
rb_find_pkey_n(Rb_node root
, const void *key
, int *found
);
107 extern Rb_node
rb_find_gkey_n(Rb_node root
, const void *key
,
108 Rb_compfn
*func
, int *found
);
111 /* Creates a node with key key and val val and inserts it into the
112 tree before/after node nd. Does not check to ensure that you are
113 keeping the correct order */
115 extern Rb_node
rb_insert_b(Rb_node nd
, const void *key
, void *val
);
116 extern Rb_node
rb_insert_a(Rb_node nd
, const void *key
, void *val
);
119 extern void rb_delete_node(Rb_node node
); /* Deletes and frees a node (but
120 not the key or val) */
121 extern void rb_free_tree(Rb_node root
); /* Deletes and frees an entire tree */
123 extern void *rb_val(Rb_node node
); /* Returns node->v.val -- this is to shut
126 extern int rb_nblack(Rb_node n
); /* returns # of black nodes in path from
128 int rb_plength(Rb_node n
); /* returns the # of nodes in path from
131 #define rb_first(n) (n->c.list.flink)
132 #define rb_last(n) (n->c.list.blink)
133 #define rb_next(n) (n->c.list.flink)
134 #define rb_prev(n) (n->c.list.blink)
135 #define rb_empty(t) (t->c.list.flink == t)
137 #define rb_nil(t) (t)
140 #define rb_traverse(ptr, lst) \
141 for(ptr = rb_first(lst); ptr != rb_nil(lst); ptr = rb_next(ptr))
143 #endif /* LIBTU_RB_H */