stdtree is a small C++20 tree library that provides a TreeNode implementation plus a TreeView wrapper for building, traversing, and printing tree structures.
- Create tree nodes with values, depth, and index metadata
- Add child nodes and navigate siblings/parents
- Inspect descendants, paths, and subtree contents
- Print trees in an ASCII-style layout with
printTree() - Build and test with CMake and CTest
stdtree.hpp— public umbrella header forTreeViewtreenode.hpp—TreeNodeimplementationmain.cpp— example program that builds and prints a sample treetests/stdtree_tests.cpp— unit testsCMakeLists.txt— build and test configuration
From the project root:
cmake -S . -B build
cmake --build build./build/stdtreeExample output:
└──(0, 0)
├──(1, 1)
│ ├──(3, 3)
│ └──(4, 4)
└──(2, 2)
├──(5, 5)
│ ├──(7, 7)
│ └──(8, 8)
└──(6, 6)
ctest --test-dir build --output-on-failure- C++20 compiler
- CMake 3.10 or newer
#include <iostream>
#include <memory>
#include <utility>
#include "stdtree.hpp"
struct Geo2D
{
double x;
double y;
Geo2D(double x, double y) : x(x), y(y) {}
friend std::ostream& operator<<(std::ostream& os, const Geo2D& geo)
{
os << "(" << geo.x << ", " << geo.y << ")";
return os;
}
};
int main()
{
TreeView<Geo2D, std::size_t> tree(std::make_unique<TreeNode<Geo2D, std::size_t>>(Geo2D(0, 0), 0, 0));
tree.getRoot()->addChild(Geo2D(1, 1));
tree.getRoot()->addChild(Geo2D(2, 2));
tree.getRoot()->getChild(0)->addChild(Geo2D(3, 3));
tree.getRoot()->getChild(0)->addChild(Geo2D(4, 4));
tree.getRoot()->getChild(1)->addChild(Geo2D(5, 5));
tree.getRoot()->getChild(1)->addChild(Geo2D(6, 6));
tree.getRoot()->getChild(1)->getChild(0)->addChild(Geo2D(7, 7));
tree.getRoot()->getChild(1)->getChild(0)->addChild(Geo2D(8, 8));
tree.printTree();
return 0;
}