Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
b_tree_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <exception>
37#include <iostream>
38
39#include <ah-errors.H>
40#include <tpl_b_tree.H>
41
42using namespace Aleph;
43
44namespace
45{
46 template <typename T>
47 void print_array(const char * label, const Array<T> & values)
48 {
49 std::cout << label << " [";
50 for (size_t i = 0; i < values.size(); ++i)
51 {
52 if (i != 0)
53 std::cout << ", ";
54 std::cout << values[i];
55 }
56 std::cout << "]\n";
57 }
58}
59
60int main()
61{
62 try
63 {
65
66 Catalog catalog = {40, 10, 90, 20, 70, 60, 30, 50, 80};
67
68 std::cout << "B-Tree example\n";
69 std::cout << "==============\n";
70 print_array("initial keys :", catalog.keys());
71 std::cout << "height : " << catalog.height() << '\n';
72
73 if (const auto lb = catalog.lower_bound(55); lb.has_value())
74 std::cout << "lower_bound(55) -> " << *lb << '\n';
75
76 if (const auto ub = catalog.upper_bound(60); ub.has_value())
77 std::cout << "upper_bound(60) -> " << *ub << '\n';
78
79 catalog.insert(65);
80 catalog.insert(75);
81 catalog.insert(5);
82 catalog.remove(40);
83 catalog.remove(20);
84
86 << "B_Tree example produced an invalid tree";
87
88 std::cout << "\nafter updates\n";
89 std::cout << "-------------\n";
90 print_array("sorted keys :", catalog.keys());
91 std::cout << "size : " << catalog.size() << '\n';
92 std::cout << "height : " << catalog.height() << '\n';
93
94 if (const auto mn = catalog.min_key(); mn.has_value())
95 std::cout << "min : " << *mn << '\n';
96
97 if (const auto mx = catalog.max_key(); mx.has_value())
98 std::cout << "max : " << *mx << '\n';
99
100 std::cout << "\nB-Tree keeps data sorted while internal nodes also hold keys.\n";
101 }
102 catch (const std::exception & ex)
103 {
104 std::cerr << "b_tree_example failed: " << ex.what() << '\n';
105 return 1;
106 }
107
108 return 0;
109}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:250
int main()
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
Generic B-Tree with configurable minimum degree.
Definition tpl_b_tree.H:134
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void print_array(const char *label, const Container &a)
B-Tree implementation with configurable minimum degree.