/* btree.cpp * Date: 2021-11-22 * Author: Aryadev Chavali */ enum Order { LT, GT, EQ }; template struct BinaryTree { T value; BinaryTree *left, *right; enum Order (*compare)(T, T); ~BinaryTree() { delete left; delete right; } }; template BinaryTree *insert(BinaryTree *tree, T value) { BinaryTree **node = &tree; switch (tree->compare(value, tree->value)) { case LT: node = &tree->left; break; case EQ: case GT: node = &tree->right; break; } if (*node) { *node = insert(*node, value); return tree; } *node = new BinaryTree; (*node)->value = value; (*node)->left = (*node)->right = nullptr; (*node)->compare = tree->compare; return tree; }