cdcontainers  0.1.1
Library of data containers and collections for C programming language.
tree-utils.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2018 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 #ifndef CDCONTAINERS_SRC_TREE_H
22 #define CDCONTAINERS_SRC_TREE_H
23 
24 #include <cdcontainers/data-info.h>
25 
26 #include <stddef.h>
27 
28 #define CDC_MAKE_FIND_NODE_FN(T) \
29  static T cdc_find_tree_node(T node, void *key, cdc_binary_pred_fn_t cmp) \
30  { \
31  while (node != NULL && cdc_not_eq(cmp, node->key, key)) { \
32  if (cmp(key, node->key)) { \
33  node = node->left; \
34  } else { \
35  node = node->right; \
36  } \
37  } \
38  return node; \
39  }
40 
41 #define CDC_MAKE_MIN_NODE_FN(T) \
42  static T cdc_min_tree_node(T node) \
43  { \
44  if (node == NULL) { \
45  return NULL; \
46  } \
47  while (node->left != NULL) { \
48  node = node->left; \
49  } \
50  return node; \
51  }
52 
53 #define CDC_MAKE_MAX_NODE_FN(T) \
54  static T cdc_max_tree_node(T node) \
55  { \
56  if (node == NULL) { \
57  return NULL; \
58  } \
59  while (node->right != NULL) { \
60  node = node->right; \
61  } \
62  return node; \
63  }
64 
65 #define CDC_MAKE_SUCCESSOR_FN(T) \
66  static T cdc_tree_successor(T node) \
67  { \
68  if (node->right) { \
69  return cdc_min_tree_node(node->right); \
70  } \
71  T p = node->parent; \
72  while (p && node == p->right) { \
73  node = p; \
74  p = p->parent; \
75  } \
76  return p; \
77  }
78 
79 #define CDC_MAKE_PREDECESSOR_FN(T) \
80  static T cdc_tree_predecessor(T node) \
81  { \
82  if (node->left) { \
83  return cdc_max_tree_node(node->left); \
84  } \
85  T p = node->parent; \
86  while (p && node == p->left) { \
87  node = p; \
88  p = p->parent; \
89  } \
90  return p; \
91  }
92 
93 #define CDC_MAKE_TREE_HEIGTH_FN(T) \
94  static size_t cdc_tree_height(T node) \
95  { \
96  if (node == NULL) { \
97  return 0; \
98  } \
99  size_t lh = cdc_tree_height(node->left); \
100  size_t rh = cdc_tree_height(node->right); \
101  return CDC_MAX(lh, rh) + 1; \
102  }
103 
104 #endif // CDCONTAINERS_SRC_TREE_H