| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | *@@sourcefile tree.h:
|
|---|
| 4 | * header file for tree.c (red-black balanced binary trees).
|
|---|
| 5 | * See remarks there.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #if __cplusplus
|
|---|
| 9 | extern "C" {
|
|---|
| 10 | #endif
|
|---|
| 11 |
|
|---|
| 12 | #ifndef SFLTREE_INCLUDED // Allow multiple inclusions
|
|---|
| 13 | #define SFLTREE_INCLUDED
|
|---|
| 14 |
|
|---|
| 15 | #if (!defined OS2_INCLUDED) && (!defined _OS2_H) && (!defined __SIMPLES_DEFINED) // changed V0.9.0 (99-10-22) [umoeller]
|
|---|
| 16 | typedef unsigned long BOOL;
|
|---|
| 17 | #define TRUE (BOOL)1
|
|---|
| 18 | #define FALSE (BOOL)0
|
|---|
| 19 |
|
|---|
| 20 | #ifdef __IBMCPP__ // added V0.9.0 (99-10-22) [umoeller]
|
|---|
| 21 | #define APIENTRY _System
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #define __SIMPLES_DEFINED
|
|---|
| 25 | #endif
|
|---|
| 26 |
|
|---|
| 27 | typedef enum { BLACK, RED } nodeColor;
|
|---|
| 28 |
|
|---|
| 29 | /*
|
|---|
| 30 | *@@ TREE:
|
|---|
| 31 | * tree node.
|
|---|
| 32 | *
|
|---|
| 33 | * To use the tree functions, all your structures
|
|---|
| 34 | * must have a TREE structure as their first member.
|
|---|
| 35 | *
|
|---|
| 36 | * Example:
|
|---|
| 37 | *
|
|---|
| 38 | + typedef struct _MYTREENODE
|
|---|
| 39 | + {
|
|---|
| 40 | + TREE tree;
|
|---|
| 41 | + char acMyData[1000];
|
|---|
| 42 | + } MYTREENODE, *PMYTREENODE;
|
|---|
| 43 | *
|
|---|
| 44 | * See tree.c for an introduction to the tree functions.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | typedef struct _TREE
|
|---|
| 48 | {
|
|---|
| 49 | struct _TREE *left,
|
|---|
| 50 | *right,
|
|---|
| 51 | *parent;
|
|---|
| 52 | nodeColor color; // the node's color (BLACK, RED)
|
|---|
| 53 |
|
|---|
| 54 | unsigned long ulKey; // the node's key (data)
|
|---|
| 55 |
|
|---|
| 56 | } TREE, *PTREE;
|
|---|
| 57 |
|
|---|
| 58 | #if defined(__IBMC__) || defined(__IBMCPP__)
|
|---|
| 59 | #define TREEENTRY _Optlink
|
|---|
| 60 | #else
|
|---|
| 61 | // EMX or whatever: doesn't know calling conventions
|
|---|
| 62 | #define TREEENTRY
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | #define STATUS_OK 0
|
|---|
| 66 | #define STATUS_DUPLICATE_KEY -1
|
|---|
| 67 | #define STATUS_INVALID_NODE -2
|
|---|
| 68 |
|
|---|
| 69 | typedef int TREEENTRY FNTREE_COMPARE(unsigned long ul1, unsigned long ul2);
|
|---|
| 70 |
|
|---|
| 71 | // Function prototypes
|
|---|
| 72 | void treeInit(TREE **root);
|
|---|
| 73 |
|
|---|
| 74 | int TREEENTRY treeCompareKeys(unsigned long ul1, unsigned long ul2);
|
|---|
| 75 |
|
|---|
| 76 | int treeInsert(TREE **root,
|
|---|
| 77 | TREE *x,
|
|---|
| 78 | FNTREE_COMPARE *pfnCompare);
|
|---|
| 79 |
|
|---|
| 80 | int treeDelete(TREE **root,
|
|---|
| 81 | TREE *z);
|
|---|
| 82 |
|
|---|
| 83 | TREE* treeFind(TREE *root,
|
|---|
| 84 | unsigned long key,
|
|---|
| 85 | FNTREE_COMPARE *pfnCompare);
|
|---|
| 86 |
|
|---|
| 87 | TREE* treeFirst(TREE *r);
|
|---|
| 88 |
|
|---|
| 89 | TREE* treeLast(TREE *r);
|
|---|
| 90 |
|
|---|
| 91 | TREE* treeNext(TREE *r);
|
|---|
| 92 |
|
|---|
| 93 | TREE* treePrev(TREE *r);
|
|---|
| 94 |
|
|---|
| 95 | TREE** treeBuildArray(TREE* pRoot,
|
|---|
| 96 | unsigned long *pulCount);
|
|---|
| 97 |
|
|---|
| 98 | #endif
|
|---|
| 99 |
|
|---|
| 100 | #if __cplusplus
|
|---|
| 101 | }
|
|---|
| 102 | #endif
|
|---|
| 103 |
|
|---|