tree.h
4.69 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_TREE_H__
#define __TR_TREE_H__
#include "trbase.h"
#define TR_TREE_RIGHT(node) (NULL!=(node)?(node)->right:NULL)
#define TR_TREE_LEFT(node) (NULL!=(node)?(node)->left:NULL)
#define TR_TREE_PARENT(node) (NULL!=(node)?(node)->parent:NULL)
#define TR_TREE_CHILD(node) \
(NULL==TR_TREE_RIGHT((node))?TR_TREE_LEFT((node)):TR_TREE_RIGHT((node)))
#define TR_TREE_SIBLING(node) \
(NULL!=TR_TREE_PARENT((node))? \
((node)==TR_TREE_PARENT((node))->left? \
TR_TREE_PARENT((node))->right: \
TR_TREE_PARENT((node))->left): \
NULL)
#define TR_TREE_GRANDPARENT(node) (TR_TREE_PARENT((node))->parent)
#define TR_TREE_UNCLE(node) \
((node)->parent == (node)->parent->parent->left? \
(node)->parent->parent->right: \
(node)->parent->parent->left)
#define TR_TREE_REPLACE_NODE(root, node1, node2) \
if (NULL != (node1)->parent) { \
if ((node1) == (node1)->parent->left) { \
(node1)->parent->left = (node2); \
} else { \
(node1)->parent->right = (node2); \
} \
} else { \
*(root) = (node2); \
} \
if (NULL != (node2)) { \
(node2)->parent = (node1)->parent; \
}
#define TR_TREE_ROT_RELCHILD_right(node) ((node)->left)
#define TR_TREE_ROT_RELCHILD_CHILD_right (relChild->right)
#define TR_TREE_ROT_RELCHILD_left(node) ((node)->right)
#define TR_TREE_ROT_RELCHILD_CHILD_left (relChild->left)
#define TR_TREE_ROTATE(lr, _this, node) \
if (NULL != (node)) { \
TR_Tree _node = (node); \
TR_Tree relChild = TR_TREE_ROT_RELCHILD_##lr(_node); \
TR_Tree relChildLvl2 = TR_TREE_ROT_RELCHILD_##lr(_node)->lr; \
relChild->lr = _node; \
relChild->parent = _node->parent; \
TR_TREE_ROT_RELCHILD_##lr(_node) = relChildLvl2; \
if (NULL != relChildLvl2) { \
relChildLvl2->parent = _node; \
} \
if (NULL != _node->parent) { \
if (_node->parent->left == _node) { \
_node->parent->left = relChild; \
} else { \
_node->parent->right = relChild; \
} \
} else { \
*(_this) = relChild; \
} \
_node->parent = relChild; \
}
typedef enum {rbBlack=1, rbRed=2} TR_rbColor;
#define TR_TREE_NODE_BLACK(node) (NULL == (node) || rbBlack == (node)->color)
#define TR_TREE_NODE_RED(node) (NULL == (node) || rbRed == (node)->color)
#define TR_TREE_NODE_STRICT_BLACK(node) (NULL != (node) && rbBlack == (node)->color)
#define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color)
#define TR_TREE_INORDER_SUCC(node, succ) \
succ = (node)->right; \
while (NULL != succ->left) { \
succ = succ->left; \
}
TR_CLASS(TR_Tree) {
void * data;
TR_rbColor color;
TR_Tree parent;
TR_Tree left;
TR_Tree right;
};
typedef int (*TR_TreeComp)(const void *, const void *);
typedef void (*TR_TreeAction)(const void *, const int);
void * TR_treeFind(TR_Tree, const void *, TR_TreeComp);
void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp);
void * TR_treeDelete(TR_Tree *, const void *, TR_TreeComp);
void TR_treeWalk(TR_Tree, TR_TreeAction);
void TR_treeDestroy(TR_Tree *, TR_TreeAction);
#endif // __TR_TREE_H__
// vim: set ts=4 sw=4: