source: trunk/include/helpers/tree.h@ 15

Last change on this file since 15 was 14, checked in by umoeller, 25 years ago

Major updates; timers, LVM, miscellaneous.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1
2/*
3 *@@sourcefile tree.h:
4 * header file for tree.c (red-black balanced binary trees).
5 * See remarks there.
6 */
7
8/*
9 * Written: 97/11/18 Jonathan Schultz <jonathan@imatix.com>
10 * Revised: 98/12/08 Jonathan Schultz <jonathan@imatix.com>
11 *
12 * Copyright (C) 1991-99 iMatix Corporation.
13 * Copyright (C) 2000 Ulrich M”ller.
14 * This file is part of the "XWorkplace helpers" source package.
15 * This is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published
17 * by the Free Software Foundation, in version 2 as it comes in the
18 * "COPYING" file of the XWorkplace main distribution.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25
26/* ----------------------------------------------------------------<Prolog>-
27 Name: sfltree.h
28 Title: Linked-list functions
29 Package: Standard Function Library (SFL)
30
31 Written: 97/11/18 Jonathan Schultz <jonathan@imatix.com>
32 Revised: 98/01/03 Jonathan Schultz <jonathan@imatix.com>
33
34 Synopsis: Provides functions to maintain 'Red-Black' balanced binary
35 trees. You can use these functions to work with trees of any
36 structure. To make this work, all structures must start with
37 the following: "void *left, *right, *parent; TREE_COLOUR
38 colour;". All trees need a pointer to the root of type TREE
39 which should be initialised with treeInit - you can test
40 whether a tree is empty by comparing its root with TREE_NULL.
41 The order of nodes in the tree is determined by calling a
42 node comparison function provided by the caller - this
43 accepts two node pointers and returns zero if the two nodes
44 are equal, -1 if the first is smaller and 1 if the first is
45 larger.
46
47 Copyright: Copyright (c) 1991-99 iMatix Corporation
48 License: This is free software; you can redistribute it and/or modify
49 it under the terms of the SFL License Agreement as provided
50 in the file LICENSE.TXT. This software is distributed in
51 the hope that it will be useful, but without any warranty.
52 ------------------------------------------------------------------</Prolog>-*/
53
54#if __cplusplus
55extern "C" {
56#endif
57
58#ifndef SFLTREE_INCLUDED // Allow multiple inclusions
59 #define SFLTREE_INCLUDED
60
61 #if (!defined OS2_INCLUDED) && (!defined _OS2_H) && (!defined __SIMPLES_DEFINED) // changed V0.9.0 (99-10-22) [umoeller]
62 typedef unsigned long BOOL;
63 #define TRUE (BOOL)1
64 #define FALSE (BOOL)0
65
66 #ifdef __IBMCPP__ // added V0.9.0 (99-10-22) [umoeller]
67 #define APIENTRY _System
68 #endif
69
70 #define __SIMPLES_DEFINED
71 #endif
72
73 // Red-Black tree description
74 typedef enum {BLACK, RED} TREE_COLOUR;
75
76 /*
77 *@@ TREE:
78 * tree node.
79 *
80 * To use the tree functions, all your structures
81 * must have a TREE structure as their first member.
82 *
83 * Example:
84 *
85 + typedef struct _MYTREENODE
86 + {
87 + TREE tree;
88 + char acMyData[1000];
89 + } MYTREENODE, *PMYTREENODE;
90 *
91 * See tree.c for an introduction to the tree functions.
92 */
93
94 typedef struct _TREE
95 {
96 struct _TREE *left,
97 *right,
98 *parent;
99 unsigned long id;
100 TREE_COLOUR colour;
101 } TREE, *PTREE;
102
103 // The tree algorithm needs to know how to sort the data.
104 // It does this using a functions provided by the calling program.
105 // This must return:
106 // -- 0: t1 == t2
107 // -- -1: t1 < t2
108 // -- +1: t1 > t2
109 typedef int (FNTREE_COMPARE_IDS) (unsigned long id1, unsigned long id2);
110 typedef int (FNTREE_COMPARE_NODES) (TREE *t1, TREE *t2);
111 typedef int (FNTREE_COMPARE_DATA) (TREE *t1, void *pData);
112
113 // Define a function type for use with the tree traversal function
114 typedef void (TREE_PROCESS) (TREE *t, void *pUser);
115
116 // Global variables
117 extern TREE TREE_EMPTY;
118
119 // Function prototypes
120 void treeInit(TREE **root);
121
122 int treeInsertID(TREE **root,
123 TREE *tree,
124 FNTREE_COMPARE_IDS *comp,
125 BOOL fAllowDuplicates);
126
127 int treeInsertNode(TREE **root,
128 TREE *tree,
129 FNTREE_COMPARE_NODES *comp,
130 BOOL fAllowDuplicates);
131
132 int treeDelete(TREE **root,
133 TREE *tree);
134
135 /* void *treeFindEQID (TREE **root,
136 TREE *tree,
137 TREE_COMPARE *comp); */
138 /* void *treeFindGEID (TREE **root,
139 TREE *tree,
140 FNTREE_COMPARE_IDS *comp); */
141
142 void* treeFindEQNode(TREE **root,
143 TREE *nodeFind,
144 FNTREE_COMPARE_NODES *comp);
145 void* treeFindLTNode(TREE **root,
146 TREE *nodeFind,
147 FNTREE_COMPARE_NODES *comp);
148 void* treeFindLENode(TREE **root,
149 TREE *nodeFind,
150 FNTREE_COMPARE_NODES *comp);
151 void* treeFindGTNode(TREE **root,
152 TREE *nodeFind,
153 FNTREE_COMPARE_NODES *comp);
154 void* treeFindGENode(TREE **root,
155 TREE *nodeFind,
156 FNTREE_COMPARE_NODES *comp);
157
158 void* treeFindEQID(TREE **root,
159 unsigned long idFind,
160 FNTREE_COMPARE_IDS *comp);
161 void* treeFindLTID(TREE **root,
162 unsigned long idFind,
163 FNTREE_COMPARE_IDS *comp);
164 void* treeFindLEID(TREE **root,
165 unsigned long idFind,
166 FNTREE_COMPARE_IDS *comp);
167 void* treeFindGTID(TREE **root,
168 unsigned long idFind,
169 FNTREE_COMPARE_IDS *comp);
170 void* treeFindGEID(TREE **root,
171 unsigned long idFind,
172 FNTREE_COMPARE_IDS *comp);
173
174 void* treeFindEQData(TREE **root,
175 void *pData,
176 FNTREE_COMPARE_DATA *comp);
177
178 void treeTraverse (TREE *tree,
179 TREE_PROCESS *process,
180 void *pUser,
181 int method);
182 void *treeNext (TREE *tree);
183 void *treePrev (TREE *tree);
184 void *treeFirst (TREE *tree);
185 void *treeLast (TREE *tree);
186
187 // Return codes
188 #define TREE_OK 0
189 #define TREE_DUPLICATE 1
190 #define TREE_INVALID_NODE 2
191
192 // Macros
193 #define TREE_NULL &TREE_EMPTY
194
195#endif
196
197#if __cplusplus
198}
199#endif
200
Note: See TracBrowser for help on using the repository browser.