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.
138 lines
5.2 KiB
138 lines
5.2 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_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_RIGHT_LEFT(node) \
|
|
(NULL!=TR_TREE_RIGHT((node))?TR_TREE_LEFT(TR_TREE_RIGHT((node))):NULL)
|
|
|
|
#define TR_TREE_LEFT_RIGHT(node) \
|
|
(NULL!=TR_TREE_LEFT((node))?TR_TREE_RIGHT(TR_TREE_LEFT((node))):NULL)
|
|
|
|
#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) \
|
|
(NULL!=TR_TREE_PARENT((node))?TR_TREE_PARENT((node))->parent:NULL)
|
|
|
|
#define TR_TREE_UNCLE(node) \
|
|
(NULL!=TR_TREE_GRANDPARENT((node))? \
|
|
(TR_TREE_PARENT((node))==TR_TREE_GRANDPARENT((node))->left? \
|
|
TR_TREE_GRANDPARENT((node))->right: \
|
|
TR_TREE_GRANDPARENT((node))->left): \
|
|
NULL)
|
|
|
|
#define TR_TREE_ROTATE_LEFT(root, node) \
|
|
do { \
|
|
if (NULL != TR_TREE_RIGHT_LEFT((node))) { \
|
|
TR_TREE_RIGHT_LEFT((node))->parent = (node); \
|
|
} \
|
|
TR_TREE_RIGHT((node))->left = (node); \
|
|
if (NULL != TR_TREE_PARENT((node))) { \
|
|
if (TR_TREE_PARENT((node))->left==(node)) { \
|
|
TR_TREE_PARENT((node))->left = (node)->right; \
|
|
} else { \
|
|
TR_TREE_PARENT((node))->right = (node)->right; \
|
|
} \
|
|
} else { \
|
|
*(root) = (node)->right; \
|
|
} \
|
|
(node)->right = TR_TREE_RIGHT_LEFT((node)); \
|
|
(node)->parent = (node)->right; \
|
|
TR_TREE_RIGHT((node))->parent = (node)->parent; \
|
|
} while(0)
|
|
|
|
#define TR_TREE_ROTATE_RIGHT(root, node) \
|
|
do { \
|
|
if (NULL != TR_TREE_LEFT_RIGHT((node))) { \
|
|
TR_TREE_LEFT_RIGHT((node))->parent = (node); \
|
|
} \
|
|
TR_TREE_LEFT((node))->right = (node); \
|
|
if (NULL != TR_TREE_PARENT((node))) { \
|
|
if (TR_TREE_PARENT((node))->left==(node)) { \
|
|
TR_TREE_PARENT((node))->left = (node)->left; \
|
|
} else { \
|
|
TR_TREE_PARENT((node))->right = (node)->left; \
|
|
} \
|
|
} else { \
|
|
*(root) = (node)->left; \
|
|
} \
|
|
TR_TREE_LEFT((node))->parent = (node)->parent; \
|
|
(node)->left = TR_TREE_LEFT_RIGHT((node)); \
|
|
(node)->parent = (node)->right; \
|
|
} while(0)
|
|
|
|
#define TR_TREE_REPLACE_NODE(root, node1, node2) \
|
|
do { \
|
|
if (NULL != TR_TREE_PARENT((node1))) { \
|
|
if ((node1) == TR_TREE_PARENT((node1))->left) { \
|
|
TR_TREE_PARENT((node1))->left = (node2); \
|
|
} else { \
|
|
TR_TREE_PARENT((node1))->right = (node2); \
|
|
} \
|
|
} else { \
|
|
*(root) = (node2); \
|
|
} \
|
|
if (NULL != (node2)) { \
|
|
(node2)->parent = (node1)->parent; \
|
|
} \
|
|
} while(0)
|
|
|
|
|
|
typedef enum {rbBlack=1, rbRed=2} TR_rbColor;
|
|
|
|
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:
|