Tofu Tree

The Fossil Tofu Tree is a lightweight, portable binary tree implementation designed to store and manage fossil_tofu_t objects efficiently. It provides a simple, C-based interface for creating, inserting, searching, and traversing nodes while allowing users to specify the type of values stored. The tree supports three common traversal orders—inorder, preorder, and postorder—with a customizable visit function. A C++ wrapper class, fossil::tofu::Tree, is included to provide a more idiomatic, RAII-compliant interface for C++ projects, including exception handling and convenient member functions for insertion, searching, and iteration.

HEADER REFERENCE #

#include <iostream>
#include "fossil/tofu/tree.h"

using namespace fossil::tofu;

// Example visitor function
void print_node(fossil_tofu_t *value) {
    std::cout << "Node value: " << fossil_tofu_to_string(value) << "\n";
}

int main() {
    try {
        // Create tree for storing string values
        Tree tree("cstr");

        // Insert nodes
        Tofu value1 = Tofu::from_string("apple");
        Tofu value2 = Tofu::from_string("banana");
        Tofu value3 = Tofu::from_string("cherry");

        tree.insert(value1);
        tree.insert(value2);
        tree.insert(value3);

        // Traverse tree
        std::cout << "Inorder traversal:" << std::endl;
        tree.traverse_inorder(print_node);

        // Search for a node
        auto found = tree.search(value2);
        if (found) {
            std::cout << "Found node with value: " 
                      << fossil_tofu_to_string(found->value) << std::endl;
        }

        std::cout << "Tree size: " << tree.size() << std::endl;

    } catch (const std::runtime_error& e) {
        std::cerr << "Tree error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

SAMPLE CODE C #

#include <iostream>
#include "fossil/tofu/tree.h"

using namespace fossil::tofu;

// Example visitor function
void print_node(fossil_tofu_t *value) {
    std::cout << "Node value: " << fossil_tofu_to_string(value) << "\n";
}

int main() {
    try {
        // Create tree for storing string values
        Tree tree("cstr");

        // Insert nodes
        Tofu value1 = Tofu::from_string("apple");
        Tofu value2 = Tofu::from_string("banana");
        Tofu value3 = Tofu::from_string("cherry");

        tree.insert(value1);
        tree.insert(value2);
        tree.insert(value3);

        // Traverse tree
        std::cout << "Inorder traversal:" << std::endl;
        tree.traverse_inorder(print_node);

        // Search for a node
        auto found = tree.search(value2);
        if (found) {
            std::cout << "Found node with value: " 
                      << fossil_tofu_to_string(found->value) << std::endl;
        }

        std::cout << "Tree size: " << tree.size() << std::endl;

    } catch (const std::runtime_error& e) {
        std::cerr << "Tree error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

SAMPLE CODE C++ #

#include <iostream>
#include "fossil/tofu/tree.h"

using namespace fossil::tofu;

// Example visitor function
void print_node(fossil_tofu_t *value) {
    std::cout << "Node value: " << fossil_tofu_to_string(value) << "\n";
}

int main() {
    try {
        // Create tree for storing string values
        Tree tree("cstr");

        // Insert nodes
        Tofu value1 = Tofu::from_string("apple");
        Tofu value2 = Tofu::from_string("banana");
        Tofu value3 = Tofu::from_string("cherry");

        tree.insert(value1);
        tree.insert(value2);
        tree.insert(value3);

        // Traverse tree
        std::cout << "Inorder traversal:" << std::endl;
        tree.traverse_inorder(print_node);

        // Search for a node
        auto found = tree.search(value2);
        if (found) {
            std::cout << "Found node with value: " 
                      << fossil_tofu_to_string(found->value) << std::endl;
        }

        std::cout << "Tree size: " << tree.size() << std::endl;

    } catch (const std::runtime_error& e) {
        std::cerr << "Tree error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

What are your feelings

Updated on September 27, 2025