rbtree.c: make rbnode an ADT.
This commit is contained in:
parent
41a253a00a
commit
e7ebbd30ec
3 changed files with 15 additions and 39 deletions
|
|
@ -4,10 +4,6 @@ Red-Black Tree
|
||||||
Macros
|
Macros
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
`RB_RED`::
|
|
||||||
`RB_BLACK`::
|
|
||||||
Used to mark the color of a `rbnode`.
|
|
||||||
|
|
||||||
`RBTREE_INIT`::
|
`RBTREE_INIT`::
|
||||||
Initialize a `rbtree` structure. +
|
Initialize a `rbtree` structure. +
|
||||||
should only be used with the declaration like:
|
should only be used with the declaration like:
|
||||||
|
|
@ -21,24 +17,6 @@ the arguments are pointers to callback functions, in order: `delete_fn`, `update
|
||||||
Data structures
|
Data structures
|
||||||
~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
* `rbnode`
|
|
||||||
+
|
|
||||||
--
|
|
||||||
The binary tree node.
|
|
||||||
|
|
||||||
`key`::
|
|
||||||
The key of this node
|
|
||||||
|
|
||||||
`child`::
|
|
||||||
pointers to the left and right child to this node
|
|
||||||
|
|
||||||
`color`::
|
|
||||||
the color, should be a value of `RB_BLACK` or `RB_RED`
|
|
||||||
--
|
|
||||||
|
|
||||||
++++
|
|
||||||
++++
|
|
||||||
|
|
||||||
* `rbtree`
|
* `rbtree`
|
||||||
+
|
+
|
||||||
--
|
--
|
||||||
|
|
@ -48,16 +26,16 @@ Structure that holds a tree of nodes
|
||||||
Pointer to the node that is the root of the tree.
|
Pointer to the node that is the root of the tree.
|
||||||
|
|
||||||
`delete_fn`::
|
`delete_fn`::
|
||||||
Pointer to the function that should handle the delete routines for the `rbnode->key` pointer.
|
Pointer to the function that should handle the delete routines for the `key` pointer.
|
||||||
|
|
||||||
`update_fn`::
|
`update_fn`::
|
||||||
Pointer to the function that is called when the implementation performs an update of an `rbnode->key` pointer. +
|
Pointer to the function that is called when the implementation performs an update of an `key` pointer. +
|
||||||
The function gets the following information passed in order: old pointer, new pointer.
|
The function gets the following information passed in order: old pointer, new pointer.
|
||||||
|
|
||||||
NOTE: You may only need this if you store the data in another structure and has to keep it synchronized with the RB-tree.
|
NOTE: You may only need this if you store the data in another structure and has to keep it synchronized with the RB-tree.
|
||||||
|
|
||||||
`cmp_fn`::
|
`cmp_fn`::
|
||||||
Pointer to the function that is used to compare two `rbnode->key` pointers. +
|
Pointer to the function that is used to compare two `key` pointers. +
|
||||||
Shall return a value greater than zero if 'ptr1' > 'ptr2', a value less than zero if 'ptr1' < 'ptr2' and zero if 'ptr1' == 'ptr2'.
|
Shall return a value greater than zero if 'ptr1' > 'ptr2', a value less than zero if 'ptr1' < 'ptr2' and zero if 'ptr1' == 'ptr2'.
|
||||||
--
|
--
|
||||||
|
|
||||||
|
|
@ -83,7 +61,7 @@ NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ens
|
||||||
|
|
||||||
`rbtree_walk()`::
|
`rbtree_walk()`::
|
||||||
|
|
||||||
Walks the tree in-order and passes a pointer to `rbnode->key`
|
Walks the tree in-order and passes a pointer to `key`
|
||||||
for the given node to the 'action' callback function.
|
for the given node to the 'action' callback function.
|
||||||
|
|
||||||
`rbtree_search()`::
|
`rbtree_search()`::
|
||||||
|
|
@ -91,8 +69,6 @@ NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ens
|
||||||
Searches the tree for 'key'. +
|
Searches the tree for 'key'. +
|
||||||
Returns a pointer to the node if found else `NULL`.
|
Returns a pointer to the node if found else `NULL`.
|
||||||
|
|
||||||
NOTE: the function uses the `rbtree->cmp_fn` function to compare 'key' with a `rbnode->key`.
|
|
||||||
|
|
||||||
`rbtree_is_empty()`::
|
`rbtree_is_empty()`::
|
||||||
|
|
||||||
Checks if a tree is empty, retruns zero if the tree is empty, nonzero otherwise.
|
Checks if a tree is empty, retruns zero if the tree is empty, nonzero otherwise.
|
||||||
|
|
|
||||||
10
src/rbtree.c
10
src/rbtree.c
|
|
@ -19,6 +19,16 @@
|
||||||
#include "xalloc.h"
|
#include "xalloc.h"
|
||||||
#include "rbtree.h"
|
#include "rbtree.h"
|
||||||
|
|
||||||
|
#define RB_RED 0
|
||||||
|
#define RB_BLACK 1
|
||||||
|
|
||||||
|
/* node definition */
|
||||||
|
typedef struct _rbn {
|
||||||
|
const void *key;
|
||||||
|
struct _rbn *child[2];
|
||||||
|
unsigned char color;
|
||||||
|
} rbnode;
|
||||||
|
|
||||||
#define is_red(n) ((n) != NULL && (n)->color == RB_RED)
|
#define is_red(n) ((n) != NULL && (n)->color == RB_RED)
|
||||||
#define swap(n,d,q) ((n)->child[(n)->child[d] == (q)])
|
#define swap(n,d,q) ((n)->child[(n)->child[d] == (q)])
|
||||||
|
|
||||||
|
|
|
||||||
12
src/rbtree.h
12
src/rbtree.h
|
|
@ -11,20 +11,10 @@
|
||||||
#ifndef __RBTREE_H
|
#ifndef __RBTREE_H
|
||||||
#define __RBTREE_H
|
#define __RBTREE_H
|
||||||
|
|
||||||
#define RB_RED 0
|
|
||||||
#define RB_BLACK 1
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/* node definition */
|
|
||||||
typedef struct _rbn {
|
|
||||||
const void *key;
|
|
||||||
struct _rbn *child[2];
|
|
||||||
unsigned char color;
|
|
||||||
} rbnode;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
rbnode *root;
|
struct _rbn *root;
|
||||||
/* user defined operations */
|
/* user defined operations */
|
||||||
void (*delete_fn)(void *);
|
void (*delete_fn)(void *);
|
||||||
void (*update_fn)(void *, void *);
|
void (*update_fn)(void *, void *);
|
||||||
|
|
|
||||||
Reference in a new issue