You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
5.0 KiB
130 lines
5.0 KiB
/**
|
|
* \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_MACROS_H__
|
|
#define __TR_TREE_MACROS_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_RCLD_right(node) ((node)->left)
|
|
#define TR_TREE_ROT_RCLD_left(node) ((node)->right)
|
|
|
|
#define TR_TREE_ROTATE(lr, root, node) \
|
|
if (NULL != (node)) { \
|
|
void * stPar = node->parent; \
|
|
void * relCld = TR_TREE_ROT_RCLD_##lr(node); \
|
|
void * relCCld = TR_TREE_ROT_RCLD_##lr(node)->lr; \
|
|
void * nLeft_p = &TR_TREE_ROT_RCLD_##lr(node); \
|
|
if (NULL != relCCld) { \
|
|
TR_TREE_ROT_RCLD_##lr(node)->lr->parent = node; \
|
|
} \
|
|
TR_TREE_ROT_RCLD_##lr(node)->lr = node; \
|
|
if (NULL != node->parent) { \
|
|
if (node->parent->left == node) { \
|
|
node->parent->left = relCld; \
|
|
} else { \
|
|
node->parent->right = relCld; \
|
|
} \
|
|
} else { \
|
|
*(root) = relCld; \
|
|
} \
|
|
node->parent = relCld; \
|
|
TR_TREE_ROT_RCLD_##lr(node)->parent = stPar; \
|
|
*(void**)nLeft_p = relCCld; \
|
|
}
|
|
|
|
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; \
|
|
}
|
|
/*
|
|
* Find data in a tree.
|
|
* Attention: This will change node, so normally you need to copy
|
|
* it before using this macro.
|
|
* Also be aware that found needs to be a valid lvalue and an integer.
|
|
*/
|
|
#define TR_TREE_FIND(node, search, found, comp) \
|
|
(found) = -1; \
|
|
if ((node)) { \
|
|
while(1) { \
|
|
(found) = (comp)((node)->data, (search)); \
|
|
if (0 != (found)) { \
|
|
if (! ((node)->left || (node)->right)) { \
|
|
break; \
|
|
} \
|
|
if (0 < (found)) { \
|
|
(node) = (node)->left; \
|
|
} else { \
|
|
(node) = (node)->right; \
|
|
} \
|
|
} else { \
|
|
break; \
|
|
} \
|
|
} \
|
|
}
|
|
|
|
#endif // __TR_TREE_MACROS_H__
|
|
|
|
// vim: set ts=4 sw=4:
|