libdict
Data Structure C Library
Loading...
Searching...
No Matches
tree_common.h
Go to the documentation of this file.
1/*
2 * libdict - common definitions for binary search trees.
3 *
4 * Copyright (c) 2001-2014, Farooq Mela
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef LIBDICT_TREE_COMMON_H__
29#define LIBDICT_TREE_COMMON_H__
30
31#include "dict.h"
32
33#define TREE_NODE_FIELDS(node_type) \
34 void* key; \
35 void* datum; \
36 node_type* parent; \
37 node_type* llink; \
38 node_type* rlink
39
43
44#define TREE_FIELDS(node_type) \
45 node_type* root; \
46 size_t count; \
47 dict_compare_func cmp_func; \
48 size_t rotation_count
49
53
54#define TREE_ITERATOR_FIELDS(tree_type, node_type) \
55 tree_type* tree; \
56 node_type* node
57
58/* Rotate |node| left.
59 * |node| and |node->rlink| must not be NULL. */
60void tree_node_rot_left(void *tree, void *node);
61/* Rotate |node| right.
62 * |node| and |node->llink| must not be NULL. */
63void tree_node_rot_right(void *tree, void *node);
64/* Return the predecessor of |node|, or NULL if |node| has no predecessor.
65 * |node| must not be NULL. */
66void* tree_node_prev(void *node);
67/* Return the successor of |node|, or NULL if |node| has no successor.
68 * |node| must not be NULL. */
69void* tree_node_next(void *node);
70/* Return the left child of |node|, or |node| if it has no right child.
71 * |node| must not be NULL. */
72void* tree_node_min(void *node);
73/* Return the rightmost child of |node|, or |node| if it has no right child.
74 * |node| must not be NULL. */
75void* tree_node_max(void *node);
76/* Return the address of the data for the given the key, or NULL if not found. */
77void** tree_search(void *tree, const void *key);
78/* Return the node has the key, or NULL if not found. */
79void* tree_search_node(void *tree, const void *key);
80/* Return the data/node associated with the first key less than or
81 * equal to the specified key, or NULL if not found. */
82void** tree_search_le(void *tree, const void *key);
83void* tree_search_le_node(void *tree, const void *key);
84/* Return the data/node associated with the first key less than the
85 * specified key, or NULL if not found. */
86void** tree_search_lt(void *tree, const void *key);
87void* tree_search_lt_node(void *tree, const void *key);
88/* Return the data/node associated with the first key greater than or
89 * equal to the specified key, or NULL if not found. */
90void** tree_search_ge(void *tree, const void *key);
91void* tree_search_ge_node(void *tree, const void *key);
92/* Return the data/node associated with the first key greater than the
93 * specified key, or NULL if not found. */
94void** tree_search_gt(void *tree, const void *key);
95void* tree_search_gt_node(void *tree, const void *key);
96/* Traverses the tree in order, calling |visit| with each key and value pair,
97 * stopping if |visit| returns false. Returns the number of times |visit| was
98 * called. */
99size_t tree_traverse(void *tree, dict_visit_func visit, void* user_data);
100/* Put the key and datum of the |n|th element of |tree| into |key| and |datum|
101 * and return true, or, if n is greater than or equal to the number of elements,
102 * return false. */
103bool tree_select(void *tree, size_t n, const void **key, void **datum);
104/* Return a count of the elements in |tree|. */
105size_t tree_count(const void *tree);
106/* Remove all elements from |tree|. */
107size_t tree_clear(void *tree, dict_delete_func delete_func);
108/* Remove all elements from |tree| and free its memory. */
109size_t tree_free(void *tree, dict_delete_func delete_func);
110/* Returns the depth of the leaf with minimal depth, or 0 for an empty tree. */
111size_t tree_min_path_length(const void *tree);
112/* Returns the depth of the leaf with maximal depth, or 0 for an empty tree. */
113size_t tree_max_path_length(const void *tree);
114/* Returns the total path length of the tree. */
115size_t tree_total_path_length(const void *tree);
116
117bool tree_iterator_valid(const void *iterator);
118void tree_iterator_invalidate(void *iterator);
119void tree_iterator_free(void *iterator);
120bool tree_iterator_next(void *iterator);
121bool tree_iterator_prev(void *iterator);
122bool tree_iterator_nextn(void *iterator, size_t count);
123bool tree_iterator_prevn(void *iterator, size_t count);
124bool tree_iterator_first(void *iterator);
125bool tree_iterator_last(void *iterator);
126bool tree_iterator_search(void *iterator, const void *key);
127bool tree_iterator_search_le(void *iterator, const void *key);
128bool tree_iterator_search_lt(void *iterator, const void *key);
129bool tree_iterator_search_ge(void *iterator, const void *key);
130bool tree_iterator_search_gt(void *iterator, const void *key);
131int tree_iterator_compare(const void* iterator1, const void* iterator2);
132const void* tree_iterator_key(const void *iterator);
133void** tree_iterator_datum(void *iterator);
134
135#endif /* !LIBDICT_TREE_COMMON_H__ */
void(* dict_delete_func)(void *, void *)
Definition dict.h:57
bool(* dict_visit_func)(const void *, void *, void *)
Definition dict.h:59
bool tree_iterator_valid(const void *iterator)
void ** tree_iterator_datum(void *iterator)
void ** tree_search_lt(void *tree, const void *key)
void tree_iterator_free(void *iterator)
void tree_node_rot_right(void *tree, void *node)
Definition tree_common.c:62
void * tree_search_node(void *tree, const void *key)
size_t tree_free(void *tree, dict_delete_func delete_func)
size_t tree_min_path_length(const void *tree)
bool tree_iterator_next(void *iterator)
void * tree_node_max(void *node)
bool tree_iterator_nextn(void *iterator, size_t count)
bool tree_iterator_prev(void *iterator)
size_t tree_traverse(void *tree, dict_visit_func visit, void *user_data)
void * tree_search_gt_node(void *tree, const void *key)
size_t tree_max_path_length(const void *tree)
bool tree_iterator_first(void *iterator)
void ** tree_search(void *tree, const void *key)
void * tree_search_ge_node(void *tree, const void *key)
void ** tree_search_le(void *tree, const void *key)
bool tree_iterator_search_ge(void *iterator, const void *key)
size_t tree_clear(void *tree, dict_delete_func delete_func)
void ** tree_search_gt(void *tree, const void *key)
bool tree_iterator_prevn(void *iterator, size_t count)
#define TREE_NODE_FIELDS(node_type)
Definition tree_common.h:33
struct tree_node_base tree_node_base
void * tree_node_prev(void *node)
Definition tree_common.c:78
void tree_node_rot_left(void *tree, void *node)
Definition tree_common.c:46
void * tree_node_next(void *node)
Definition tree_common.c:92
const void * tree_iterator_key(const void *iterator)
#define TREE_FIELDS(node_type)
Definition tree_common.h:44
bool tree_iterator_search_gt(void *iterator, const void *key)
bool tree_iterator_last(void *iterator)
int tree_iterator_compare(const void *iterator1, const void *iterator2)
bool tree_select(void *tree, size_t n, const void **key, void **datum)
void * tree_search_lt_node(void *tree, const void *key)
bool tree_iterator_search_le(void *iterator, const void *key)
struct tree_base tree_base
bool tree_iterator_search_lt(void *iterator, const void *key)
void * tree_search_le_node(void *tree, const void *key)
void tree_iterator_invalidate(void *iterator)
size_t tree_count(const void *tree)
size_t tree_total_path_length(const void *tree)
void * tree_node_min(void *node)
bool tree_iterator_search(void *iterator, const void *key)
void ** tree_search_ge(void *tree, const void *key)