|
| 1 | +/** |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Project: Fossil Logic |
| 4 | + * |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop |
| 6 | + * high-performance, cross-platform applications and libraries. The code |
| 7 | + * contained herein is licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. You may obtain |
| 9 | + * a copy of the License at: |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + * Author: Michael Gene Brockus (Dreamer) |
| 20 | + * Date: 04/05/2014 |
| 21 | + * |
| 22 | + * Copyright (C) 2014-2025 Fossil Logic. All rights reserved. |
| 23 | + * ----------------------------------------------------------------------------- |
| 24 | + */ |
| 25 | +#ifndef FOSSIL_TOFU_TREE_H |
| 26 | +#define FOSSIL_TOFU_TREE_H |
| 27 | + |
| 28 | +#include "tofu.h" |
| 29 | + |
| 30 | +#ifdef __cplusplus |
| 31 | +extern "C" |
| 32 | +{ |
| 33 | +#endif |
| 34 | + |
| 35 | +// ***************************************************************************** |
| 36 | +// Type Definitions |
| 37 | +// ***************************************************************************** |
| 38 | + |
| 39 | +typedef struct fossil_tree_node_t { |
| 40 | + fossil_tofu_t *value; |
| 41 | + struct fossil_tree_node_t *left; |
| 42 | + struct fossil_tree_node_t *right; |
| 43 | +} fossil_tree_node_t; |
| 44 | + |
| 45 | +typedef struct { |
| 46 | + fossil_tree_node_t *root; |
| 47 | + char *type; |
| 48 | + size_t size; |
| 49 | +} fossil_tree_t; |
| 50 | + |
| 51 | +/** |
| 52 | + * ----------------------------------------------------------------------------- |
| 53 | + * Tree Management Functions |
| 54 | + * ----------------------------------------------------------------------------- |
| 55 | + */ |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Creates a new tree structure. |
| 59 | + * |
| 60 | + * @param type The type of values the tree will hold. |
| 61 | + * |
| 62 | + * @return Pointer to the newly created fossil_tree_t structure. |
| 63 | + */ |
| 64 | +fossil_tree_t* fossil_tree_create(char *type); |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Destroys the specified tree and releases its resources. |
| 68 | + * |
| 69 | + * @param tree Pointer to the tree to be destroyed. |
| 70 | + */ |
| 71 | +void fossil_tree_destroy(fossil_tree_t *tree); |
| 72 | + |
| 73 | +/** |
| 74 | + * ----------------------------------------------------------------------------- |
| 75 | + * Node Operations |
| 76 | + * ----------------------------------------------------------------------------- |
| 77 | + */ |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Creates a new tree node with the given value. |
| 81 | + * |
| 82 | + * @param value Pointer to the value to store in the node. |
| 83 | + * @return Pointer to the newly created fossil_tree_node_t structure. |
| 84 | + */ |
| 85 | +fossil_tree_node_t* fossil_tree_create_node(fossil_tofu_t *value); |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Inserts a value into the tree. |
| 89 | + * |
| 90 | + * @param tree Pointer to the tree where the value will be inserted. |
| 91 | + * @param value Pointer to the value to insert. |
| 92 | + * @return 0 on success, non-zero on failure. |
| 93 | + */ |
| 94 | +int fossil_tree_insert(fossil_tree_t *tree, fossil_tofu_t *value); |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Searches for a node with the specified value in the tree. |
| 98 | + * |
| 99 | + * @param tree Pointer to the tree to search. |
| 100 | + * @param value Pointer to the value to search for. |
| 101 | + * @return Pointer to the found node, or NULL if not found. |
| 102 | + */ |
| 103 | +fossil_tree_node_t* fossil_tree_search(fossil_tree_t *tree, const fossil_tofu_t *value); |
| 104 | + |
| 105 | +/** |
| 106 | + * ----------------------------------------------------------------------------- |
| 107 | + * Traversal Functions |
| 108 | + * ----------------------------------------------------------------------------- |
| 109 | + */ |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Function pointer type for visiting tree nodes during traversal. |
| 113 | + * |
| 114 | + * @param value Pointer to the value in the node being visited. |
| 115 | + */ |
| 116 | +typedef void (*fossil_tree_visit_fn)(fossil_tofu_t *value); |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief Traverses the tree using the specified visit function. |
| 120 | + * |
| 121 | + * @param tree Pointer to the tree to traverse. |
| 122 | + * @param visit Function to call for each node's value. |
| 123 | + */ |
| 124 | +void fossil_tree_traverse(fossil_tree_t *tree, fossil_tree_visit_fn visit); |
| 125 | + |
| 126 | +/** |
| 127 | + * @brief Performs an inorder traversal of the tree nodes. |
| 128 | + * |
| 129 | + * @param node Pointer to the current node. |
| 130 | + * @param visit Function to call for each node's value. |
| 131 | + */ |
| 132 | +void fossil_tree_traverse_inorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit); |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Performs a preorder traversal of the tree nodes. |
| 136 | + * |
| 137 | + * @param node Pointer to the current node. |
| 138 | + * @param visit Function to call for each node's value. |
| 139 | + */ |
| 140 | +void fossil_tree_traverse_preorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit); |
| 141 | + |
| 142 | +/** |
| 143 | + * @brief Performs a postorder traversal of the tree nodes. |
| 144 | + * |
| 145 | + * @param node Pointer to the current node. |
| 146 | + * @param visit Function to call for each node's value. |
| 147 | + */ |
| 148 | +void fossil_tree_traverse_postorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit); |
| 149 | + |
| 150 | +#ifdef __cplusplus |
| 151 | +} |
| 152 | +#include <stdexcept> |
| 153 | +#include <string> |
| 154 | + |
| 155 | +namespace fossil { |
| 156 | + |
| 157 | +namespace tofu { |
| 158 | + |
| 159 | + /** |
| 160 | + * @class Tree |
| 161 | + * @brief A C++ wrapper class for managing a binary tree using the Fossil Logic library and Tofu objects. |
| 162 | + */ |
| 163 | + class Tree { |
| 164 | + public: |
| 165 | + /** |
| 166 | + * @brief Constructs a new Tree object. |
| 167 | + * @throws std::runtime_error If the tree cannot be created. |
| 168 | + */ |
| 169 | + explicit Tree(const std::string& type) { |
| 170 | + tree_ = fossil_tree_create(const_cast<char*>(type.c_str())); |
| 171 | + if (!tree_) { |
| 172 | + throw std::runtime_error("Failed to create tree."); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @brief Destroys the Tree object and releases its resources. |
| 178 | + */ |
| 179 | + ~Tree() { |
| 180 | + fossil_tree_destroy(tree_); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @brief Inserts a new Tofu value into the tree. |
| 185 | + * @param value The Tofu object to insert. |
| 186 | + * @throws std::runtime_error If the insertion fails. |
| 187 | + */ |
| 188 | + void insert(const Tofu& value) { |
| 189 | + fossil_tofu_t tofu_copy = value.get_c_struct(); |
| 190 | + if (fossil_tree_insert(tree_, &tofu_copy) != 0) { |
| 191 | + throw std::runtime_error("Failed to insert value into tree."); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @brief Searches for a node with the specified Tofu value in the tree. |
| 197 | + * @param value The Tofu object to search for. |
| 198 | + * @return Pointer to the found node, or nullptr if not found. |
| 199 | + */ |
| 200 | + fossil_tree_node_t* search(const Tofu& value) const { |
| 201 | + return fossil_tree_search(tree_, &value.get_c_struct()); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @brief Gets the number of nodes in the tree. |
| 206 | + * @return The number of nodes in the tree. |
| 207 | + */ |
| 208 | + size_t size() const { |
| 209 | + return tree_->size; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @brief Checks if the tree is empty. |
| 214 | + * @return True if the tree is empty, false otherwise. |
| 215 | + */ |
| 216 | + bool is_empty() const { |
| 217 | + return tree_->size == 0; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @brief Traverses the tree using the specified visit function. |
| 222 | + * @param visit Function to call for each node's value. |
| 223 | + */ |
| 224 | + void traverse(fossil_tree_visit_fn visit) { |
| 225 | + fossil_tree_traverse(tree_, visit); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @brief Performs an inorder traversal of the tree nodes. |
| 230 | + * @param visit Function to call for each node's value. |
| 231 | + */ |
| 232 | + void traverse_inorder(fossil_tree_visit_fn visit) { |
| 233 | + fossil_tree_traverse_inorder(tree_->root, visit); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * @brief Performs a preorder traversal of the tree nodes. |
| 238 | + * @param visit Function to call for each node's value. |
| 239 | + */ |
| 240 | + void traverse_preorder(fossil_tree_visit_fn visit) { |
| 241 | + fossil_tree_traverse_preorder(tree_->root, visit); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @brief Performs a postorder traversal of the tree nodes. |
| 246 | + * @param visit Function to call for each node's value. |
| 247 | + */ |
| 248 | + void traverse_postorder(fossil_tree_visit_fn visit) { |
| 249 | + fossil_tree_traverse_postorder(tree_->root, visit); |
| 250 | + } |
| 251 | + |
| 252 | + private: |
| 253 | + fossil_tree_t* tree_; /**< Pointer to the underlying C tree structure. */ |
| 254 | + }; |
| 255 | + |
| 256 | +} // namespace tofu |
| 257 | + |
| 258 | +} // namespace fossil |
| 259 | + |
| 260 | +#endif |
| 261 | + |
| 262 | +#endif /* FOSSIL_TOFU_FRAMEWORK_H */ |
0 commit comments