File: RBTree.h

package info (click to toggle)
gopher 3.0.17.3%2Bnmu1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,476 kB
  • sloc: ansic: 13,895; sh: 1,349; makefile: 318
file content (96 lines) | stat: -rw-r--r-- 2,699 bytes parent folder | download | duplicates (8)
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
86
87
88
89
90
91
92
93
94
95
96
/**********************************************************************
 * $Author: jgoerzen $
 * $Revision: 1.1 $
 * $Date: 2000/08/19 00:28:56 $
 * $Source: /home/jgoerzen/tmp/gopher-umn/gopher/head/object/RBTree.h,v $
 * $State: Exp $
 *
 * Paul Lindner, University of Minnesota CIS.
 *
 * Copyright 1995 by the Regents of the University of Minnesota
 * see the file "Copyright" in the distribution for conditions of use.
 *********************************************************************
 * MODULE: RBTree.h
 * Definition of a Red-Black balanced tree container.
 *********************************************************************
 * Revision History:
 * $Log: RBTree.h,v $
 * Revision 1.1  2000/08/19 00:28:56  jgoerzen
 * Initial revision
 *
 * Revision 3.2  1995/10/31  16:48:21  lindner
 * Better readability, fix bugs..
 *
 * Revision 3.1  1995/06/30  20:31:04  lindner
 * New red-black tree code
 *
 **********************************************************************/

#include "boolean.h"

enum direction {LEFT, RIGHT, NOWHERE };
enum nodecolor {RED, BLACK};

typedef enum direction direction;
typedef enum nodecolor nodecolor;

struct RBTree_node_struct {
     char *data;

     struct RBTree_node_struct *l_child;
     struct RBTree_node_struct *r_child;

     nodecolor rb_right;
     nodecolor rb_left;
     
     direction last_pass;

};

typedef struct RBTree_node_struct RBTree_node;


#define RBTsetRBright(a,v) ((a)->rb_right=(v))   /* Sets ptr to black or Red */
#define RBTsetRBleft(a,v)  ((a)->rb_left=(v))

#define RBTgetRBright(a)  ((a)->rb_right)
#define RBTgetRBleft(a)   ((a)->rb_left)

#define RBTgetDirection(a)   ((a)->last_pass)
#define RBTsetDirection(a,v) ((a)->last_pass=(v))

#define RBTattachLeftNode(a,v)  ((a)->l_child = (v))
#define RBTattachRightNode(a,v) ((a)->r_child = (v))

#define RBTgetRight(a)       ((a)->r_child)
#define RBTgetLeft(a)        ((a)->l_child)

#define RBTaddData(a,v)      ((a)->data=(v))
#define RBTgetData(a)        ((a)->data)

/**********************************************************************/

struct RBTree_struct {
     RBTree_node *entry;
     
     int    (*cmpfn)();
     void   (*initfn)();
     void   (*destroyfn)();
     char * (*copyfn)();

};

typedef struct RBTree_struct RBTree;

/*** For the top level tree structure ***/
#define RBTsetEntry(a,v)     ((a)->entry = (v))
#define RBTgetEntry(a)       ((a)->entry)

#define RBTcmp(a,b,c) ((a)->cmpfn(b,c))
#define RBTdestroyfcn(a,b) ((a)->destroyfn(RBTgetData(b)))

RBTree *RBTnew();
void    RBTdestroy(RBTree *rbt);
void    RBTtraverse(RBTree *rbt, void (*func)());
char   *RBTfind(RBTree *rbt, char* data);
void    RBTinsert(RBTree *rbt, char *data);