-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtree.h
75 lines (61 loc) · 2 KB
/
strtree.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
/*
* strtree.h
*
* Red-Black Tree char* as key version
*
* Reference: Introduction to ALGORITHMS from MIT Press
*
* You must start with a dummy node that contains the minimum
* key value.
*
* This version has a char pointer as the key.
*
*/
#ifndef Strtree_h
#define Strtree_h
#define MAX_NEST 256
typedef struct _StrTreeNode {
struct _StrTreeNode* parent;
struct _StrTreeNode* left;
struct _StrTreeNode* right;
unsigned char color;
unsigned char pad; /* to keep even boundary */
char* key; /* key string (null terminated) */
void* closure; /* anything that related to the key */
} STNode, *StrTreeNode;
#define RED 0
#define Red 0
#define BLACK 1
#define Black 1
#define Right(n) ((n)->right)
#define Left(n) ((n)->left)
#define Parent(n) ((n)->parent)
#define Color(n) ((n)->color)
#define Key(n) ((n)->key)
#define Closure(n) ((n)->closure)
#ifndef NULL
#define NULL (void*)0
#endif
#ifndef True
#define True 1
#define False 0
#endif
typedef void (*STreeApplyProc)(void*);
typedef void (*STreeApplyNodeProc)(StrTreeNode);
typedef int (*STreeApplyCondProc)(void*);
typedef int (*STreeUserCompProc)(char* , char*);
void STreeInit (StrTreeNode* root);
StrTreeNode STreeInsert (StrTreeNode *root, char* key, void* closure);
void* STreeSearch (StrTreeNode root, char* key);
void* STreeSearchByUserComp (StrTreeNode root, char* key, STreeUserCompProc p);
StrTreeNode STreeSearchNode (StrTreeNode root, char* key);
StrTreeNode STreeSearchNext (StrTreeNode node, char* key);
void* STreeDelete (StrTreeNode* root, char* key);
void STreeDealloc (StrTreeNode root, void (*fp)(StrTreeNode));
void STreeDeallocTree (StrTreeNode tree);
void STreeApply (StrTreeNode root, void (*app)(void*));
void STreeApplyNode (StrTreeNode root, void (*app)(StrTreeNode));
int STreeApplyCond (StrTreeNode root, int (*app)(void*));
void* STreeSample (StrTreeNode root);
void STreePrint (StrTreeNode r);
#endif